-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.go
More file actions
525 lines (454 loc) · 15.8 KB
/
github.go
File metadata and controls
525 lines (454 loc) · 15.8 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
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/rand/v2"
"net/http"
"strings"
"time"
"github.com/google/go-github/v84/github"
"golang.org/x/oauth2"
)
var ErrNoRemoteBranch = errors.New("branch does not exist on the remote")
// RepositoriesAPI defines the subset of github.RepositoriesService methods needed by this project.
type RepositoriesAPI interface {
GetBranch(ctx context.Context, owner, repo, branch string, maxRedirects int) (*github.Branch, *github.Response, error)
}
// GitAPI defines the subset of github.GitService methods needed by this project.
type GitAPI interface {
CreateRef(ctx context.Context, owner, repo string, ref github.CreateRef) (*github.Reference, *github.Response, error)
GetCommit(ctx context.Context, owner, repo, sha string) (*github.Commit, *github.Response, error)
CreateBlob(ctx context.Context, owner, repo string, blob github.Blob) (*github.Blob, *github.Response, error)
CreateTree(ctx context.Context, owner, repo, baseTree string, entries []*github.TreeEntry) (*github.Tree, *github.Response, error)
CreateCommit(ctx context.Context, owner, repo string, commit github.Commit, opts *github.CreateCommitOptions) (*github.Commit, *github.Response, error)
UpdateRef(ctx context.Context, owner, repo, ref string, updateRef github.UpdateRef) (*github.Reference, *github.Response, error)
DeleteRef(ctx context.Context, owner, repo, ref string) (*github.Response, error)
}
// GraphQLAPI abstracts the GraphQL endpoint for testing.
type GraphQLAPI interface {
Do(ctx context.Context, query string, variables map[string]any) (json.RawMessage, error)
}
// graphQLClient is the production implementation of GraphQLAPI using raw HTTP.
type graphQLClient struct {
httpClient *http.Client
endpoint string
}
func (g *graphQLClient) Do(ctx context.Context, query string, variables map[string]any) (json.RawMessage, error) {
body := map[string]any{
"query": query,
"variables": variables,
}
buf, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("marshal graphql request: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, g.endpoint, bytes.NewReader(buf))
if err != nil {
return nil, fmt.Errorf("create graphql request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := g.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("graphql request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("graphql request: http %d", resp.StatusCode)
}
var result struct {
Data json.RawMessage `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("decode graphql response: %w", err)
}
if len(result.Errors) > 0 {
msgs := make([]string, len(result.Errors))
for i, e := range result.Errors {
msgs[i] = e.Message
}
return nil, fmt.Errorf("graphql errors: %s", strings.Join(msgs, "; "))
}
return result.Data, nil
}
// Client provides methods for interacting with a remote repository on GitHub
type Client struct {
repos RepositoriesAPI
git GitAPI
graphql GraphQLAPI
owner string
repo string
branch string
userToken bool
dryrun bool
force bool
signAttempts int
}
// NewClient returns a Client configured to make GitHub requests for branch owned by owner/repo on
// GitHub using the oauth token in token.
func NewClient(ctx context.Context, token, owner, repo, branch string) *Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpC := oauth2.NewClient(ctx, ts)
ghClient := github.NewClient(httpC)
return &Client{
repos: ghClient.Repositories,
git: ghClient.Git,
graphql: &graphQLClient{
httpClient: httpC,
endpoint: "https://api.github.com/graphql",
},
owner: owner,
repo: repo,
branch: branch,
userToken: isUserToken(token),
}
}
func (c *Client) commitURL(hash string) string {
return fmt.Sprintf("https://github.com/%s/%s/commit/%s", c.owner, c.repo, hash)
}
func (c *Client) compareURL(base, head string) string {
return fmt.Sprintf("https://github.com/%s/%s/compare/%s...%s", c.owner, c.repo, base, head)
}
// GetHeadCommitHash returns the current head commit hash for the configured repository and branch
func (c *Client) GetHeadCommitHash(ctx context.Context) (string, error) {
branch, resp, err := c.repos.GetBranch(ctx, c.owner, c.repo, c.branch, 0)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return "", fmt.Errorf("get branch %q: %w", c.branch, ErrNoRemoteBranch)
}
return "", fmt.Errorf("get commit hash: %w", err)
}
return branch.GetCommit().GetSHA(), nil
}
// CreateBranch attempts to create c.branch using headSha as the branch point
func (c *Client) CreateBranch(ctx context.Context, headSha string) (string, error) {
logger.Printf("Creating branch from commit %s\n", headSha)
ref := github.CreateRef{
Ref: fmt.Sprintf("refs/heads/%s", c.branch),
SHA: headSha,
}
created, resp, err := c.git.CreateRef(ctx, c.owner, c.repo, ref)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusUnprocessableEntity {
return "", fmt.Errorf("create branch: http 422 (does the branch point exist?)")
}
return "", fmt.Errorf("create branch: %w", err)
}
return created.GetObject().GetSHA(), nil
}
// commitStrategy indicates which API to use for creating a commit.
type commitStrategy int
const (
strategyGraphQL commitStrategy = iota
strategyREST
)
func (s commitStrategy) String() string {
if s == strategyGraphQL {
return "GraphQL"
}
return "REST"
}
// chooseStrategy decides whether to use GraphQL or REST for a given change.
func (c *Client) chooseStrategy(change Change) commitStrategy {
if change.HasNonDefaultModes() && !c.userToken {
return strategyREST
}
return strategyGraphQL
}
// PushChanges creates commits for each change on a throwaway branch, then updates the real branch
// ref once at the end. The throwaway branch is always cleaned up on completion.
// It returns the number of changes that were successfully pushed, the new head reference hash, and
// any error encountered.
func (c *Client) PushChanges(ctx context.Context, headCommit string, changes ...Change) (int, string, error) {
if c.dryrun {
for i, change := range changes {
_, _, err := c.CreateChange(ctx, "", headCommit, change)
if err != nil {
return i + 1, "", fmt.Errorf("push change %d: %w", i, err)
}
}
return len(changes), strings.Repeat("0", 40), nil
}
// Create throwaway branch
tmpBranch := fmt.Sprintf("%s--headless-tmp-%s", c.branch, randomSuffix())
tmpRef := fmt.Sprintf("refs/heads/%s", tmpBranch)
logger.Printf("Creating working branch %s\n", tmpBranch)
_, _, err := c.git.CreateRef(ctx, c.owner, c.repo, github.CreateRef{
Ref: tmpRef,
SHA: headCommit,
})
if err != nil {
return 0, "", fmt.Errorf("create working branch: %w", err)
}
// Always clean up the throwaway branch
defer func() {
logger.Printf("Cleaning up working branch %s\n", tmpBranch)
if _, err := c.git.DeleteRef(ctx, c.owner, c.repo, tmpRef); err != nil {
logger.Warningf("Failed to delete working branch %s: %v", tmpBranch, err)
}
}()
// Create commits on the throwaway branch
for i, change := range changes {
newHead, strategy, err := c.CreateChange(ctx, tmpBranch, headCommit, change)
if err != nil {
return i + 1, "", fmt.Errorf("push change %d: %w", i, err)
}
if strategy == strategyREST {
// REST creates a detached commit; advance the throwaway branch to stay in sync
_, _, err = c.git.UpdateRef(ctx, c.owner, c.repo, tmpRef, github.UpdateRef{
SHA: newHead,
Force: github.Ptr(true),
})
if err != nil {
return i + 1, "", fmt.Errorf("advance working branch: %w", err)
}
}
headCommit = newHead
}
// Update the real branch
_, _, err = c.git.UpdateRef(ctx, c.owner, c.repo, "refs/heads/"+c.branch, github.UpdateRef{
SHA: headCommit,
Force: github.Ptr(c.force),
})
if err != nil {
return len(changes), "", fmt.Errorf("update ref: %w", err)
}
return len(changes), headCommit, nil
}
// CreateChange creates a single commit from a change, dispatching to GraphQL or REST based on
// the change contents and token type. It handles logging, dry-run, and signature verification
// retry for both strategies.
//
// tmpBranch is the name of the throwaway branch (without refs/heads/ prefix) used for GraphQL
// commits. It may be empty for dry-run mode.
//
// Returns the new commit SHA, the strategy used, and any error.
func (c *Client) CreateChange(ctx context.Context, tmpBranch, headCommit string, change Change) (string, commitStrategy, error) {
shortHash := change.hash
if len(shortHash) > 8 {
shortHash = shortHash[:8]
}
endGroup := logger.Group(fmt.Sprintf("Commit %s: %s", shortHash, change.Headline()))
defer endGroup()
// Log commit details
if change.author != "" {
logger.Printf("Author: %s\n", change.author)
}
if body := change.Body(); body != "" {
logger.Printf("Body: %s\n", body)
}
logger.Printf("Changed files: %d\n", len(change.entries))
for path, fe := range change.entries {
action := "MODIFY"
if fe.Content == nil {
action = "DELETE"
}
logger.Printf(" - %s: %s\n", action, path)
}
strategy := c.chooseStrategy(change)
logger.Printf("Strategy: %s\n", strategy)
if strategy == strategyGraphQL && change.HasNonDefaultModes() {
logger.Warningf("GraphQL API does not preserve file modes; non-default modes in this commit will be reset to 100644")
}
if c.dryrun {
logger.Notice("Dry run enabled, not writing commit")
return strings.Repeat("0", len(change.hash)), strategy, nil
}
// Build the commit and reset functions based on strategy.
// resetFn rewinds the throwaway branch before a GraphQL retry so expectedHeadOid matches again.
var commitFn func() (string, bool, error)
var resetFn func() error
switch strategy {
case strategyGraphQL:
tmpRef := fmt.Sprintf("refs/heads/%s", tmpBranch)
commitFn = func() (string, bool, error) {
return c.execGraphQLCommit(ctx, tmpBranch, headCommit, change)
}
resetFn = func() error {
_, _, err := c.git.UpdateRef(ctx, c.owner, c.repo, tmpRef, github.UpdateRef{
SHA: headCommit,
Force: github.Ptr(true),
})
if err != nil {
return fmt.Errorf("reset working branch for retry: %w", err)
}
return nil
}
case strategyREST:
// Prep tree once before the retry loop
treeSHA, err := c.prepTree(ctx, headCommit, change)
if err != nil {
return "", strategy, err
}
commitFn = func() (string, bool, error) {
return c.execRESTCommit(ctx, headCommit, treeSHA, change)
}
resetFn = func() error { return nil }
}
// Execute with signature verification retry
commitSha, verified, err := commitFn()
if err != nil {
return "", strategy, fmt.Errorf("create commit: %w", err)
}
if c.signAttempts > 0 {
backoff := 1 * time.Second
for attempt := 1; attempt <= c.signAttempts && !verified; attempt++ {
if attempt == c.signAttempts {
return "", strategy, fmt.Errorf("commit %s was not signed after %d attempt(s)", commitSha, c.signAttempts)
}
logger.Warningf("Commit %s not signed (attempt %d/%d), retrying in %s...", commitSha, attempt, c.signAttempts, backoff)
time.Sleep(backoff)
backoff *= 2
if err := resetFn(); err != nil {
return "", strategy, err
}
commitSha, verified, err = commitFn()
if err != nil {
return "", strategy, fmt.Errorf("create commit (attempt %d): %w", attempt+1, err)
}
}
}
logger.Printf("Created: %s\n", c.commitURL(commitSha))
return commitSha, strategy, nil
}
// prepTree creates blobs and a tree for the REST commit path. This is called once before the
// retry loop since the tree is immutable and can be reused across retries.
func (c *Client) prepTree(ctx context.Context, headCommit string, change Change) (string, error) {
parentCommit, _, err := c.git.GetCommit(ctx, c.owner, c.repo, headCommit)
if err != nil {
return "", fmt.Errorf("get parent commit: %w", err)
}
baseTreeSHA := parentCommit.GetTree().GetSHA()
var entries []*github.TreeEntry
for path, fe := range change.entries {
mode := fe.Mode
if mode == "" {
mode = "100644"
}
entry := &github.TreeEntry{
Path: github.Ptr(path),
Mode: github.Ptr(mode),
Type: github.Ptr("blob"),
}
if fe.Content == nil {
// Deletion: SHA must be empty string for go-github to omit it
} else {
blob, _, err := c.git.CreateBlob(ctx, c.owner, c.repo, github.Blob{
Content: github.Ptr(base64.StdEncoding.EncodeToString(fe.Content)),
Encoding: github.Ptr("base64"),
})
if err != nil {
return "", fmt.Errorf("create blob for %s: %w", path, err)
}
entry.SHA = blob.SHA
}
entries = append(entries, entry)
}
tree, _, err := c.git.CreateTree(ctx, c.owner, c.repo, baseTreeSHA, entries)
if err != nil {
return "", fmt.Errorf("create tree: %w", err)
}
return tree.GetSHA(), nil
}
// execRESTCommit creates a commit using the REST API with a pre-built tree.
func (c *Client) execRESTCommit(ctx context.Context, headCommit, treeSHA string, change Change) (string, bool, error) {
message := change.Headline()
if body := change.Body(); body != "" {
message = message + "\n\n" + body
}
commitInput := github.Commit{
Message: github.Ptr(message),
Tree: &github.Tree{SHA: github.Ptr(treeSHA)},
Parents: []*github.Commit{{SHA: github.Ptr(headCommit)}},
}
commit, _, err := c.git.CreateCommit(ctx, c.owner, c.repo, commitInput, nil)
if err != nil {
return "", false, fmt.Errorf("REST create commit: %w", err)
}
return commit.GetSHA(), commit.GetVerification().GetVerified(), nil
}
// createCommitOnBranch GraphQL mutation
const createCommitOnBranchMutation = `
mutation CreateCommitOnBranch($input: CreateCommitOnBranchInput!) {
createCommitOnBranch(input: $input) {
commit {
oid
signature {
isValid
}
}
}
}
`
// execGraphQLCommit creates a commit using the GraphQL createCommitOnBranch mutation.
func (c *Client) execGraphQLCommit(ctx context.Context, tmpBranch, headCommit string, change Change) (string, bool, error) {
// Build file additions and deletions
var additions []map[string]string
var deletions []map[string]string
for path, fe := range change.entries {
if fe.Content == nil {
deletions = append(deletions, map[string]string{"path": path})
} else {
additions = append(additions, map[string]string{
"path": path,
"contents": base64.StdEncoding.EncodeToString(fe.Content),
})
}
}
headline := change.Headline()
body := change.Body()
message := map[string]string{"headline": headline}
if body != "" {
message["body"] = body
}
fileChanges := map[string]any{}
if len(additions) > 0 {
fileChanges["additions"] = additions
}
if len(deletions) > 0 {
fileChanges["deletions"] = deletions
}
input := map[string]any{
"branch": map[string]any{
"repositoryNameWithOwner": fmt.Sprintf("%s/%s", c.owner, c.repo),
"branchName": tmpBranch,
},
"expectedHeadOid": headCommit,
"message": message,
"fileChanges": fileChanges,
}
data, err := c.graphql.Do(ctx, createCommitOnBranchMutation, map[string]any{"input": input})
if err != nil {
return "", false, fmt.Errorf("GraphQL createCommitOnBranch: %w", err)
}
var result struct {
CreateCommitOnBranch struct {
Commit struct {
OID string `json:"oid"`
Signature struct {
IsValid bool `json:"isValid"`
} `json:"signature"`
} `json:"commit"`
} `json:"createCommitOnBranch"`
}
if err := json.Unmarshal(data, &result); err != nil {
return "", false, fmt.Errorf("unmarshal GraphQL response: %w", err)
}
oid := result.CreateCommitOnBranch.Commit.OID
signed := result.CreateCommitOnBranch.Commit.Signature.IsValid
return oid, signed, nil
}
func randomSuffix() string {
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
b := make([]byte, 8)
for i := range b {
b[i] = chars[rand.IntN(len(chars))]
}
return string(b)
}