This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit.go
136 lines (124 loc) · 3.9 KB
/
submit.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
package main
import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/google/go-github/v28/github"
"github.com/hashicorp/go-version"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"golang.org/x/oauth2"
git "gopkg.in/src-d/go-git.v4"
)
// var repoArg = ""
var accessToken = ""
// ./release-cli submit
var submitCommand *cli.Command = &cli.Command{
Name: "submit",
Usage: "To submit the pull requests to the given release branch",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "repo",
Usage: "The path where the git repository locates. /home/pegasus eg.",
Required: true,
Destination: &repoArg,
},
&cli.StringFlag{
Name: "access",
Usage: "The access token to github, see https://github.com/settings/tokens",
EnvVar: "ACCESS_TOKEN",
Required: true,
Destination: &accessToken,
},
},
Action: func(c *cli.Context) error {
var err error
var repo *git.Repository
// validate --repo
if repo, err = git.PlainOpen(repoArg); err != nil {
return fatalError("cannot open repo '%s': %s", repoArg, err)
}
latestVer := getLatestVersion(repo)
latestVerObj, err := version.NewSemver(latestVer)
if err != nil {
return fatalError("latest version is invalid to be released: %s, %s", latestVer, err)
}
if latestVerObj.Prerelease() != "" {
return fatalError("repo is still in pre-released state: %s", latestVer)
}
versions := getAllVersions(repo, nil)
sort.Sort(sort.Reverse(version.Collection(versions)))
var pastReleasedVer *version.Version
for _, v := range versions[1:] {
if v.Prerelease() == "" {
pastReleasedVer = v
break
}
}
infoLog("submitting PRs between %s and %s", pastReleasedVer, latestVer)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"PR", "Title"})
table.SetBorder(false)
table.SetColWidth(120)
var prs []int
for _, c := range getAllCommitsPickedForUpcomingRelease(repo, pastReleasedVer.String()) {
prID, err := getPrIDInt(c.title)
if err != nil {
warnLog("unable to get PR ID from commit \"%s\"", c.title)
continue
}
table.Append([]string{fmt.Sprintf("#%d", prID), c.title})
prs = append(prs, prID)
}
infoLog("submit %d commits to %s\n", len(prs), latestVer)
table.Render()
println()
origin, err := repo.Remote("origin")
fatalExitIfNotNil(err)
owner, repoName := getOwnerAndRepoFromURL(origin.Config().URLs[0])
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// find existing label for version
newLabel := latestVer[1:] // remove prefixed 'v'
ctx, cancel = context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
_, resp, err := client.Issues.GetLabel(ctx, owner, repoName, newLabel)
if err != nil {
if resp.StatusCode == 404 {
fmt.Printf("info: create github label %s\n", latestVer)
_, _, err = client.Issues.CreateLabel(ctx, owner, repoName, &github.Label{Name: &newLabel})
}
fatalExitIfNotNil(err)
}
// Add release label to the specific PR
releaseBranchRaw := getBranch(latestVer)[1:] // remove prefixed 'v'
for _, prID := range prs {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
defer cancel()
pr, _, err := client.PullRequests.Get(ctx, owner, repoName, prID)
fatalExitIfNotNil(err)
labelAdded := false
for _, label := range pr.Labels {
if strings.HasPrefix(label.GetName(), releaseBranchRaw) {
fmt.Printf("info: #%d is already labeled to %s\n", prID, newLabel)
labelAdded = true
break
}
}
if !labelAdded {
_, _, err := client.Issues.AddLabelsToIssue(ctx, owner, repoName, prID, []string{newLabel})
fatalExitIfNotNil(err)
fmt.Printf("info: add github label %s to #%d\n", latestVer, prID)
}
}
return nil
},
}