/
github.go
697 lines (628 loc) · 18 KB
/
github.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
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package client
import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"net/url"
"os"
"reflect"
"strconv"
"strings"
"time"
"github.com/caarlos0/log"
"github.com/charmbracelet/x/exp/ordered"
"github.com/google/go-github/v61/github"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/tmpl"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"golang.org/x/oauth2"
)
const DefaultGitHubDownloadURL = "https://github.com"
var (
_ Client = &githubClient{}
_ ReleaseNotesGenerator = &githubClient{}
_ PullRequestOpener = &githubClient{}
_ ForkSyncer = &githubClient{}
)
type githubClient struct {
client *github.Client
}
// NewGitHubReleaseNotesGenerator returns a GitHub client that can generate
// changelogs.
func NewGitHubReleaseNotesGenerator(ctx *context.Context, token string) (ReleaseNotesGenerator, error) {
return newGitHub(ctx, token)
}
// newGitHub returns a github client implementation.
func newGitHub(ctx *context.Context, token string) (*githubClient, error) {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
httpClient := oauth2.NewClient(ctx, ts)
base := httpClient.Transport.(*oauth2.Transport).Base
if base == nil || reflect.ValueOf(base).IsNil() {
base = http.DefaultTransport
}
// nolint: gosec
base.(*http.Transport).TLSClientConfig = &tls.Config{
InsecureSkipVerify: ctx.Config.GitHubURLs.SkipTLSVerify,
}
base.(*http.Transport).Proxy = http.ProxyFromEnvironment
httpClient.Transport.(*oauth2.Transport).Base = base
client := github.NewClient(httpClient)
err := overrideGitHubClientAPI(ctx, client)
if err != nil {
return &githubClient{}, err
}
return &githubClient{client: client}, nil
}
func (c *githubClient) checkRateLimit(ctx *context.Context) {
limits, _, err := c.client.RateLimit.Get(ctx)
if err != nil {
log.Warn("could not check rate limits, hoping for the best...")
return
}
if limits.Core.Remaining > 100 { // 100 should be safe enough
return
}
sleep := limits.Core.Reset.UTC().Sub(time.Now().UTC())
if sleep <= 0 {
// it seems that sometimes, after the rate limit just reset, it might
// still get <100 remaining and a reset time in the past... in such
// cases we can probably sleep a bit more before trying again...
sleep = 15 * time.Second
}
log.Warnf("token too close to rate limiting, will sleep for %s before continuing...", sleep)
time.Sleep(sleep)
c.checkRateLimit(ctx)
}
func (c *githubClient) GenerateReleaseNotes(ctx *context.Context, repo Repo, prev, current string) (string, error) {
c.checkRateLimit(ctx)
notes, _, err := c.client.Repositories.GenerateReleaseNotes(ctx, repo.Owner, repo.Name, &github.GenerateNotesOptions{
TagName: current,
PreviousTagName: github.String(prev),
})
if err != nil {
return "", err
}
return notes.Body, err
}
func (c *githubClient) Changelog(ctx *context.Context, repo Repo, prev, current string) (string, error) {
c.checkRateLimit(ctx)
var log []string
opts := &github.ListOptions{PerPage: 100}
for {
result, resp, err := c.client.Repositories.CompareCommits(ctx, repo.Owner, repo.Name, prev, current, opts)
if err != nil {
return "", err
}
for _, commit := range result.Commits {
log = append(log, fmt.Sprintf(
"%s: %s (@%s)",
commit.GetSHA(),
strings.Split(commit.Commit.GetMessage(), "\n")[0],
commit.GetAuthor().GetLogin(),
))
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return strings.Join(log, "\n"), nil
}
// getDefaultBranch returns the default branch of a github repo
func (c *githubClient) getDefaultBranch(ctx *context.Context, repo Repo) (string, error) {
c.checkRateLimit(ctx)
p, res, err := c.client.Repositories.Get(ctx, repo.Owner, repo.Name)
if err != nil {
log := log.WithField("projectID", repo.String())
if res != nil {
log = log.WithField("statusCode", res.StatusCode)
}
log.
WithError(err).
Warn("error checking for default branch")
return "", err
}
return p.GetDefaultBranch(), nil
}
// CloseMilestone closes a given milestone.
func (c *githubClient) CloseMilestone(ctx *context.Context, repo Repo, title string) error {
c.checkRateLimit(ctx)
milestone, err := c.getMilestoneByTitle(ctx, repo, title)
if err != nil {
return err
}
if milestone == nil {
return ErrNoMilestoneFound{Title: title}
}
closedState := "closed"
milestone.State = &closedState
_, _, err = c.client.Issues.EditMilestone(
ctx,
repo.Owner,
repo.Name,
*milestone.Number,
milestone,
)
return err
}
func headString(base, head Repo) string {
return strings.Join([]string{
ordered.First(head.Owner, base.Owner),
ordered.First(head.Name, base.Name),
ordered.First(head.Branch, base.Branch),
}, ":")
}
func (c *githubClient) getPRTemplate(ctx *context.Context, repo Repo) (string, error) {
content, _, _, err := c.client.Repositories.GetContents(
ctx, repo.Owner, repo.Name,
".github/PULL_REQUEST_TEMPLATE.md",
&github.RepositoryContentGetOptions{
Ref: repo.Branch,
},
)
if err != nil {
return "", err
}
return content.GetContent()
}
const prFooter = "###### Automated with [GoReleaser](https://goreleaser.com)"
func (c *githubClient) OpenPullRequest(
ctx *context.Context,
base, head Repo,
title string,
draft bool,
) error {
c.checkRateLimit(ctx)
base.Owner = ordered.First(base.Owner, head.Owner)
base.Name = ordered.First(base.Name, head.Name)
if base.Branch == "" {
def, err := c.getDefaultBranch(ctx, base)
if err != nil {
return err
}
base.Branch = def
}
tpl, err := c.getPRTemplate(ctx, base)
if err != nil {
log.WithError(err).Debug("no pull request template found...")
}
if len(tpl) > 0 {
log.Info("got a pr template")
}
log := log.
WithField("base", headString(base, Repo{})).
WithField("head", headString(base, head)).
WithField("draft", draft)
log.Info("opening pull request")
pr, res, err := c.client.PullRequests.Create(
ctx,
base.Owner,
base.Name,
&github.NewPullRequest{
Title: github.String(title),
Base: github.String(base.Branch),
Head: github.String(headString(base, head)),
Body: github.String(strings.Join([]string{tpl, prFooter}, "\n")),
Draft: github.Bool(draft),
},
)
if err != nil {
if res.StatusCode == http.StatusUnprocessableEntity {
log.WithError(err).Warn("pull request validation failed")
return nil
}
return fmt.Errorf("could not create pull request: %w", err)
}
log.WithField("url", pr.GetHTMLURL()).Info("pull request created")
return nil
}
func (c *githubClient) SyncFork(ctx *context.Context, head, base Repo) error {
branch := base.Branch
if branch == "" {
def, err := c.getDefaultBranch(ctx, base)
if err != nil {
return err
}
branch = def
}
res, _, err := c.client.Repositories.MergeUpstream(
ctx,
head.Owner,
head.Name,
&github.RepoMergeUpstreamRequest{
Branch: github.String(branch),
},
)
if res != nil {
log.WithField("merge_type", res.GetMergeType()).
WithField("base_branch", res.GetBaseBranch()).
Info(res.GetMessage())
}
return err
}
func (c *githubClient) CreateFile(
ctx *context.Context,
commitAuthor config.CommitAuthor,
repo Repo,
content []byte,
path,
message string,
) error {
c.checkRateLimit(ctx)
defBranch, err := c.getDefaultBranch(ctx, repo)
if err != nil {
return fmt.Errorf("could not get default branch: %w", err)
}
branch := repo.Branch
if branch == "" {
branch = defBranch
}
options := &github.RepositoryContentFileOptions{
Committer: &github.CommitAuthor{
Name: github.String(commitAuthor.Name),
Email: github.String(commitAuthor.Email),
},
Content: content,
Message: github.String(message),
}
// Set the branch if we got it above...otherwise, just default to
// whatever the SDK does auto-magically
if branch != "" {
options.Branch = &branch
}
log.
WithField("repository", repo.String()).
WithField("branch", repo.Branch).
WithField("file", path).
Info("pushing")
if defBranch != branch && branch != "" {
_, res, err := c.client.Repositories.GetBranch(ctx, repo.Owner, repo.Name, branch, 100)
if err != nil && (res == nil || res.StatusCode != http.StatusNotFound) {
return fmt.Errorf("could not get branch %q: %w", branch, err)
}
if res.StatusCode == http.StatusNotFound {
defRef, _, err := c.client.Git.GetRef(ctx, repo.Owner, repo.Name, "refs/heads/"+defBranch)
if err != nil {
return fmt.Errorf("could not get ref %q: %w", "refs/heads/"+defBranch, err)
}
if _, _, err := c.client.Git.CreateRef(ctx, repo.Owner, repo.Name, &github.Reference{
Ref: github.String("refs/heads/" + branch),
Object: &github.GitObject{
SHA: defRef.Object.SHA,
},
}); err != nil {
rerr := new(github.ErrorResponse)
if !errors.As(err, &rerr) || rerr.Message != "Reference already exists" {
return fmt.Errorf("could not create ref %q from %q: %w", "refs/heads/"+branch, defRef.Object.GetSHA(), err)
}
}
}
}
file, _, res, err := c.client.Repositories.GetContents(
ctx,
repo.Owner,
repo.Name,
path,
&github.RepositoryContentGetOptions{
Ref: branch,
},
)
if err != nil && (res == nil || res.StatusCode != http.StatusNotFound) {
return fmt.Errorf("could not get %q: %w", path, err)
}
options.SHA = github.String(file.GetSHA())
if _, _, err := c.client.Repositories.UpdateFile(
ctx,
repo.Owner,
repo.Name,
path,
options,
); err != nil {
return fmt.Errorf("could not update %q: %w", path, err)
}
return nil
}
func (c *githubClient) CreateRelease(ctx *context.Context, body string) (string, error) {
c.checkRateLimit(ctx)
title, err := tmpl.New(ctx).Apply(ctx.Config.Release.NameTemplate)
if err != nil {
return "", err
}
if ctx.Config.Release.Draft && ctx.Config.Release.ReplaceExistingDraft {
if err := c.deleteExistingDraftRelease(ctx, title); err != nil {
return "", err
}
}
// Truncate the release notes if it's too long (github doesn't allow more than 125000 characters)
body = truncateReleaseBody(body)
data := &github.RepositoryRelease{
Name: github.String(title),
TagName: github.String(ctx.Git.CurrentTag),
Body: github.String(body),
// Always start with a draft release while uploading artifacts.
// PublishRelease will undraft it.
Draft: github.Bool(true),
Prerelease: github.Bool(ctx.PreRelease),
MakeLatest: github.String("true"),
}
if ctx.Config.Release.DiscussionCategoryName != "" {
data.DiscussionCategoryName = github.String(ctx.Config.Release.DiscussionCategoryName)
}
if target := ctx.Config.Release.TargetCommitish; target != "" {
target, err := tmpl.New(ctx).Apply(target)
if err != nil {
return "", err
}
if target != "" {
data.TargetCommitish = github.String(target)
}
}
if latest := strings.TrimSpace(ctx.Config.Release.MakeLatest); latest == "false" {
data.MakeLatest = github.String(latest)
}
release, err := c.createOrUpdateRelease(ctx, data, body)
if err != nil {
return "", fmt.Errorf("could not release: %w", err)
}
return strconv.FormatInt(release.GetID(), 10), nil
}
func (c *githubClient) PublishRelease(ctx *context.Context, releaseID string) error {
draft := ctx.Config.Release.Draft
if draft {
return nil
}
releaseIDInt, err := strconv.ParseInt(releaseID, 10, 64)
if err != nil {
return fmt.Errorf("non-numeric release ID %q: %w", releaseID, err)
}
if _, err := c.updateRelease(ctx, releaseIDInt, &github.RepositoryRelease{
Draft: github.Bool(draft),
}); err != nil {
return fmt.Errorf("could not update existing release: %w", err)
}
return nil
}
func (c *githubClient) createOrUpdateRelease(ctx *context.Context, data *github.RepositoryRelease, body string) (*github.RepositoryRelease, error) {
c.checkRateLimit(ctx)
release, _, err := c.client.Repositories.GetReleaseByTag(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
data.GetTagName(),
)
if err != nil {
release, resp, err := c.client.Repositories.CreateRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
data,
)
if err == nil {
log.WithField("name", data.GetName()).
WithField("release-id", release.GetID()).
WithField("request-id", resp.Header.Get("X-GitHub-Request-Id")).
Info("release created")
}
return release, err
}
data.Draft = release.Draft
data.Body = github.String(getReleaseNotes(release.GetBody(), body, ctx.Config.Release.ReleaseNotesMode))
return c.updateRelease(ctx, release.GetID(), data)
}
func (c *githubClient) updateRelease(ctx *context.Context, id int64, data *github.RepositoryRelease) (*github.RepositoryRelease, error) {
c.checkRateLimit(ctx)
release, resp, err := c.client.Repositories.EditRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
id,
data,
)
if err == nil {
log.WithField("name", data.GetName()).
WithField("release-id", release.GetID()).
WithField("request-id", resp.Header.Get("X-GitHub-Request-Id")).
Info("release updated")
}
return release, err
}
func (c *githubClient) ReleaseURLTemplate(ctx *context.Context) (string, error) {
downloadURL, err := tmpl.New(ctx).Apply(ctx.Config.GitHubURLs.Download)
if err != nil {
return "", fmt.Errorf("templating GitHub download URL: %w", err)
}
return fmt.Sprintf(
"%s/%s/%s/releases/download/{{ .Tag }}/{{ .ArtifactName }}",
downloadURL,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
), nil
}
func (c *githubClient) deleteReleaseArtifact(ctx *context.Context, releaseID int64, name string, page int) error {
c.checkRateLimit(ctx)
log.WithField("name", name).Info("delete pre-existing asset from the release")
assets, resp, err := c.client.Repositories.ListReleaseAssets(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
releaseID,
&github.ListOptions{
PerPage: 100,
Page: page,
},
)
if err != nil {
githubErrLogger(resp, err).
WithField("release-id", releaseID).
Warn("could not list release assets")
return err
}
for _, asset := range assets {
if asset.GetName() != name {
continue
}
resp, err := c.client.Repositories.DeleteReleaseAsset(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
asset.GetID(),
)
if err != nil {
githubErrLogger(resp, err).
WithField("release-id", releaseID).
WithField("id", asset.GetID()).
WithField("name", name).
Warn("could not delete asset")
}
return err
}
if next := resp.NextPage; next > 0 {
return c.deleteReleaseArtifact(ctx, releaseID, name, next)
}
return nil
}
func (c *githubClient) Upload(
ctx *context.Context,
releaseID string,
artifact *artifact.Artifact,
file *os.File,
) error {
c.checkRateLimit(ctx)
githubReleaseID, err := strconv.ParseInt(releaseID, 10, 64)
if err != nil {
return err
}
_, resp, err := c.client.Repositories.UploadReleaseAsset(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
githubReleaseID,
&github.UploadOptions{
Name: artifact.Name,
},
file,
)
if err != nil {
githubErrLogger(resp, err).
WithField("name", artifact.Name).
WithField("release-id", releaseID).
Warn("upload failed")
}
if err == nil {
return nil
}
// this status means the asset already exists
if resp != nil && resp.StatusCode == http.StatusUnprocessableEntity {
if !ctx.Config.Release.ReplaceExistingArtifacts {
return err
}
// if the user allowed to delete assets, we delete it, and return a
// retriable error.
if err := c.deleteReleaseArtifact(ctx, githubReleaseID, artifact.Name, 1); err != nil {
return err
}
return RetriableError{err}
}
return RetriableError{err}
}
// getMilestoneByTitle returns a milestone by title.
func (c *githubClient) getMilestoneByTitle(ctx *context.Context, repo Repo, title string) (*github.Milestone, error) {
c.checkRateLimit(ctx)
// The GitHub API/SDK does not provide lookup by title functionality currently.
opts := &github.MilestoneListOptions{
ListOptions: github.ListOptions{PerPage: 100},
}
for {
milestones, resp, err := c.client.Issues.ListMilestones(
ctx,
repo.Owner,
repo.Name,
opts,
)
if err != nil {
return nil, err
}
for _, m := range milestones {
if m != nil && m.Title != nil && *m.Title == title {
return m, nil
}
}
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
return nil, nil
}
func overrideGitHubClientAPI(ctx *context.Context, client *github.Client) error {
if ctx.Config.GitHubURLs.API == "" {
return nil
}
apiURL, err := tmpl.New(ctx).Apply(ctx.Config.GitHubURLs.API)
if err != nil {
return fmt.Errorf("templating GitHub API URL: %w", err)
}
api, err := url.Parse(apiURL)
if err != nil {
return err
}
uploadURL, err := tmpl.New(ctx).Apply(ctx.Config.GitHubURLs.Upload)
if err != nil {
return fmt.Errorf("templating GitHub upload URL: %w", err)
}
upload, err := url.Parse(uploadURL)
if err != nil {
return err
}
client.BaseURL = api
client.UploadURL = upload
return nil
}
func (c *githubClient) deleteExistingDraftRelease(ctx *context.Context, name string) error {
c.checkRateLimit(ctx)
opt := github.ListOptions{PerPage: 50}
for {
releases, resp, err := c.client.Repositories.ListReleases(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
&opt,
)
if err != nil {
return fmt.Errorf("could not delete existing drafts: %w", err)
}
for _, r := range releases {
if r.GetDraft() && r.GetName() == name {
if _, err := c.client.Repositories.DeleteRelease(
ctx,
ctx.Config.Release.GitHub.Owner,
ctx.Config.Release.GitHub.Name,
r.GetID(),
); err != nil {
return fmt.Errorf("could not delete previous draft release: %w", err)
}
log.WithField("commit", r.GetTargetCommitish()).
WithField("tag", r.GetTagName()).
WithField("name", r.GetName()).
Info("deleted previous draft release")
// in theory, there should be only 1 release matching, so we can just return
return nil
}
}
if resp.NextPage == 0 {
return nil
}
opt.Page = resp.NextPage
}
}
func githubErrLogger(resp *github.Response, err error) *log.Entry {
requestID := ""
if resp != nil {
requestID = resp.Header.Get("X-GitHub-Request-Id")
}
return log.WithField("request-id", requestID).WithError(err)
}