/
githubRelease.go
139 lines (123 loc) · 3.22 KB
/
githubRelease.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"github.com/google/go-github/github"
)
type myRoundTripper struct {
accessToken string
}
func (rt myRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("Authorization", fmt.Sprintf("token %s", rt.accessToken))
return http.DefaultTransport.RoundTrip(r)
}
var (
client *github.Client
number string
binDir string
sha string
)
func init() {
accessToken := os.Getenv("GITHUB_ACCESS_TOKEN")
if accessToken == "" {
log.Fatal("GITHUB_ACCESS_TOKEN env required")
}
if number = os.Getenv("BUILD_NUMBER"); number == "" {
log.Fatal("BUILD_NUMBER env required")
}
if sha = os.Getenv("GIT_SHA"); sha == "" {
log.Fatal("GIT_SHA env required")
}
binDir = os.Getenv("OUTPUTDIR")
hClient := &http.Client{Transport: myRoundTripper{accessToken}}
client = github.NewClient(hClient)
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
fmt.Printf("Releasing to github %s.\n", number)
fmt.Println("Fetching latest release")
latest, _, err := client.Repositories.GetLatestRelease("bosun-monitor", "bosun")
checkError(err)
fmt.Println("Getting all prs since last release to build release notes...")
opts := &github.PullRequestListOptions{}
opts.Base = "master"
opts.Direction = "desc"
opts.State = "closed"
opts.Sort = "updated"
opts.PerPage = 100
reqs, _, err := client.PullRequests.List("bosun-monitor", "bosun", opts)
checkError(err)
// group pr titles if they are prefaced by `cmd/scollector:` or similar.
groups := make(map[string][]*github.PullRequest)
for _, pr := range reqs {
p := pr
if pr.ClosedAt.Before((*latest.CreatedAt).Time) {
continue
}
if pr.MergedAt == nil {
continue
}
titleParts := strings.SplitN(*pr.Title, ":", 2)
if len(titleParts) == 1 {
titleParts = []string{"other", titleParts[0]}
}
group := titleParts[0]
group = strings.Replace(group, "cmd/", "", -1)
*pr.Title = titleParts[1]
groups[group] = append(groups[group], &p)
}
body := ""
for key, prs := range groups {
body += fmt.Sprintf("\n### %s: ###\n", key)
for _, pr := range prs {
body += fmt.Sprintf(" - %s [#%d](%s)\n", *pr.Title, *pr.Number, *pr.HTMLURL)
}
}
fmt.Println("Creating the release...")
release := &github.RepositoryRelease{}
release.TagName = &number
release.TargetCommitish = &sha
release.Name = &number
isDraft := true
release.Draft = &isDraft
release.Body = &body
release, _, err = client.Repositories.CreateRelease("bosun-monitor", "bosun", release)
checkError(err)
fmt.Println("Uploading artifacts...")
files, err := ioutil.ReadDir(binDir)
checkError(err)
wg := sync.WaitGroup{}
for _, file := range files {
if file.IsDir() {
return
}
wg.Add(1)
filename := file.Name()
go func() {
uploadArtifact(filename, *release.ID)
wg.Done()
}()
}
wg.Wait()
fmt.Printf("Done. %s\n", *release.HTMLURL)
}
func uploadArtifact(filename string, id int) {
fmt.Println("\t", filename)
f, err := os.Open(filepath.Join(binDir, filename))
checkError(err)
defer f.Close()
opts := &github.UploadOptions{}
opts.Name = filename
_, _, err = client.Repositories.UploadReleaseAsset("bosun-monitor", "bosun", id, opts, f)
checkError(err)
}