1
1
package projects
2
2
3
3
import (
4
+ "context"
4
5
"encoding/csv"
5
6
"fmt"
6
7
"os"
@@ -10,6 +11,7 @@ import (
10
11
"github.com/flant/glaball/pkg/limiter"
11
12
"github.com/flant/glaball/pkg/sort/v2"
12
13
"github.com/flant/glaball/pkg/util"
14
+ "github.com/google/go-github/v58/github"
13
15
14
16
"github.com/flant/glaball/cmd/common"
15
17
@@ -219,6 +221,74 @@ func ListProjectsByNamespace(h *client.Host, namespaces []string, opt gitlab.Lis
219
221
return listProjectsByNamespace (h , namespaces , opt , wg , data , options ... )
220
222
}
221
223
224
+ func listRepositories (h * client.Host , archived bool , opt github.RepositoryListByOrgOptions ,
225
+ wg * limiter.Limiter , data chan <- interface {}) error {
226
+ defer wg .Done ()
227
+
228
+ ctx := context .TODO ()
229
+ wg .Lock ()
230
+ list , resp , err := h .GithubClient .Repositories .ListByOrg (ctx , h .Org , & opt )
231
+ if err != nil {
232
+ wg .Error (h , err )
233
+ wg .Unlock ()
234
+ return err
235
+ }
236
+ wg .Unlock ()
237
+
238
+ for _ , v := range list {
239
+ if v .GetArchived () == archived {
240
+ data <- sort.Element {Host : h , Struct : v , Cached : resp .Header .Get ("X-From-Cache" ) == "1" }
241
+ }
242
+ }
243
+
244
+ if resp .NextPage > 0 {
245
+ wg .Add (1 )
246
+ opt .Page = resp .NextPage
247
+ go listRepositories (h , archived , opt , wg , data )
248
+ }
249
+
250
+ return nil
251
+ }
252
+
253
+ func ListRepositories (h * client.Host , archived bool , opt github.RepositoryListByOrgOptions ,
254
+ wg * limiter.Limiter , data chan <- interface {}) error {
255
+ return listRepositories (h , archived , opt , wg , data )
256
+ }
257
+
258
+ func listRepositoriesByNamespace (h * client.Host , namespaces []string , archived bool , opt github.RepositoryListByOrgOptions ,
259
+ wg * limiter.Limiter , data chan <- interface {}) error {
260
+ defer wg .Done ()
261
+
262
+ ctx := context .TODO ()
263
+ wg .Lock ()
264
+ list , resp , err := h .GithubClient .Repositories .ListByOrg (ctx , h .Org , & opt )
265
+ if err != nil {
266
+ wg .Error (h , err )
267
+ wg .Unlock ()
268
+ return err
269
+ }
270
+ wg .Unlock ()
271
+
272
+ for _ , v := range list {
273
+ if v .GetArchived () == archived && (len (namespaces ) == 0 || util .ContainsString (namespaces , v .GetName ())) {
274
+ data <- sort.Element {Host : h , Struct : v , Cached : resp .Header .Get ("X-From-Cache" ) == "1" }
275
+ }
276
+ }
277
+
278
+ if resp .NextPage > 0 {
279
+ wg .Add (1 )
280
+ opt .Page = resp .NextPage
281
+ go listRepositoriesByNamespace (h , namespaces , archived , opt , wg , data )
282
+ }
283
+
284
+ return nil
285
+ }
286
+
287
+ func ListRepositoriesByNamespace (h * client.Host , namespaces []string , archived bool , opt github.RepositoryListByOrgOptions ,
288
+ wg * limiter.Limiter , data chan <- interface {}) error {
289
+ return listRepositoriesByNamespace (h , namespaces , archived , opt , wg , data )
290
+ }
291
+
222
292
func listMergeRequests (h * client.Host , project * gitlab.Project , opt gitlab.ListProjectMergeRequestsOptions ,
223
293
wg * limiter.Limiter , data chan <- interface {}, options ... gitlab.RequestOptionFunc ) error {
224
294
defer wg .Done ()
@@ -360,12 +430,93 @@ func listMergeRequestsByAssigneeOrAuthorID(h *client.Host, project *gitlab.Proje
360
430
return nil
361
431
}
362
432
433
+ func listPullRequestsByAssigneeOrAuthorID (h * client.Host , repository * github.Repository , IDs []int ,
434
+ opt github.PullRequestListOptions , wg * limiter.Limiter , data chan <- interface {}) error {
435
+ defer wg .Done ()
436
+
437
+ ctx := context .TODO ()
438
+ wg .Lock ()
439
+ list , resp , err := h .GithubClient .PullRequests .List (ctx , h .Org , repository .GetName (), & opt )
440
+ if err != nil {
441
+ wg .Error (h , err )
442
+ wg .Unlock ()
443
+ return err
444
+ }
445
+ wg .Unlock ()
446
+
447
+ for _ , v := range list {
448
+ if len (IDs ) == 0 {
449
+ data <- sort.Element {Host : h , Struct : v , Cached : resp .Header .Get ("X-From-Cache" ) == "1" }
450
+ continue
451
+ }
452
+
453
+ // if pr has assignee, then check and continue
454
+ if v .Assignee != nil {
455
+ if util .ContainsInt (IDs , int (v .Assignee .GetID ())) {
456
+ data <- sort.Element {Host : h , Struct : v , Cached : resp .Header .Get ("X-From-Cache" ) == "1" }
457
+ }
458
+ continue
459
+ }
460
+
461
+ // otherwise check the author
462
+ if v .User != nil && util .ContainsInt (IDs , int (v .User .GetID ())) {
463
+ data <- sort.Element {Host : h , Struct : v , Cached : resp .Header .Get ("X-From-Cache" ) == "1" }
464
+ }
465
+ }
466
+
467
+ if resp .NextPage > 0 {
468
+ wg .Add (1 )
469
+ opt .Page = resp .NextPage
470
+ go listPullRequestsByAssigneeOrAuthorID (h , repository , IDs , opt , wg , data )
471
+ }
472
+
473
+ return nil
474
+ }
475
+
363
476
// authorIDs slice must be sorted in ascending order
364
477
func ListMergeRequestsByAuthorOrAssigneeID (h * client.Host , project * gitlab.Project , IDs []int , opt gitlab.ListProjectMergeRequestsOptions ,
365
478
wg * limiter.Limiter , data chan <- interface {}, options ... gitlab.RequestOptionFunc ) error {
366
479
return listMergeRequestsByAssigneeOrAuthorID (h , project , IDs , opt , wg , data , options ... )
367
480
}
368
481
482
+ // authorIDs slice must be sorted in ascending order
483
+ func ListPullRequestsByAuthorOrAssigneeID (h * client.Host , repository * github.Repository , IDs []int ,
484
+ opt github.PullRequestListOptions , wg * limiter.Limiter , data chan <- interface {}) error {
485
+ return listPullRequestsByAssigneeOrAuthorID (h , repository , IDs , opt , wg , data )
486
+ }
487
+
488
+ func listPullRequests (h * client.Host , repository * github.Repository , opt github.PullRequestListOptions ,
489
+ wg * limiter.Limiter , data chan <- interface {}) error {
490
+ defer wg .Done ()
491
+
492
+ ctx := context .TODO ()
493
+ wg .Lock ()
494
+ list , resp , err := h .GithubClient .PullRequests .List (ctx , h .Org , repository .GetName (), & opt )
495
+ if err != nil {
496
+ wg .Error (h , err )
497
+ wg .Unlock ()
498
+ return err
499
+ }
500
+ wg .Unlock ()
501
+
502
+ for _ , v := range list {
503
+ data <- sort.Element {Host : h , Struct : v , Cached : resp .Header .Get ("X-From-Cache" ) == "1" }
504
+ }
505
+
506
+ if resp .NextPage > 0 {
507
+ wg .Add (1 )
508
+ opt .Page = resp .NextPage
509
+ go listPullRequests (h , repository , opt , wg , data )
510
+ }
511
+
512
+ return nil
513
+ }
514
+
515
+ func ListPullRequests (h * client.Host , repository * github.Repository , opt github.PullRequestListOptions ,
516
+ wg * limiter.Limiter , data chan <- interface {}, options ... gitlab.RequestOptionFunc ) error {
517
+ return listPullRequests (h , repository , opt , wg , data )
518
+ }
519
+
369
520
func listMergeRequestsSearch (h * client.Host , project * gitlab.Project , key string , value * regexp.Regexp , opt gitlab.ListProjectMergeRequestsOptions ,
370
521
wg * limiter.Limiter , data chan <- interface {}, options ... gitlab.RequestOptionFunc ) error {
371
522
defer wg .Done ()
0 commit comments