-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathgithub.go
More file actions
591 lines (522 loc) · 17.9 KB
/
Copy pathgithub.go
File metadata and controls
591 lines (522 loc) · 17.9 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
package github
import (
"bytes"
"encoding/json"
"fmt"
"math"
"net/http"
"github.com/cli/go-gh/v2/pkg/api"
graphql "github.com/cli/shurcooL-graphql"
)
// MergeQueueEntry represents a merge queue entry. When the GraphQL field
// mergeQueueEntry is null (PR not queued), the pointer will be nil.
type MergeQueueEntry struct {
ID string `graphql:"id"`
}
// AutoMergeRequest represents an auto-merge configuration on a PR.
// When the GraphQL field autoMergeRequest is null (auto-merge not enabled),
// the pointer will be nil.
type AutoMergeRequest struct {
EnabledAt string `graphql:"enabledAt"`
}
// PullRequest represents a GitHub pull request.
type PullRequest struct {
ID string `graphql:"id"`
Number int `graphql:"number"`
State string `graphql:"state"`
URL string `graphql:"url"`
Title string `graphql:"title"`
Body string `graphql:"body"`
HeadRefName string `graphql:"headRefName"`
BaseRefName string `graphql:"baseRefName"`
IsDraft bool `graphql:"isDraft"`
Merged bool `graphql:"merged"`
MergeQueueEntry *MergeQueueEntry `graphql:"mergeQueueEntry"`
AutoMergeRequest *AutoMergeRequest `graphql:"autoMergeRequest"`
}
// IsQueued reports whether the pull request is currently in a merge queue.
func (pr *PullRequest) IsQueued() bool {
return pr != nil && pr.MergeQueueEntry != nil && pr.MergeQueueEntry.ID != ""
}
// IsAutoMergeEnabled reports whether the pull request has auto-merge enabled.
func (pr *PullRequest) IsAutoMergeEnabled() bool {
return pr != nil && pr.AutoMergeRequest != nil
}
// 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 []struct {
ID string `graphql:"id"`
Number int `graphql:"number"`
URL string `graphql:"url"`
Title string `graphql:"title"`
Body string `graphql:"body"`
BaseRefName string `graphql:"baseRefName"`
IsDraft bool `graphql:"isDraft"`
MergeQueueEntry *MergeQueueEntry `graphql:"mergeQueueEntry"`
AutoMergeRequest *AutoMergeRequest `graphql:"autoMergeRequest"`
}
} `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,
URL: n.URL,
Title: n.Title,
Body: n.Body,
BaseRefName: n.BaseRefName,
IsDraft: n.IsDraft,
MergeQueueEntry: n.MergeQueueEntry,
AutoMergeRequest: n.AutoMergeRequest,
}, 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
URL string `graphql:"url"`
}
} `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,
URL: pr.URL,
}, nil
}
// UpdatePRBase updates the base branch of an existing pull request.
func (c *Client) UpdatePRBase(number int, base string) error {
type updatePRRequest struct {
Base string `json:"base"`
}
body, err := json.Marshal(updatePRRequest{Base: base})
if err != nil {
return fmt.Errorf("marshaling request: %w", err)
}
path := fmt.Sprintf("repos/%s/%s/pulls/%d", c.owner, c.repo, number)
return c.rest.Patch(path, bytes.NewReader(body), nil)
}
// MarkPRReadyForReview converts a draft pull request to ready for review.
func (c *Client) MarkPRReadyForReview(prID string) error {
var mutation struct {
MarkPullRequestReadyForReview struct {
PullRequest struct {
ID string
}
} `graphql:"markPullRequestReadyForReview(input: $input)"`
}
type MarkPullRequestReadyForReviewInput struct {
PullRequestID string `json:"pullRequestId"`
}
variables := map[string]interface{}{
"input": MarkPullRequestReadyForReviewInput{
PullRequestID: prID,
},
}
if err := c.gql.Mutate("MarkPullRequestReadyForReview", &mutation, variables); err != nil {
return fmt.Errorf("marking PR ready for review: %w", err)
}
return nil
}
// DisableAutoMerge disables auto-merge on a pull request.
func (c *Client) DisableAutoMerge(prID string) error {
var mutation struct {
DisablePullRequestAutoMerge struct {
PullRequest struct {
ID string
}
} `graphql:"disablePullRequestAutoMerge(input: $input)"`
}
type DisablePullRequestAutoMergeInput struct {
PullRequestID string `json:"pullRequestId"`
}
variables := map[string]interface{}{
"input": DisablePullRequestAutoMergeInput{
PullRequestID: prID,
},
}
if err := c.gql.Mutate("DisablePullRequestAutoMerge", &mutation, variables); err != nil {
return fmt.Errorf("disabling auto-merge: %w", err)
}
return nil
}
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
}
// PRDetails holds enriched pull request data for display in the TUI.
type PRDetails struct {
Number int
State string // OPEN, CLOSED, MERGED
URL string
Title string
Body string
IsDraft bool
Merged bool
IsQueued bool
}
// 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 {
Number int `graphql:"number"`
State string `graphql:"state"`
URL string `graphql:"url"`
IsDraft bool `graphql:"isDraft"`
Merged bool `graphql:"merged"`
MergeQueueEntry *MergeQueueEntry `graphql:"mergeQueueEntry"`
}
} `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,
State: n.State,
URL: n.URL,
IsDraft: n.IsDraft,
Merged: n.Merged,
IsQueued: n.MergeQueueEntry != nil && n.MergeQueueEntry.ID != "",
}, nil
}
// FindPRByNumber fetches a pull request by its number.
func (c *Client) FindPRByNumber(number int) (*PullRequest, error) {
gqlNumber, err := toGraphQLInt(number)
if err != nil {
return nil, err
}
var query struct {
Repository struct {
PullRequest struct {
ID string `graphql:"id"`
Number int `graphql:"number"`
State string `graphql:"state"`
URL string `graphql:"url"`
Title string `graphql:"title"`
Body string `graphql:"body"`
HeadRefName string `graphql:"headRefName"`
BaseRefName string `graphql:"baseRefName"`
IsDraft bool `graphql:"isDraft"`
Merged bool `graphql:"merged"`
MergeQueueEntry *MergeQueueEntry `graphql:"mergeQueueEntry"`
AutoMergeRequest *AutoMergeRequest `graphql:"autoMergeRequest"`
} `graphql:"pullRequest(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": graphql.String(c.owner),
"name": graphql.String(c.repo),
"number": gqlNumber,
}
if err := c.gql.Query("FindPRByNumber", &query, variables); err != nil {
return nil, fmt.Errorf("querying PR #%d: %w", number, err)
}
n := query.Repository.PullRequest
if n.Number == 0 && n.ID == "" {
return nil, nil
}
return &PullRequest{
ID: n.ID,
Number: n.Number,
State: n.State,
URL: n.URL,
Title: n.Title,
Body: n.Body,
HeadRefName: n.HeadRefName,
BaseRefName: n.BaseRefName,
IsDraft: n.IsDraft,
Merged: n.Merged,
MergeQueueEntry: n.MergeQueueEntry,
AutoMergeRequest: n.AutoMergeRequest,
}, nil
}
func toGraphQLInt(n int) (graphql.Int, error) {
if n < math.MinInt32 || n > math.MaxInt32 {
return 0, fmt.Errorf("number %d is out of GraphQL Int range", n)
}
return graphql.Int(n), nil
}
// RemoteStackBase describes the base ref (and optionally SHA) of a stack.
type RemoteStackBase struct {
Ref string `json:"ref"`
Sha string `json:"sha,omitempty"`
}
// RemoteStackPRHead describes the head ref of a pull request in a stack.
type RemoteStackPRHead struct {
Ref string `json:"ref"`
Sha string `json:"sha"`
}
// RemoteStackPR is a pull request entry within a remote stack, as returned by
// the Stacks REST API list/detail endpoints.
type RemoteStackPR struct {
Number int `json:"number"`
State string `json:"state"` // open, closed
Draft bool `json:"draft"`
MergedAt *string `json:"merged_at"`
Head RemoteStackPRHead `json:"head"`
}
// IsMerged reports whether the pull request has been merged.
func (p RemoteStackPR) IsMerged() bool {
return p.MergedAt != nil && *p.MergedAt != ""
}
// RemoteStack represents a stack of pull requests as returned by the public
// Stacks REST API (GET/POST /repos/{owner}/{repo}/stacks...). ID is the
// internal identifier; Number is the human-facing stack number shown in the
// github.com UI and used to address the stack in API paths.
//
// The API returns pull_requests as an array of objects; UnmarshalJSON flattens
// them to the ordered PullRequests numbers (bottom to top) and preserves the
// full entries in PRDetails for callers that need head refs or PR state.
type RemoteStack struct {
ID int `json:"id"`
Number int `json:"number"`
NodeID string `json:"node_id"`
URL string `json:"url"`
Base RemoteStackBase `json:"base"`
Open bool `json:"open"`
CreatedAt string `json:"created_at"`
PullRequests []int `json:"-"`
PRDetails []RemoteStackPR `json:"-"`
}
// UnmarshalJSON decodes the Stacks REST API representation, deriving the
// ordered PullRequests numbers from the pull_requests objects.
func (s *RemoteStack) UnmarshalJSON(data []byte) error {
type wire struct {
ID int `json:"id"`
Number int `json:"number"`
NodeID string `json:"node_id"`
URL string `json:"url"`
Base RemoteStackBase `json:"base"`
Open bool `json:"open"`
CreatedAt string `json:"created_at"`
PullRequests []RemoteStackPR `json:"pull_requests"`
}
var w wire
if err := json.Unmarshal(data, &w); err != nil {
return err
}
s.ID = w.ID
s.Number = w.Number
s.NodeID = w.NodeID
s.URL = w.URL
s.Base = w.Base
s.Open = w.Open
s.CreatedAt = w.CreatedAt
s.PRDetails = w.PullRequests
s.PullRequests = make([]int, len(w.PullRequests))
for i, p := range w.PullRequests {
s.PullRequests[i] = p.Number
}
return nil
}
// PRNumbers returns the ordered pull request numbers in the stack, from bottom
// to top.
func (s *RemoteStack) PRNumbers() []int {
return s.PullRequests
}
// ListStacks returns all stacks in the repository, ordered by stack number
// (descending). Returns an empty slice if no stacks exist. A 404 response
// indicates stacked PRs are not enabled for this repository.
func (c *Client) ListStacks() ([]RemoteStack, error) {
path := fmt.Sprintf("repos/%s/%s/stacks", c.owner, c.repo)
var stacks []RemoteStack
if err := c.rest.Get(path, &stacks); err != nil {
return nil, err
}
if stacks == nil {
stacks = []RemoteStack{}
}
return stacks, nil
}
// FindStackForPR returns the stack that contains the given pull request number,
// using the list endpoint's server-side pull_request filter. Returns nil
// (without error) when the PR is not part of any stack.
func (c *Client) FindStackForPR(prNumber int) (*RemoteStack, error) {
path := fmt.Sprintf("repos/%s/%s/stacks?pull_request=%d", c.owner, c.repo, prNumber)
var stacks []RemoteStack
if err := c.rest.Get(path, &stacks); err != nil {
return nil, err
}
if len(stacks) == 0 {
return nil, nil
}
return &stacks[0], nil
}
// GetStack fetches a single stack by its stack number.
func (c *Client) GetStack(stackNumber int) (*RemoteStack, error) {
path := fmt.Sprintf("repos/%s/%s/stacks/%d", c.owner, c.repo, stackNumber)
var rs RemoteStack
if err := c.rest.Get(path, &rs); err != nil {
return nil, err
}
return &rs, nil
}
// CreateStack creates a stack on GitHub from an ordered list of PR numbers.
// The PR numbers must be ordered from bottom to top of the stack (at least two)
// and must form a valid base-to-head chain. Returns the created stack.
func (c *Client) CreateStack(prNumbers []int) (*RemoteStack, error) {
type createStackRequest struct {
PullRequests []int `json:"pull_requests"`
}
body, err := json.Marshal(createStackRequest{PullRequests: prNumbers})
if err != nil {
return nil, fmt.Errorf("marshaling request: %w", err)
}
path := fmt.Sprintf("repos/%s/%s/stacks", c.owner, c.repo)
var rs RemoteStack
if err := c.rest.Post(path, bytes.NewReader(body), &rs); err != nil {
return nil, err
}
return &rs, nil
}
// AddToStack appends pull requests to the top of an existing stack. Only the
// new PR numbers (the delta) should be provided, ordered from the current top
// of the stack upward. Returns the updated stack.
func (c *Client) AddToStack(stackNumber int, prNumbers []int) (*RemoteStack, error) {
type addToStackRequest struct {
PullRequests []int `json:"pull_requests"`
}
body, err := json.Marshal(addToStackRequest{PullRequests: prNumbers})
if err != nil {
return nil, fmt.Errorf("marshaling request: %w", err)
}
path := fmt.Sprintf("repos/%s/%s/stacks/%d/add", c.owner, c.repo, stackNumber)
var rs RemoteStack
if err := c.rest.Post(path, bytes.NewReader(body), &rs); err != nil {
return nil, err
}
return &rs, nil
}
// Unstack removes unlocked pull requests from a stack. The server leaves PRs
// that cannot be unstacked (queued for merge or with auto-merge enabled) in
// place. When PRs remain, the updated stack is returned with dissolved=false;
// when none remain the stack is destroyed and dissolved=true (HTTP 204).
func (c *Client) Unstack(stackNumber int) (rs *RemoteStack, dissolved bool, err error) {
path := fmt.Sprintf("repos/%s/%s/stacks/%d/unstack", c.owner, c.repo, stackNumber)
resp, err := c.rest.Request(http.MethodPost, path, nil)
if err != nil {
return nil, false, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode == http.StatusNoContent {
return nil, true, nil
}
var remaining RemoteStack
if decErr := json.NewDecoder(resp.Body).Decode(&remaining); decErr != nil {
return nil, false, fmt.Errorf("decoding unstack response: %w", decErr)
}
return &remaining, false, nil
}