-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtag.go
More file actions
140 lines (128 loc) · 3.83 KB
/
tag.go
File metadata and controls
140 lines (128 loc) · 3.83 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
package tagpr
import (
"context"
"log"
"strings"
"time"
"github.com/google/go-github/v83/github"
)
func (tp *tagpr) latestPullRequest(ctx context.Context) (*github.PullRequest, error) {
// tag and exit if the HEAD is the merged tagpr
commitish, _, err := tp.c.Git("rev-parse", "HEAD")
if err != nil {
return nil, err
}
// Retry because GitHub's internal commit-to-PR index may not be updated
// immediately after a merge, causing the API to return an empty list.
// This is especially common with squash merges but can also happen with
// regular merge commits when the workflow triggers within seconds.
// See https://github.com/Songmu/tagpr/issues/330
const maxRetries = 3
const retryInterval = 2 * time.Second
for i := range maxRetries {
pulls, resp, err := tp.gh.PullRequests.ListPullRequestsWithCommit(
ctx, tp.owner, tp.repo, commitish, nil)
if err != nil {
showGHError(err, resp)
return nil, err
}
if len(pulls) > 0 {
return pulls[0], nil
}
if i < maxRetries-1 {
log.Printf("ListPullRequestsWithCommit returned empty for %s, retrying in %s (%d/%d)",
commitish, retryInterval, i+1, maxRetries)
time.Sleep(retryInterval)
}
}
return nil, nil
}
func (tp *tagpr) tagRelease(ctx context.Context, pr *github.PullRequest, currVer *semv, latestSemverTag string) error {
var (
vfile string
err error
)
releaseBranch := tp.cfg.ReleaseBranch()
// Using "HEAD~" to retrieve the one previous commit before merging does not work well in cases
// "Rebase and merge" was used. However, we don't care about "Rebase and merge" and only support
// "Create a merge commit" and "Squash and merge."
if tp.cfg.VersionFile() == "" {
if _, _, err := tp.c.Git("checkout", "HEAD~"); err != nil {
return err
}
vfile, err = detectVersionFile(".", currVer)
if err != nil {
return err
}
if _, _, err := tp.c.Git("checkout", releaseBranch); err != nil {
return err
}
} else if tp.cfg.VersionFile() != "-" {
vfiles := strings.Split(tp.cfg.VersionFile(), ",")
vfile = strings.TrimSpace(vfiles[0])
}
var nextTag string
if vfile != "" {
nextVer, err := retrieveVersionFromFile(vfile, currVer.vPrefix)
if err != nil {
return err
}
nextTag = nextVer.Tag()
} else {
var labels []string
for _, l := range pr.Labels {
labels = append(labels, l.GetName())
}
nextTag = currVer.GuessNext(labels).Tag()
}
// Add prefix for monorepo support
fullNextTag := fullTag(tp.normalizedTagPrefix, nextTag)
previousTag := &latestSemverTag
if *previousTag == "" {
previousTag = nil
}
// To avoid putting pull requests created by tagpr itself in the release notes,
// we generate release notes in advance.
// Get the previous commitish to avoid picking up the merge of the pull
// request made by tagpr.
targetCommitish, _, err := tp.c.Git("rev-parse", "HEAD~")
if err != nil {
return nil
}
releases, resp, err := tp.gh.Repositories.GenerateReleaseNotes(
ctx, tp.owner, tp.repo, &github.GenerateNotesOptions{
TagName: fullNextTag,
PreviousTagName: previousTag,
TargetCommitish: &targetCommitish,
ConfigurationFilePath: github.Ptr(tp.cfg.ReleaseYAMLPath()),
})
if err != nil {
showGHError(err, resp)
return err
}
if _, _, err := tp.c.Git("tag", fullNextTag); err != nil {
return err
}
_, _, err = tp.c.Git("push", "--tags")
if err != nil {
return err
}
tp.setOutput("tag", fullNextTag)
if !tp.cfg.Release() {
return nil
}
// Don't use GenerateReleaseNote flag and use pre generated one
_, resp, err = tp.gh.Repositories.CreateRelease(
ctx, tp.owner, tp.repo, &github.RepositoryRelease{
TagName: &fullNextTag,
TargetCommitish: &releaseBranch,
Name: &releases.Name,
Body: &releases.Body,
Draft: github.Ptr(tp.cfg.ReleaseDraft()),
})
if err != nil {
showGHError(err, resp)
return err
}
return nil
}