Repository navigation
Expand file tree
/
Copy pathclient.go
More file actions
265 lines (237 loc) · 9.06 KB
/
Copy pathclient.go
File metadata and controls
265 lines (237 loc) · 9.06 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
// Copyright (c) 2026 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/uber/submitqueue/platform/githubapi"
phttp "github.com/uber/submitqueue/platform/http"
)
// pullStateOpen is the REST API state of a pull request that is neither merged
// nor closed.
const pullStateOpen = "open"
// mergeableStateDirty is GitHub's mergeable_state for a pull request whose
// head conflicts with its base.
const mergeableStateDirty = "dirty"
// Async merge request statuses.
const (
mergeStatusPending = "pending"
mergeStatusMerged = "merged"
mergeStatusEnqueued = "enqueued"
mergeStatusFailed = "failed"
)
// mergeActionDirect merges immediately instead of entering GitHub's own merge
// queue: Runway is the queue, so a second one would reorder what it decided.
const mergeActionDirect = "direct_merge"
// pullRequest is the subset of a GitHub pull request the merger reads.
type pullRequest struct {
Number int `json:"number"`
State string `json:"state"`
Draft bool `json:"draft"`
Merged bool `json:"merged"`
Mergeable *bool `json:"mergeable"`
MergeableState string `json:"mergeable_state"`
Head gitRef `json:"head"`
Base gitRef `json:"base"`
}
// gitRef is a branch name and the commit it points at.
type gitRef struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
}
// stack is a GitHub pull request stack; PullRequests are ordered bottom to top.
type stack struct {
Number int `json:"number"`
Open bool `json:"open"`
Base gitRef `json:"base"`
PullRequests []stackPull `json:"pull_requests"`
}
// stackPull is one member of a stack.
type stackPull struct {
Number int `json:"number"`
State string `json:"state"`
MergedAt *string `json:"merged_at"`
Head gitRef `json:"head"`
}
func (p stackPull) unmerged() bool {
return p.State == pullStateOpen && p.MergedAt == nil
}
// asyncMergeRequest is the body of PUT .../merge-async.
type asyncMergeRequest struct {
SHA string `json:"sha"`
MergeMethod string `json:"merge_method"`
MergeAction string `json:"merge_action"`
BypassRules bool `json:"bypass_rules,omitempty"`
}
// asyncMergeStatus is both the submit response and the poll response of the
// async merge API.
type asyncMergeStatus struct {
Status string `json:"status"`
Details struct {
Message string `json:"message"`
UUID string `json:"uuid"`
SHA string `json:"sha"`
ExpectedHeadSHA string `json:"expected_head_sha"`
MergeMethod string `json:"merge_method"`
} `json:"details"`
}
// submitOutcome classifies a merge-async submission.
type submitOutcome int
const (
// submitAccepted means a merge request is in flight and must be polled;
// that includes adopting one an earlier delivery already submitted.
submitAccepted submitOutcome = iota + 1
// submitDone means GitHub answered with a final status straight away.
submitDone
// submitRejected means GitHub refused the merge as asked (closed, draft,
// failed validation); retrying the same request cannot change that.
submitRejected
// submitConflicting means a merge request is already in flight for the pull
// request but for a different head or merge method, so it is not this one.
submitConflicting
)
// client is a thin wrapper over the GitHub REST endpoints the merger needs,
// bound to one repository. Base URL and auth come from the injected
// *http.Client's transport.
type client struct {
httpClient *http.Client
owner string
repo string
}
func (c *client) getPull(ctx context.Context, number int) (pullRequest, error) {
var pr pullRequest
if err := c.getJSON(ctx, c.repoPath("pulls", strconv.Itoa(number)), &pr); err != nil {
return pullRequest{}, fmt.Errorf("get pull request #%d: %w", number, err)
}
return pr, nil
}
// issueEvent is one entry of a pull request's issue event timeline.
type issueEvent struct {
Event string `json:"event"`
CommitID string `json:"commit_id"`
}
// issueEventsPageSize is the largest page the issue events endpoint serves.
const issueEventsPageSize = 100
// getMergeCommit returns the commit a merged pull request landed as, and false
// while GitHub has not recorded it yet. API version 2026-03-10 dropped
// merge_commit_sha from the pull request resource; the "merged" issue event is
// where that version still reports it.
func (c *client) getMergeCommit(ctx context.Context, number int) (string, bool, error) {
var mergeCommit string
for page := 1; ; page++ {
var events []issueEvent
path := fmt.Sprintf("%s?per_page=%d&page=%d", c.repoPath("issues", strconv.Itoa(number), "events"), issueEventsPageSize, page)
if err := c.getJSON(ctx, path, &events); err != nil {
return "", false, fmt.Errorf("list events of pull request #%d: %w", number, err)
}
for _, e := range events {
if e.Event == "merged" && e.CommitID != "" {
mergeCommit = e.CommitID
}
}
if len(events) < issueEventsPageSize {
return mergeCommit, mergeCommit != "", nil
}
}
}
// getStackForPull returns the stack containing the pull request, and false
// when it belongs to none.
func (c *client) getStackForPull(ctx context.Context, number int) (stack, bool, error) {
var stacks []stack
path := c.repoPath("stacks") + "?pull_request=" + strconv.Itoa(number)
if err := c.getJSON(ctx, path, &stacks); err != nil {
return stack{}, false, fmt.Errorf("list stacks for pull request #%d: %w", number, err)
}
for _, s := range stacks {
for _, p := range s.PullRequests {
if p.Number == number {
return s, true, nil
}
}
}
return stack{}, false, nil
}
// submitMerge asks GitHub to merge the pull request (and, for a stacked one,
// every unmerged pull request below it). A 409 carries the request already in
// flight; it is adopted only when it merges the same head with the same method,
// so a redelivered merge converges on its own GitHub request without taking
// over a different one.
func (c *client) submitMerge(ctx context.Context, number int, req asyncMergeRequest) (asyncMergeStatus, submitOutcome, error) {
body, err := json.Marshal(req)
if err != nil {
return asyncMergeStatus{}, 0, fmt.Errorf("marshal merge request: %w", err)
}
path := c.repoPath("pulls", strconv.Itoa(number), "merge-async")
status, respBody, err := phttp.SendRequest(ctx, c.httpClient, http.MethodPut, path, body, githubapi.SetRESTHeaders)
if err != nil {
return asyncMergeStatus{}, 0, fmt.Errorf("merge pull request #%d: %w", number, err)
}
var out asyncMergeStatus
switch status {
case http.StatusOK, http.StatusAccepted, http.StatusConflict:
if err := json.Unmarshal(respBody, &out); err != nil {
return asyncMergeStatus{}, 0, fmt.Errorf("merge pull request #%d: unmarshal response: %w", number, err)
}
case http.StatusBadRequest, http.StatusUnprocessableEntity:
_ = json.Unmarshal(respBody, &out)
return out, submitRejected, nil
default:
return asyncMergeStatus{}, 0, fmt.Errorf("merge pull request #%d: %w", number, phttp.NewStatusError(status, respBody))
}
switch {
case status == http.StatusConflict && out.Details.UUID != "":
if out.Details.ExpectedHeadSHA != req.SHA || out.Details.MergeMethod != req.MergeMethod {
return out, submitConflicting, nil
}
return out, submitAccepted, nil
case status == http.StatusConflict:
return asyncMergeStatus{}, 0, fmt.Errorf("merge pull request #%d: %w", number, phttp.NewStatusError(status, respBody))
case out.Status == mergeStatusPending && out.Details.UUID != "":
return out, submitAccepted, nil
default:
return out, submitDone, nil
}
}
func (c *client) getMergeStatus(ctx context.Context, number int, uuid string) (asyncMergeStatus, error) {
var out asyncMergeStatus
path := c.repoPath("pulls", strconv.Itoa(number), "merge-async", uuid)
if err := c.getJSON(ctx, path, &out); err != nil {
return asyncMergeStatus{}, fmt.Errorf("get merge status %s for pull request #%d: %w", uuid, number, err)
}
return out, nil
}
func (c *client) getJSON(ctx context.Context, path string, out any) error {
status, respBody, err := phttp.SendRequest(ctx, c.httpClient, http.MethodGet, path, nil, githubapi.SetRESTHeaders)
if err != nil {
return err
}
if status != http.StatusOK {
return phttp.NewStatusError(status, respBody)
}
if err := json.Unmarshal(respBody, out); err != nil {
return fmt.Errorf("unmarshal response: %w", err)
}
return nil
}
func (c *client) repoPath(segments ...string) string {
path := "/repos/" + url.PathEscape(c.owner) + "/" + url.PathEscape(c.repo)
for _, s := range segments {
path += "/" + url.PathEscape(s)
}
return path
}