-
Notifications
You must be signed in to change notification settings - Fork 2
/
matcher.go
216 lines (192 loc) · 5.14 KB
/
matcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
import (
"fmt"
"regexp"
"strconv"
"github.com/cli/go-gh/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
)
type Match interface {
// Reference returns a string to reference this match by
Reference() string
// URL returns a URL to this match
URL() string
// Title fetches the title of a match from the API
Title(client api.GQLClient) (string, error)
}
type Issue struct {
Owner string
Repo string
Num string
}
type Pull struct {
Owner string
Repo string
Num string
}
type Discussion struct {
Owner string
Repo string
Num string
}
func (i Issue) Reference() string {
return i.Owner + "/" + i.Repo + "#" + i.Num
}
func (i Issue) URL() string {
return fmt.Sprintf("https://github.com/%s/%s/issues/%s", i.Owner, i.Repo, i.Num)
}
func (i Issue) Title(client api.GQLClient) (string, error) {
var query struct {
Repository struct {
IssueOrPullRequest struct {
Issue struct {
Title string
} `graphql:"... on Issue"`
PullRequest struct {
Title string
} `graphql:"... on PullRequest"`
} `graphql:"issueOrPullRequest(number: $number)"`
Discussion struct {
Title string
} `graphql:"discussion(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
num, err := strconv.Atoi(i.Num)
if err != nil {
return "", err
}
variables := map[string]interface{}{
"owner": graphql.String(i.Owner),
"name": graphql.String(i.Repo),
"number": graphql.Int(num),
}
err = client.Query("IssueTitle", &query, variables)
// ignore error unless no title was found - a "not found" is expected for either
// the issue-ish or the discussion.
if query.Repository.IssueOrPullRequest.Issue.Title != "" {
return query.Repository.IssueOrPullRequest.Issue.Title, nil
}
if query.Repository.IssueOrPullRequest.PullRequest.Title != "" {
return query.Repository.IssueOrPullRequest.PullRequest.Title, nil
}
if query.Repository.Discussion.Title != "" {
return query.Repository.Discussion.Title, nil
}
return "", err
}
func (p Pull) Reference() string {
return p.Owner + "/" + p.Repo + "#" + p.Num
}
func (p Pull) URL() string {
return fmt.Sprintf("https://github.com/%s/%s/pull/%s", p.Owner, p.Repo, p.Num)
}
func (p Pull) Title(client api.GQLClient) (string, error) {
var query struct {
Repository struct {
IssueOrPullRequest struct {
Issue struct {
Title string
} `graphql:"... on Issue"`
PullRequest struct {
Title string
} `graphql:"... on PullRequest"`
} `graphql:"issueOrPullRequest(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
num, err := strconv.Atoi(p.Num)
if err != nil {
return "", err
}
variables := map[string]interface{}{
"owner": graphql.String(p.Owner),
"name": graphql.String(p.Repo),
"number": graphql.Int(num),
}
err = client.Query("IssueTitle", &query, variables)
if query.Repository.IssueOrPullRequest.Issue.Title != "" {
return query.Repository.IssueOrPullRequest.Issue.Title, nil
}
if query.Repository.IssueOrPullRequest.PullRequest.Title != "" {
return query.Repository.IssueOrPullRequest.PullRequest.Title, nil
}
return "", err
}
func (d Discussion) Reference() string {
return d.Owner + "/" + d.Repo + "#" + d.Num
}
func (d Discussion) URL() string {
return fmt.Sprintf("https://github.com/%s/%s/discussions/%s", d.Owner, d.Repo, d.Num)
}
func (d Discussion) Title(client api.GQLClient) (string, error) {
var query struct {
Repository struct {
Discussion struct {
Title string
} `graphql:"discussion(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
num, err := strconv.Atoi(d.Num)
if err != nil {
return "", err
}
variables := map[string]interface{}{
"owner": graphql.String(d.Owner),
"name": graphql.String(d.Repo),
"number": graphql.Int(num),
}
err = client.Query("DiscussionTitle", &query, variables)
if query.Repository.Discussion.Title != "" {
return query.Repository.Discussion.Title, nil
}
return "", err
}
var urlReferencePattern = regexp.MustCompile(`https://github.com/([^/]+)/([^/]+)/(issues|pull|discussions)/(\d+)(.*)`)
var issueReferencePattern = regexp.MustCompile(`\b([^/]+)/([^/#]+)#(\d+)\b`)
// match attempts to find a Match (issue, PR, discussion) in the given input.
func match(input string) (Match, bool) {
if ref, ok := matchURL(input); ok {
return ref, true
}
if issue, ok := matchIssueReference(input); ok {
return issue, true
}
return nil, false
}
func matchIssueReference(input string) (Issue, bool) {
matches := issueReferencePattern.FindStringSubmatch(input)
if matches != nil {
return Issue{
Owner: matches[1],
Repo: matches[2],
Num: matches[3],
}, true
}
return Issue{}, false
}
func matchURL(url string) (Match, bool) {
matches := urlReferencePattern.FindStringSubmatch(url)
if matches == nil {
return nil, false
}
switch matches[3] {
case "issues":
return Issue{
Owner: matches[1],
Repo: matches[2],
Num: matches[4],
}, true
case "pull":
return Pull{
Owner: matches[1],
Repo: matches[2],
Num: matches[4],
}, true
case "discussions":
return Discussion{
Owner: matches[1],
Repo: matches[2],
Num: matches[4],
}, true
}
return nil, false
}