@@ -2,17 +2,20 @@ package client
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"net/http"
7
- "os"
8
8
"sort"
9
9
"strings"
10
10
11
+ "github.com/gomicro/crawl"
12
+ "github.com/gomicro/crawl/bar"
11
13
"github.com/google/go-github/github"
12
- "github.com/gosuri/uiprogress"
13
14
)
14
15
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 ) {
16
19
count := 0
17
20
orgFound := true
18
21
@@ -24,7 +27,7 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
24
27
return nil , fmt .Errorf ("github: hit rate limit" )
25
28
}
26
29
27
- return nil , fmt .Errorf ("get org: %v " , err )
30
+ return nil , fmt .Errorf ("get org: %w " , err )
28
31
}
29
32
30
33
if resp .StatusCode == http .StatusNotFound {
@@ -49,12 +52,16 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
49
52
return nil , fmt .Errorf ("no repos found" )
50
53
}
51
54
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 )
58
65
59
66
orgOpts := & github.RepositoryListByOrgOptions {
60
67
Type : "all" ,
@@ -139,21 +146,25 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
139
146
return repos , nil
140
147
}
141
148
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 ) {
143
150
count := len (repos )
144
151
name := repos [0 ].GetName ()
145
152
owner := repos [0 ].GetOwner ().GetLogin ()
146
153
appendStr := fmt .Sprintf ("\n Current Repo: %v/%v" , owner , name )
147
154
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 )
157
168
158
169
urls := []string {}
159
170
for _ , repo := range repos {
@@ -163,17 +174,16 @@ func (c *Client) ProcessRepos(ctx context.Context, repos []*github.Repository, d
163
174
164
175
url , err := c .processRepo (ctx , repo , dryRun )
165
176
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 ()
168
179
continue
169
180
}
170
181
171
- fmt .Printf ("process repo: %v\n " , err .Error ())
172
- os .Exit (1 )
182
+ return nil , fmt .Errorf ("process repo: %w" , err )
173
183
}
174
184
175
185
urls = append (urls , url )
176
- bar .Incr ()
186
+ repoBar .Incr ()
177
187
}
178
188
179
189
appendStr = ""
@@ -191,7 +201,7 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
191
201
c .rate .Wait (ctx ) //nolint: errcheck
192
202
_ , _ , err := c .ghClient .Repositories .GetBranch (ctx , owner , name , c .cfg .ReleaseBranch )
193
203
if err != nil {
194
- return "" , fmt .Errorf ("get branch : %v " , err . Error () )
204
+ return "" , fmt .Errorf ("%w : %w " , ErrGetBranch , err )
195
205
}
196
206
197
207
opts := & github.PullRequestListOptions {
@@ -202,7 +212,7 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
202
212
c .rate .Wait (ctx ) //nolint: errcheck
203
213
prs , _ , err := c .ghClient .PullRequests .List (ctx , owner , name , opts )
204
214
if err != nil {
205
- return "" , fmt .Errorf ("list prs: %v " , err . Error () )
215
+ return "" , fmt .Errorf ("list prs: %w " , err )
206
216
}
207
217
208
218
if len (prs ) > 0 {
@@ -242,17 +252,17 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
242
252
c .rate .Wait (ctx ) //nolint: errcheck
243
253
pr , _ , err := c .ghClient .PullRequests .Create (ctx , owner , name , newPR )
244
254
if err != nil {
245
- return "" , fmt .Errorf ("create pr: %v " , err . Error () )
255
+ return "" , fmt .Errorf ("create pr: %w " , err )
246
256
}
247
257
248
258
return pr .GetHTMLURL (), nil
249
259
}
250
260
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
252
262
}
253
263
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 )
256
266
if err != nil {
257
267
return nil , fmt .Errorf ("releases: %v\n " , err .Error ())
258
268
}
@@ -267,15 +277,19 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
267
277
owner := repo .GetOwner ().GetLogin ()
268
278
appendStr := fmt .Sprintf ("\n Current Repo: %v/%v" , owner , name )
269
279
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 )
279
293
280
294
var released []string
281
295
for _ , release := range releases {
@@ -292,7 +306,7 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
292
306
}
293
307
294
308
if strings .ToLower (release .GetMergeableState ()) != "clean" {
295
- bar .Incr ()
309
+ repoBar .Incr ()
296
310
continue
297
311
}
298
312
@@ -310,7 +324,7 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
310
324
released = append (released , release .GetHTMLURL ())
311
325
}
312
326
313
- bar .Incr ()
327
+ repoBar .Incr ()
314
328
}
315
329
316
330
appendStr = ""
@@ -320,23 +334,27 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
320
334
return released , nil
321
335
}
322
336
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 ) {
324
338
var releases []* github.PullRequest
325
339
326
340
count := len (repos )
327
341
name := repos [0 ].GetName ()
328
342
owner := repos [0 ].GetOwner ().GetLogin ()
329
343
appendStr := fmt .Sprintf ("\n Current Repo: %v/%v" , owner , name )
330
344
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 )
340
358
341
359
for _ , repo := range repos {
342
360
owner = repo .GetOwner ().GetLogin ()
@@ -356,7 +374,7 @@ func (c *Client) getReleases(ctx context.Context, repos []*github.Repository) ([
356
374
}
357
375
358
376
releases = append (releases , rs ... )
359
- bar .Incr ()
377
+ repoBar .Incr ()
360
378
}
361
379
362
380
appendStr = ""
0 commit comments