/
releasenotes.go
235 lines (205 loc) · 6.33 KB
/
releasenotes.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
package main
import (
"context"
"flag"
"fmt"
"io"
"net/http"
"os"
"regexp"
"strings"
"github.com/google/go-github/github"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
var releasenoterx = regexp.MustCompile(`(?s)` + "```" + `release-notes?\r\n(.*?)(\r\n)?` + "```")
func env(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
type options struct {
githubToken string
githubOrg string
githubRepo string
outputFile string
startSHA string
endSHA string
releaseVersion string
}
func (o *options) bindFlags() *flag.FlagSet {
flags := flag.NewFlagSet("release-notes", flag.ContinueOnError)
flags.StringVar(&o.githubToken, "github-token", env("GITHUB_TOKEN", ""), "GitHub access token (required).")
flags.StringVar(&o.githubOrg, "github-org", env("GITHUB_ORG", "openshift"), "Name of github organization.")
flags.StringVar(&o.githubRepo, "github-repo", env("GITHUB_REPO", "openshift-azure"), "Name of github repository.")
flags.StringVar(&o.outputFile, "output-file", env("OUTPUT_FILE", "CHANGELOG.md"), "The path to the where the release notes will be printed.")
flags.StringVar(&o.startSHA, "start-sha", env("START_SHA", ""), "The tag or commit hash to start at.")
flags.StringVar(&o.endSHA, "end-sha", env("END_SHA", "master"), "The tag or commit hash to end at.")
flags.StringVar(&o.releaseVersion, "release-version", env("RELEASE_VERSION", ""), "Which release version to tag the entries as.")
return flags
}
func (o *options) validate() error {
if o.githubToken == "" {
return fmt.Errorf("gitHub token must be set via -github-token or $GITHUB_TOKEN")
}
if o.startSHA == "" {
return fmt.Errorf("starting commit hash must be set via -start-sha or $START_SHA")
}
if o.endSHA == "" {
return fmt.Errorf("ending commit hash must be set via -end-sha or $END_SHA")
}
if o.releaseVersion == "" {
return fmt.Errorf("release version must be set via -release-version or $RELEASE_VERSION")
}
return nil
}
type note struct {
commit *github.RepositoryCommit
pullRequest *github.PullRequest
message *string
}
func (n *note) String() string {
msg := *n.message
title := strings.ToUpper(string(msg[0])) + msg[1:]
number := n.pullRequest.GetNumber()
url := n.pullRequest.GetHTMLURL()
author := n.pullRequest.GetUser().GetLogin()
authorUrl := n.pullRequest.GetUser().GetHTMLURL()
mergedAt := n.pullRequest.GetMergedAt().Format("02/01/2006")
return fmt.Sprintf("- %s ([#%d](%s), [@%s](%s), %s)\n", title, number, url, author, authorUrl, mergedAt)
}
type scraper struct {
*github.Client
log *logrus.Entry
options *options
}
func NewScraper(ctx context.Context, log *logrus.Entry, config *options) (*scraper, error) {
var cli *http.Client
cli = oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: config.githubToken}))
s := &scraper{
Client: github.NewClient(cli),
log: log,
options: config,
}
return s, nil
}
func (s *scraper) noteFromCommit(ctx context.Context, commit *github.RepositoryCommit) (*note, error) {
var prNumber int
if _, err := fmt.Sscanf(commit.Commit.GetMessage(), "Merge pull request #%d", &prNumber); err != nil {
return nil, err
}
pr, _, err := s.Client.PullRequests.Get(ctx, s.options.githubOrg, s.options.githubRepo, prNumber)
if err != nil {
return nil, err
}
noteBlocks := releasenoterx.FindStringSubmatch(pr.GetBody())
if noteBlocks != nil && !(noteBlocks[1] == "" || strings.EqualFold(noteBlocks[1], "none")) {
return ¬e{
commit: commit,
pullRequest: pr,
message: ¬eBlocks[1],
}, nil
}
return nil, fmt.Errorf("no release-notes tag found for commit %s", commit.GetSHA())
}
func (s *scraper) FetchReleaseNotes(ctx context.Context) ([]*note, error) {
var notes []*note
startCommit, _, err := s.Repositories.GetCommit(ctx, s.options.githubOrg, s.options.githubRepo, s.options.startSHA)
if err != nil {
return nil, err
}
endCommit, _, err := s.Repositories.GetCommit(ctx, s.options.githubOrg, s.options.githubRepo, s.options.endSHA)
if err != nil {
return nil, err
}
listOptions := &github.hscsec.cnmitsListOptions{
Since: startCommit.Commit.GetCommitter().GetDate(),
Until: endCommit.Commit.GetCommitter().GetDate(),
}
listOptions.PerPage = 100
for {
commits, resp, err := s.Client.Repositories.ListCommits(ctx, s.options.githubOrg, s.options.githubRepo, listOptions)
if err != nil {
return nil, err
}
for _, commit := range commits {
if len(commit.Parents) > 1 {
note, err := s.noteFromCommit(ctx, commit)
if err != nil {
s.log.Debug(err)
continue
}
notes = append(notes, note)
}
}
if resp.NextPage == 0 {
break
}
listOptions.Page = resp.NextPage
}
return notes, nil
}
func (s *scraper) WriteToFile(w io.Writer, notes []*note) error {
var err error
write := func(s string) {
if err != nil {
return
}
_, err = w.Write([]byte(s))
}
write(fmt.Sprintf("## %s\n\n", s.options.releaseVersion))
for _, note := range notes {
write(note.String())
}
write("\n\n")
return nil
}
func run(ctx context.Context, log *logrus.Entry) error {
config := &options{}
flags := config.bindFlags()
log.Info("parsing flags")
if err := flags.Parse(os.Args[1:]); err != nil {
return err
}
log.Info("validating config")
if err := config.validate(); err != nil {
return err
}
log.Info("creating release notes scraper")
scraper, err := NewScraper(ctx, log, config)
if err != nil {
return err
}
repo := fmt.Sprintf("github.com/%s/%s", scraper.options.githubOrg, scraper.options.githubRepo)
log.Infof("fetching release notes %s..%s from %s", scraper.options.startSHA, scraper.options.endSHA, repo)
notes, err := scraper.FetchReleaseNotes(ctx)
if err != nil {
return err
}
log.Infof("writing release notes to file %s", scraper.options.outputFile)
var output io.Writer
if config.outputFile == "" {
output = os.Stdout
} else {
output, err = os.OpenFile(config.outputFile, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer output.(*os.File).Close()
}
err = scraper.WriteToFile(output, notes)
if err != nil {
return err
}
return nil
}
func main() {
ctx := context.Background()
logrus.SetLevel(logrus.InfoLevel)
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
log := logrus.NewEntry(logrus.StandardLogger())
if err := run(ctx, log); err != nil {
log.Fatal(err)
}
}