-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgithub.go
More file actions
290 lines (257 loc) · 7.48 KB
/
github.go
File metadata and controls
290 lines (257 loc) · 7.48 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package github
import (
"fmt"
"github.com/cli/go-gh/v2/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
)
// PullRequest represents a GitHub pull request.
type PullRequest struct {
ID string `graphql:"id"`
Number int `graphql:"number"`
Title string `graphql:"title"`
State string `graphql:"state"`
URL string `graphql:"url"`
HeadRefName string `graphql:"headRefName"`
BaseRefName string `graphql:"baseRefName"`
IsDraft bool `graphql:"isDraft"`
Merged bool `graphql:"merged"`
}
// Client wraps GitHub API operations.
type Client struct {
gql *api.GraphQLClient
rest *api.RESTClient
host string
owner string
repo string
slug string
}
// NewClient creates a new GitHub API client for the given repository.
// The host parameter specifies the GitHub hostname (e.g. "github.com" or a
// GHES hostname like "github.mycompany.com"). If empty, it defaults to
// "github.com".
func NewClient(host, owner, repo string) (*Client, error) {
if host == "" {
host = "github.com"
}
opts := api.ClientOptions{Host: host}
gql, err := api.NewGraphQLClient(opts)
if err != nil {
return nil, fmt.Errorf("creating GraphQL client: %w", err)
}
rest, err := api.NewRESTClient(opts)
if err != nil {
return nil, fmt.Errorf("creating REST client: %w", err)
}
return &Client{
gql: gql,
rest: rest,
host: host,
owner: owner,
repo: repo,
slug: owner + "/" + repo,
}, nil
}
// PRURL constructs the web URL for a pull request on the given host.
func PRURL(host, owner, repo string, number int) string {
if host == "" {
host = "github.com"
}
return fmt.Sprintf("https://%s/%s/%s/pull/%d", host, owner, repo, number)
}
// FindPRForBranch finds an open PR by head branch name.
func (c *Client) FindPRForBranch(branch string) (*PullRequest, error) {
var query struct {
Repository struct {
PullRequests struct {
Nodes []PullRequest
} `graphql:"pullRequests(headRefName: $head, states: [OPEN], first: 1)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": graphql.String(c.owner),
"name": graphql.String(c.repo),
"head": graphql.String(branch),
}
if err := c.gql.Query("FindPRForBranch", &query, variables); err != nil {
return nil, fmt.Errorf("querying PRs: %w", err)
}
nodes := query.Repository.PullRequests.Nodes
if len(nodes) == 0 {
return nil, nil
}
n := nodes[0]
return &PullRequest{
ID: n.ID,
Number: n.Number,
Title: n.Title,
State: n.State,
URL: n.URL,
HeadRefName: n.HeadRefName,
BaseRefName: n.BaseRefName,
IsDraft: n.IsDraft,
Merged: n.Merged,
}, nil
}
// FindAnyPRForBranch finds the most recent PR by head branch name regardless of state.
func (c *Client) FindAnyPRForBranch(branch string) (*PullRequest, error) {
var query struct {
Repository struct {
PullRequests struct {
Nodes []PullRequest
} `graphql:"pullRequests(headRefName: $head, last: 1)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": graphql.String(c.owner),
"name": graphql.String(c.repo),
"head": graphql.String(branch),
}
if err := c.gql.Query("FindAnyPRForBranch", &query, variables); err != nil {
return nil, fmt.Errorf("querying PRs: %w", err)
}
nodes := query.Repository.PullRequests.Nodes
if len(nodes) == 0 {
return nil, nil
}
n := nodes[0]
return &PullRequest{
ID: n.ID,
Number: n.Number,
Title: n.Title,
State: n.State,
URL: n.URL,
HeadRefName: n.HeadRefName,
BaseRefName: n.BaseRefName,
IsDraft: n.IsDraft,
Merged: n.Merged,
}, nil
}
// CreatePR creates a new pull request.
func (c *Client) CreatePR(base, head, title, body string, draft bool) (*PullRequest, error) {
var mutation struct {
CreatePullRequest struct {
PullRequest struct {
ID string
Number int
Title string
State string
URL string `graphql:"url"`
HeadRefName string
BaseRefName string
IsDraft bool
}
} `graphql:"createPullRequest(input: $input)"`
}
repoID, err := c.repositoryID()
if err != nil {
return nil, err
}
type CreatePullRequestInput struct {
RepositoryID string `json:"repositoryId"`
BaseRefName string `json:"baseRefName"`
HeadRefName string `json:"headRefName"`
Title string `json:"title"`
Body string `json:"body,omitempty"`
Draft bool `json:"draft"`
}
variables := map[string]interface{}{
"input": CreatePullRequestInput{
RepositoryID: repoID,
BaseRefName: base,
HeadRefName: head,
Title: title,
Body: body,
Draft: draft,
},
}
if err := c.gql.Mutate("CreatePullRequest", &mutation, variables); err != nil {
return nil, fmt.Errorf("creating PR: %w", err)
}
pr := mutation.CreatePullRequest.PullRequest
return &PullRequest{
ID: pr.ID,
Number: pr.Number,
Title: pr.Title,
State: pr.State,
URL: pr.URL,
HeadRefName: pr.HeadRefName,
BaseRefName: pr.BaseRefName,
IsDraft: pr.IsDraft,
}, nil
}
// PRDetails holds enriched pull request data for display in the TUI.
type PRDetails struct {
Number int
Title string
State string // OPEN, CLOSED, MERGED
URL string
IsDraft bool
Merged bool
CommentsCount int
}
// FindPRDetailsForBranch fetches enriched PR data for display purposes.
// Returns nil without error if no PR exists for the branch.
func (c *Client) FindPRDetailsForBranch(branch string) (*PRDetails, error) {
var query struct {
Repository struct {
PullRequests struct {
Nodes []struct {
ID string `graphql:"id"`
Number int `graphql:"number"`
Title string `graphql:"title"`
State string `graphql:"state"`
URL string `graphql:"url"`
HeadRefName string `graphql:"headRefName"`
BaseRefName string `graphql:"baseRefName"`
IsDraft bool `graphql:"isDraft"`
Merged bool `graphql:"merged"`
Comments struct {
TotalCount int `graphql:"totalCount"`
} `graphql:"comments"`
}
} `graphql:"pullRequests(headRefName: $head, last: 1)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": graphql.String(c.owner),
"name": graphql.String(c.repo),
"head": graphql.String(branch),
}
if err := c.gql.Query("FindPRDetailsForBranch", &query, variables); err != nil {
return nil, fmt.Errorf("querying PR details: %w", err)
}
nodes := query.Repository.PullRequests.Nodes
if len(nodes) == 0 {
return nil, nil
}
n := nodes[0]
return &PRDetails{
Number: n.Number,
Title: n.Title,
State: n.State,
URL: n.URL,
IsDraft: n.IsDraft,
Merged: n.Merged,
CommentsCount: n.Comments.TotalCount,
}, nil
}
// DeleteStack deletes a stack on GitHub.
// TODO: Implement once the stack API is available.
func (c *Client) DeleteStack() error {
return fmt.Errorf("deleting a stack on GitHub is not yet supported by the API")
}
func (c *Client) repositoryID() (string, error) {
var query struct {
Repository struct {
ID string
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": graphql.String(c.owner),
"name": graphql.String(c.repo),
}
if err := c.gql.Query("RepositoryID", &query, variables); err != nil {
return "", fmt.Errorf("fetching repository ID: %w", err)
}
return query.Repository.ID, nil
}