-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tag.go
118 lines (109 loc) · 3.01 KB
/
tag.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
package tagpr
import (
"context"
"strings"
"github.com/google/go-github/v66/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
}
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 nil, nil
}
return pulls[0], 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()
}
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: nextTag,
PreviousTagName: previousTag,
TargetCommitish: &targetCommitish,
})
if err != nil {
showGHError(err, resp)
return err
}
if _, _, err := tp.c.Git("tag", nextTag); err != nil {
return err
}
_, _, err = tp.c.Git("push", "--tags")
if err != nil {
return err
}
tp.setOutput("tag", nextTag)
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: &nextTag,
TargetCommitish: &releaseBranch,
Name: &releases.Name,
Body: &releases.Body,
Draft: github.Bool(tp.cfg.ReleaseDraft()),
})
if err != nil {
showGHError(err, resp)
return err
}
return nil
}