Skip to content

Commit ec2024f

Browse files
authored
Merge pull request #69 from gomicro/next-test
next test
2 parents 87393df + 32af5cb commit ec2024f

File tree

121 files changed

+9685
-2536
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+9685
-2536
lines changed

client/changelog.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package client
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"strings"
78
)
89

9-
var prBodyTemplate = `
10+
var (
11+
ErrNoCommits = errors.New("no commits")
12+
13+
prBodyTemplate = `
1014
----
1115
Release PR created with ` + "`train`"
16+
)
1217

1318
func (c *Client) createChangeLog(ctx context.Context, owner, name, base, head string) (map[string][]string, error) {
1419
changes := map[string][]string{
@@ -22,11 +27,11 @@ func (c *Client) createChangeLog(ctx context.Context, owner, name, base, head st
2227

2328
comp, _, err := c.ghClient.Repositories.CompareCommits(ctx, owner, name, base, head)
2429
if err != nil {
25-
return nil, fmt.Errorf("compare commits: %v", err.Error())
30+
return nil, fmt.Errorf("compare commits: %w", err)
2631
}
2732

2833
if len(comp.Commits) == 0 {
29-
return nil, fmt.Errorf("no commits")
34+
return nil, ErrNoCommits
3035
}
3136

3237
for _, commit := range comp.Commits {

client/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (c *Client) GetLogins(ctx context.Context) ([]string, error) {
8080
return nil, fmt.Errorf("github: hit rate limit")
8181
}
8282

83-
return nil, fmt.Errorf("get user: %v", err.Error())
83+
return nil, fmt.Errorf("get user: %w", err)
8484
}
8585

8686
logins = append(logins, strings.ToLower(user.GetLogin()))
@@ -96,7 +96,7 @@ func (c *Client) GetLogins(ctx context.Context) ([]string, error) {
9696
return nil, fmt.Errorf("github: hit rate limit")
9797
}
9898

99-
return nil, fmt.Errorf("list orgs: %v", err.Error())
99+
return nil, fmt.Errorf("list orgs: %w", err)
100100
}
101101

102102
for i := range orgs {

client/clienttest/clienttest.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package clienttest
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/gomicro/crawl"
8+
"github.com/google/go-github/github"
9+
)
10+
11+
type ClientTest struct {
12+
cfg *Config
13+
}
14+
15+
type Config struct {
16+
BaseBranchName string
17+
Logins []string
18+
LoginsError error
19+
Repos []*github.Repository
20+
ReposError error
21+
ProcessReposError error
22+
}
23+
24+
func New(cfg *Config) *ClientTest {
25+
return &ClientTest{
26+
cfg: cfg,
27+
}
28+
}
29+
30+
func (ct *ClientTest) GetBaseBranchName() string {
31+
if ct.cfg != nil {
32+
return ct.cfg.BaseBranchName
33+
}
34+
35+
return ""
36+
}
37+
38+
func (ct *ClientTest) GetLogins(context.Context) ([]string, error) {
39+
if ct.cfg.LoginsError != nil {
40+
return nil, ct.cfg.LoginsError
41+
}
42+
43+
return ct.cfg.Logins, nil
44+
}
45+
46+
func (ct *ClientTest) GetRepos(ctx context.Context, progress *crawl.Progress, name string) ([]*github.Repository, error) {
47+
if ct.cfg.ReposError != nil {
48+
return nil, ct.cfg.ReposError
49+
}
50+
51+
return ct.cfg.Repos, nil
52+
}
53+
54+
func (ct *ClientTest) ProcessRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
55+
if ct.cfg.ProcessReposError != nil {
56+
return nil, ct.cfg.ProcessReposError
57+
}
58+
59+
urls := make([]string, len(ct.cfg.Repos))
60+
61+
for i, r := range repos {
62+
name := r.GetName()
63+
owner := r.GetOwner().GetLogin()
64+
head := r.GetDefaultBranch()
65+
66+
if !dryRun {
67+
urls = append(urls, fmt.Sprintf("https://github.com/%s/%s/pull/%d", owner, name, i))
68+
continue
69+
}
70+
71+
urls = append(urls, fmt.Sprintf("https://github.com/%s/%s/compare/%s...%s", owner, name, ct.cfg.BaseBranchName, head))
72+
}
73+
74+
return urls, nil
75+
}
76+
77+
func (ct *ClientTest) ReleaseRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
78+
return nil, nil
79+
}

client/interface.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package client
2+
3+
import (
4+
"context"
5+
6+
"github.com/gomicro/crawl"
7+
"github.com/google/go-github/github"
8+
)
9+
10+
// interface for a train client
11+
type Clienter interface {
12+
GetBaseBranchName() string
13+
GetLogins(context.Context) ([]string, error)
14+
GetRepos(context.Context, *crawl.Progress, string) ([]*github.Repository, error)
15+
ProcessRepos(context.Context, *crawl.Progress, []*github.Repository, bool) ([]string, error)
16+
ReleaseRepos(context.Context, *crawl.Progress, []*github.Repository, bool) ([]string, error)
17+
}

client/repos.go

Lines changed: 71 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ package client
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
7-
"os"
88
"sort"
99
"strings"
1010

11+
"github.com/gomicro/crawl"
12+
"github.com/gomicro/crawl/bar"
1113
"github.com/google/go-github/github"
12-
"github.com/gosuri/uiprogress"
1314
)
1415

15-
func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repository, error) {
16+
var ErrGetBranch = errors.New("get branch")
17+
18+
func (c *Client) GetRepos(ctx context.Context, progress *crawl.Progress, name string) ([]*github.Repository, error) {
1619
count := 0
1720
orgFound := true
1821

@@ -24,7 +27,7 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
2427
return nil, fmt.Errorf("github: hit rate limit")
2528
}
2629

27-
return nil, fmt.Errorf("get org: %v", err)
30+
return nil, fmt.Errorf("get org: %w", err)
2831
}
2932

3033
if resp.StatusCode == http.StatusNotFound {
@@ -49,12 +52,16 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
4952
return nil, fmt.Errorf("no repos found")
5053
}
5154

52-
repoBar := uiprogress.AddBar(count).
53-
AppendCompleted().
54-
PrependElapsed().
55-
PrependFunc(func(b *uiprogress.Bar) string {
56-
return fmt.Sprintf("Fetching (%d/%d)", b.Current(), count)
57-
})
55+
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
56+
theme.Append(func(b *bar.Bar) string {
57+
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
58+
})
59+
theme.Prepend(func(b *bar.Bar) string {
60+
return fmt.Sprintf("Fetching (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
61+
})
62+
63+
repoBar := bar.New(theme, count)
64+
progress.AddBar(repoBar)
5865

5966
orgOpts := &github.RepositoryListByOrgOptions{
6067
Type: "all",
@@ -139,21 +146,25 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
139146
return repos, nil
140147
}
141148

142-
func (c *Client) ProcessRepos(ctx context.Context, repos []*github.Repository, dryRun bool) ([]string, error) {
149+
func (c *Client) ProcessRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
143150
count := len(repos)
144151
name := repos[0].GetName()
145152
owner := repos[0].GetOwner().GetLogin()
146153
appendStr := fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)
147154

148-
bar := uiprogress.AddBar(count).
149-
AppendCompleted().
150-
PrependElapsed().
151-
PrependFunc(func(b *uiprogress.Bar) string {
152-
return fmt.Sprintf("Processing (%d/%d)", b.Current(), count)
153-
}).
154-
AppendFunc(func(b *uiprogress.Bar) string {
155-
return appendStr
156-
})
155+
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
156+
theme.Append(func(b *bar.Bar) string {
157+
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
158+
})
159+
theme.Append(func(b *bar.Bar) string {
160+
return appendStr
161+
})
162+
theme.Prepend(func(b *bar.Bar) string {
163+
return fmt.Sprintf("Processing (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
164+
})
165+
166+
repoBar := bar.New(theme, count)
167+
progress.AddBar(repoBar)
157168

158169
urls := []string{}
159170
for _, repo := range repos {
@@ -163,17 +174,16 @@ func (c *Client) ProcessRepos(ctx context.Context, repos []*github.Repository, d
163174

164175
url, err := c.processRepo(ctx, repo, dryRun)
165176
if err != nil {
166-
if strings.HasPrefix(err.Error(), "get branch: ") || strings.HasPrefix(err.Error(), "no commits") {
167-
bar.Incr()
177+
if errors.Is(err, ErrGetBranch) || errors.Is(err, ErrNoCommits) {
178+
repoBar.Incr()
168179
continue
169180
}
170181

171-
fmt.Printf("process repo: %v\n", err.Error())
172-
os.Exit(1)
182+
return nil, fmt.Errorf("process repo: %w", err)
173183
}
174184

175185
urls = append(urls, url)
176-
bar.Incr()
186+
repoBar.Incr()
177187
}
178188

179189
appendStr = ""
@@ -191,7 +201,7 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
191201
c.rate.Wait(ctx) //nolint: errcheck
192202
_, _, err := c.ghClient.Repositories.GetBranch(ctx, owner, name, c.cfg.ReleaseBranch)
193203
if err != nil {
194-
return "", fmt.Errorf("get branch: %v", err.Error())
204+
return "", fmt.Errorf("%w: %w", ErrGetBranch, err)
195205
}
196206

197207
opts := &github.PullRequestListOptions{
@@ -202,7 +212,7 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
202212
c.rate.Wait(ctx) //nolint: errcheck
203213
prs, _, err := c.ghClient.PullRequests.List(ctx, owner, name, opts)
204214
if err != nil {
205-
return "", fmt.Errorf("list prs: %v", err.Error())
215+
return "", fmt.Errorf("list prs: %w", err)
206216
}
207217

208218
if len(prs) > 0 {
@@ -242,17 +252,17 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
242252
c.rate.Wait(ctx) //nolint: errcheck
243253
pr, _, err := c.ghClient.PullRequests.Create(ctx, owner, name, newPR)
244254
if err != nil {
245-
return "", fmt.Errorf("create pr: %v", err.Error())
255+
return "", fmt.Errorf("create pr: %w", err)
246256
}
247257

248258
return pr.GetHTMLURL(), nil
249259
}
250260

251-
return fmt.Sprintf("https://github.com/%v/%v/compare/%v...%v", owner, name, c.cfg.ReleaseBranch, head), nil
261+
return fmt.Sprintf("https://github.com/%s/%s/compare/%s...%s", owner, name, c.cfg.ReleaseBranch, head), nil
252262
}
253263

254-
func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, dryRun bool) ([]string, error) {
255-
releases, err := c.getReleases(ctx, repos)
264+
func (c *Client) ReleaseRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
265+
releases, err := c.getReleases(ctx, progress, repos)
256266
if err != nil {
257267
return nil, fmt.Errorf("releases: %v\n", err.Error())
258268
}
@@ -267,15 +277,19 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
267277
owner := repo.GetOwner().GetLogin()
268278
appendStr := fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)
269279

270-
bar := uiprogress.AddBar(count).
271-
AppendCompleted().
272-
PrependElapsed().
273-
PrependFunc(func(b *uiprogress.Bar) string {
274-
return fmt.Sprintf("Processing Releases (%d/%d)", b.Current(), count)
275-
}).
276-
AppendFunc(func(b *uiprogress.Bar) string {
277-
return appendStr
278-
})
280+
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
281+
theme.Append(func(b *bar.Bar) string {
282+
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
283+
})
284+
theme.Append(func(b *bar.Bar) string {
285+
return appendStr
286+
})
287+
theme.Prepend(func(b *bar.Bar) string {
288+
return fmt.Sprintf("Processing Releases (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
289+
})
290+
291+
repoBar := bar.New(theme, count)
292+
progress.AddBar(repoBar)
279293

280294
var released []string
281295
for _, release := range releases {
@@ -292,7 +306,7 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
292306
}
293307

294308
if strings.ToLower(release.GetMergeableState()) != "clean" {
295-
bar.Incr()
309+
repoBar.Incr()
296310
continue
297311
}
298312

@@ -310,7 +324,7 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
310324
released = append(released, release.GetHTMLURL())
311325
}
312326

313-
bar.Incr()
327+
repoBar.Incr()
314328
}
315329

316330
appendStr = ""
@@ -320,23 +334,27 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
320334
return released, nil
321335
}
322336

323-
func (c *Client) getReleases(ctx context.Context, repos []*github.Repository) ([]*github.PullRequest, error) {
337+
func (c *Client) getReleases(ctx context.Context, progress *crawl.Progress, repos []*github.Repository) ([]*github.PullRequest, error) {
324338
var releases []*github.PullRequest
325339

326340
count := len(repos)
327341
name := repos[0].GetName()
328342
owner := repos[0].GetOwner().GetLogin()
329343
appendStr := fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)
330344

331-
bar := uiprogress.AddBar(count).
332-
AppendCompleted().
333-
PrependElapsed().
334-
PrependFunc(func(b *uiprogress.Bar) string {
335-
return fmt.Sprintf("Collecting Releases (%d/%d)", b.Current(), count)
336-
}).
337-
AppendFunc(func(b *uiprogress.Bar) string {
338-
return appendStr
339-
})
345+
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
346+
theme.Append(func(b *bar.Bar) string {
347+
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
348+
})
349+
theme.Append(func(b *bar.Bar) string {
350+
return appendStr
351+
})
352+
theme.Prepend(func(b *bar.Bar) string {
353+
return fmt.Sprintf("Collecting Releases (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
354+
})
355+
356+
repoBar := bar.New(theme, count)
357+
progress.AddBar(repoBar)
340358

341359
for _, repo := range repos {
342360
owner = repo.GetOwner().GetLogin()
@@ -356,7 +374,7 @@ func (c *Client) getReleases(ctx context.Context, repos []*github.Repository) ([
356374
}
357375

358376
releases = append(releases, rs...)
359-
bar.Incr()
377+
repoBar.Incr()
360378
}
361379

362380
appendStr = ""

0 commit comments

Comments
 (0)