forked from Songmu/ghch
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ghch.go
341 lines (312 loc) · 7.31 KB
/
ghch.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package ghch
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/Songmu/gitsemvers"
"github.com/google/go-github/github"
"github.com/pkg/errors"
"github.com/tcnksm/go-gitconfig"
"golang.org/x/oauth2"
)
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
// Run the ghch
func (gh *Ghch) Run() error {
ctx := context.Background()
if err := gh.initialize(ctx); err != nil {
return err
}
if gh.All {
return gh.runAll(ctx)
}
return gh.run(ctx)
}
func (gh *Ghch) runAll(ctx context.Context) error {
chlog := Changelog{}
vers := append(gh.versions(), "")
prevRev := ""
for _, rev := range vers {
r, err := gh.getSection(ctx, rev, prevRev)
if err != nil {
return err
}
if prevRev == "" && gh.NextVersion != "" {
r.ToRevision = gh.NextVersion
}
chlog.Sections = append(chlog.Sections, r)
prevRev = rev
}
if gh.Format != "markdown" { // json
encoder := json.NewEncoder(gh.OutStream)
encoder.SetIndent("", " ")
return encoder.Encode(chlog)
}
results := make([]string, len(chlog.Sections))
for i, v := range chlog.Sections {
results[i], _ = v.toMkdn()
}
if gh.Write {
content := "# Changelog\n\n" + strings.Join(results, "\n\n")
if err := ioutil.WriteFile(gh.ChangelogMd, []byte(content), 0644); err != nil {
return err
}
} else {
fmt.Fprintln(gh.OutStream, strings.Join(results, "\n\n"))
}
return nil
}
func (gh *Ghch) run(ctx context.Context) error {
if gh.Latest {
vers := gh.versions()
if len(vers) > 0 {
gh.To = vers[0]
}
if gh.From == "" && len(vers) > 1 {
gh.From = vers[1]
}
} else if gh.From == "" && gh.To == "" {
gh.From = gh.getLatestSemverTag()
}
r, err := gh.getSection(ctx, gh.From, gh.To)
if err != nil {
return err
}
if r.ToRevision == "" && gh.NextVersion != "" {
r.ToRevision = gh.NextVersion
}
if gh.Format != "markdown" { // json
encoder := json.NewEncoder(gh.OutStream)
encoder.SetIndent("", " ")
return encoder.Encode(r)
}
str, err := r.toMkdn()
if err != nil {
return err
}
if gh.Write {
content := ""
if exists(gh.ChangelogMd) {
byt, err := ioutil.ReadFile(gh.ChangelogMd)
if err != nil {
return err
}
content = insertNewChangelog(byt, str)
} else {
content = "# Changelog\n\n" + str + "\n"
}
if err := ioutil.WriteFile(gh.ChangelogMd, []byte(content), 0644); err != nil {
return err
}
} else {
fmt.Fprintln(gh.OutStream, str)
}
return nil
}
func (gh *Ghch) initialize(ctx context.Context) error {
if gh.Write {
gh.Format = "markdown"
if gh.ChangelogMd == "" {
gh.ChangelogMd = "CHANGELOG.md"
}
}
if gh.OutStream == nil {
gh.OutStream = os.Stdout
}
var oauthClient *http.Client
gh.setToken()
if gh.Token != "" {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: gh.Token})
oauthClient = oauth2.NewClient(ctx, ts)
}
gh.client = github.NewClient(oauthClient)
gh.setBaseURL()
if gh.BaseURL != "" {
u, err := url.Parse(gh.BaseURL)
if err != nil {
return err
}
gh.client.BaseURL = u
}
return nil
}
func (gh *Ghch) setToken() {
if gh.Token != "" {
return
}
if gh.Token = os.Getenv("GITHUB_TOKEN"); gh.Token != "" {
return
}
gh.Token, _ = gitconfig.GithubToken()
}
func (gh *Ghch) setBaseURL() {
if gh.BaseURL != "" {
return
}
if gh.BaseURL = os.Getenv("GITHUB_API"); gh.BaseURL != "" {
return
}
}
func (gh *Ghch) gitProg() string {
if gh.GitPath != "" {
return gh.GitPath
}
return "git"
}
func (gh *Ghch) cmd(argv ...string) (string, error) {
arg := []string{"-C", gh.RepoPath}
arg = append(arg, argv...)
cmd := exec.Command(gh.gitProg(), arg...)
cmd.Env = append(os.Environ(), "LANG=C")
var b bytes.Buffer
cmd.Stdout = &b
cmd.Stderr = os.Stderr
err := cmd.Run()
return b.String(), err
}
var verReg = regexp.MustCompile(`^v?[0-9]+(?:\.[0-9]+){0,2}$`)
func (gh *Ghch) versions() []string {
sv := gitsemvers.Semvers{
RepoPath: gh.RepoPath,
GitPath: gh.GitPath,
}
return sv.VersionStrings()
}
func (gh *Ghch) getRemote() string {
if gh.Remote != "" {
return gh.Remote
}
return "origin"
}
var repoURLReg = regexp.MustCompile(`([^/:]+)/([^/]+?)(?:\.git)?$`)
func (gh *Ghch) ownerAndRepo() (owner, repo string) {
out, _ := gh.cmd("remote", "-v")
remotes := strings.Split(out, "\n")
for _, r := range remotes {
fields := strings.Fields(r)
if len(fields) > 1 && fields[0] == gh.getRemote() {
if matches := repoURLReg.FindStringSubmatch(fields[1]); len(matches) > 2 {
return matches[1], matches[2]
}
}
}
return
}
func (gh *Ghch) htmlURL(ctx context.Context, owner, repoStr string) (string, error) {
repo, resp, err := gh.client.Repositories.Get(ctx, owner, repoStr)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return "", nil
}
return "", err
}
return *repo.HTMLURL, nil
}
func (gh *Ghch) mergedPRs(ctx context.Context, from, to string) (prs []*github.PullRequest, err error) {
owner, repo := gh.ownerAndRepo()
prlogs, err := gh.mergedPRLogs(from, to)
if err != nil {
return
}
prs = make([]*github.PullRequest, 0, len(prlogs))
prsWithNil := make([]*github.PullRequest, len(prlogs))
errsWithNil := make([]error, len(prlogs))
var wg sync.WaitGroup
for i, prlog := range prlogs {
wg.Add(1)
go func(i int, prlog *mergedPRLog) {
defer wg.Done()
pr, resp, err := gh.client.PullRequests.Get(ctx, owner, repo, prlog.num)
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
return
}
errsWithNil[i] = err
log.Println(err)
return
}
// replace repoowner:branch-name to repo-owner/branch-name
if strings.Replace(*pr.Head.Label, ":", "/", 1) != prlog.branch {
return
}
if !gh.Verbose {
pr = reducePR(pr)
}
prsWithNil[i] = pr
}(i, prlog)
}
wg.Wait()
for _, pr := range prsWithNil {
if pr != nil {
prs = append(prs, pr)
}
}
for _, e := range errsWithNil {
if e != nil {
err = e
}
}
return
}
func (gh *Ghch) getLatestSemverTag() string {
vers := gh.versions()
if len(vers) < 1 {
return ""
}
return vers[0]
}
type mergedPRLog struct {
num int
branch string
}
func (gh *Ghch) mergedPRLogs(from, to string) (nums []*mergedPRLog, err error) {
revisionRange := fmt.Sprintf("%s..%s", from, to)
out, err := gh.cmd("log", revisionRange, "--merges", "--oneline")
if err != nil {
return []*mergedPRLog{}, err
}
return parseMergedPRLogs(out), nil
}
var prMergeReg = regexp.MustCompile(`^[a-f0-9]+ Merge pull request #([0-9]+) from (\S+)`)
func parseMergedPRLogs(out string) (prs []*mergedPRLog) {
lines := strings.Split(out, "\n")
for _, line := range lines {
if matches := prMergeReg.FindStringSubmatch(line); len(matches) > 2 {
i, _ := strconv.Atoi(matches[1])
prs = append(prs, &mergedPRLog{
num: i,
branch: matches[2],
})
}
}
return
}
func (gh *Ghch) getChangedAt(rev string) (time.Time, error) {
if rev == "" {
rev = "HEAD"
}
out, err := gh.cmd("show", "-s", rev+"^{commit}", `--format=%ct`)
if err != nil {
return time.Time{}, errors.Wrap(err, "failed to get changed at from git revision. `git show` failed")
}
out = strings.TrimSpace(out)
i, err := strconv.ParseInt(out, 10, 64)
if err != nil {
return time.Time{}, errors.Wrap(err, "failed to get changed at from git revision. ParseInt failed")
}
return time.Unix(i, 0), nil
}