@@ -75,6 +75,12 @@ const (
75
75
gopherbotGitHubID = 8566911
76
76
)
77
77
78
+ const (
79
+ gobotGerritID = "5976"
80
+ gerritbotGerritID = "12446"
81
+ kokoroGerritID = "37747"
82
+ )
83
+
78
84
// GitHub Label IDs for the golang/go repo.
79
85
const (
80
86
needsDecisionID = 373401956
@@ -2322,24 +2328,29 @@ func (b *gopherbot) whoNeedsAccess(ctx context.Context) error {
2322
2328
// used as a key to deletedChanges and the ID returned by cl.ChangeID() can be
2323
2329
// associated with multiple changes (cherry-picks, for example).
2324
2330
func (b * gopherbot ) humanReviewersOnChange (ctx context.Context , change gerritChange , cl * maintner.GerritCL ) ([]string , bool ) {
2325
- const (
2326
- gobotID = 5976
2327
- gerritbotID = 12446
2328
- kokoroID = 37747
2329
- )
2330
2331
// The CL's owner will be GerritBot if it is imported from a PR.
2331
2332
// In that case, if the CL's author has a Gerrit account, they will be
2332
2333
// added as a reviewer (golang.org/issue/30265). Otherwise, no reviewers
2333
2334
// will be added. Work around this by requiring 2 human reviewers on PRs.
2334
- ownerID := int64 (cl .OwnerID ())
2335
- isPR := ownerID == gerritbotID
2335
+ ownerID := strconv . Itoa (cl .OwnerID ())
2336
+ isPR := ownerID == gerritbotGerritID
2336
2337
minHumans := 1
2337
2338
if isPR {
2338
2339
minHumans = 2
2339
2340
}
2341
+ reject := []string {gobotGerritID , gerritbotGerritID , kokoroGerritID , ownerID }
2342
+ ownerOrRobot := func (gerritID string ) bool {
2343
+ for _ , r := range reject {
2344
+ if gerritID == r {
2345
+ return true
2346
+ }
2347
+ }
2348
+ return false
2349
+ }
2340
2350
2341
- if reviewers , found := humanReviewersInMetas (cl .Metas , minHumans ); found {
2342
- return reviewers , true
2351
+ ids := deleteStrings (reviewersInMetas (cl .Metas ), ownerOrRobot )
2352
+ if len (ids ) >= minHumans {
2353
+ return ids , true
2343
2354
}
2344
2355
2345
2356
reviewers , err := b .gerrit .ListReviewers (ctx , change .ID ())
@@ -2350,22 +2361,29 @@ func (b *gopherbot) humanReviewersOnChange(ctx context.Context, change gerritCha
2350
2361
log .Printf ("Could not list reviewers on change %q: %v" , change .ID (), err )
2351
2362
return nil , true
2352
2363
}
2353
- var ids []string
2364
+ ids = []string {}
2354
2365
for _ , r := range reviewers {
2355
- switch id := r .NumericID ; {
2356
- case id == gobotID , id == gerritbotID , id == kokoroID ,
2357
- hasServiceUserTag (r .AccountInfo ):
2358
- // Skip bots.
2359
- continue
2360
- case id == ownerID :
2361
- // Skip owner.
2366
+ id := strconv .FormatInt (r .NumericID , 10 )
2367
+ if hasServiceUserTag (r .AccountInfo ) || ownerOrRobot (id ) {
2368
+ // Skip bots and owner.
2362
2369
continue
2363
2370
}
2364
- ids = append (ids , strconv . FormatInt ( r . NumericID , 10 ) )
2371
+ ids = append (ids , id )
2365
2372
}
2366
2373
return ids , len (ids ) >= minHumans
2367
2374
}
2368
2375
2376
+ func deleteStrings (s []string , reject func (val string ) bool ) []string {
2377
+ var filtered []string
2378
+ for _ , val := range s {
2379
+ if reject (val ) {
2380
+ continue
2381
+ }
2382
+ filtered = append (filtered , val )
2383
+ }
2384
+ return filtered
2385
+ }
2386
+
2369
2387
// hasServiceUserTag reports whether the account has a SERVICE_USER tag.
2370
2388
func hasServiceUserTag (a gerrit.AccountInfo ) bool {
2371
2389
for _ , t := range a .Tags {
@@ -2579,17 +2597,8 @@ var reviewerRe = regexp.MustCompile(`.* <(?P<id>\d+)@.*>`)
2579
2597
2580
2598
const gerritInstanceID = "@62eb7196-b449-3ce5-99f1-c037f21e1705"
2581
2599
2582
- // humanReviewersInMetas reports whether there are at least minHumans human
2583
- // reviewers in the given metas. It also returns the Gerrit IDs of all of the
2584
- // human reviewers.
2585
- func humanReviewersInMetas (metas []* maintner.GerritMeta , minHumans int ) ([]string , bool ) {
2586
- // Emails as they appear in maintner (<numeric ID>@<instance ID>)
2587
- var (
2588
- gobotEmail = "5976" + gerritInstanceID
2589
- gerritbotEmail = "12446" + gerritInstanceID
2590
- kokoroEmail = "37747" + gerritInstanceID
2591
- )
2592
- var count int
2600
+ // reviewersInMetas returns the Gerrit IDs of any reviewers in the metadata.
2601
+ func reviewersInMetas (metas []* maintner.GerritMeta ) []string {
2593
2602
var ids []string
2594
2603
for _ , m := range metas {
2595
2604
if ! strings .Contains (m .Commit .Msg , "Reviewer:" ) && ! strings .Contains (m .Commit .Msg , "CC:" ) {
@@ -2600,32 +2609,27 @@ func humanReviewersInMetas(metas []*maintner.GerritMeta, minHumans int) ([]strin
2600
2609
if ! strings .HasPrefix (ln , "Reviewer:" ) && ! strings .HasPrefix (ln , "CC:" ) {
2601
2610
return nil
2602
2611
}
2603
- if ! strings .Contains (ln , gobotEmail ) && ! strings .Contains (ln , gerritbotEmail ) &&
2604
- ! strings .Contains (ln , kokoroEmail ) {
2605
- match := reviewerRe .FindStringSubmatch (ln )
2606
- if match == nil {
2607
- return nil
2612
+ match := reviewerRe .FindStringSubmatch (ln )
2613
+ if match == nil {
2614
+ return nil
2615
+ }
2616
+ // Extract the human's Gerrit ID.
2617
+ for i , name := range reviewerRe .SubexpNames () {
2618
+ if name != "id" {
2619
+ continue
2608
2620
}
2609
- // A human is already on the change.
2610
- count ++
2611
- // Extract the human's Gerrit ID.
2612
- for i , name := range reviewerRe .SubexpNames () {
2613
- if name != "id" {
2614
- continue
2615
- }
2616
- if i < 0 || i > len (match ) {
2617
- continue
2618
- }
2619
- ids = append (ids , match [i ])
2621
+ if i < 0 || i > len (match ) {
2622
+ continue
2620
2623
}
2624
+ ids = append (ids , match [i ])
2621
2625
}
2622
2626
return nil
2623
2627
})
2624
2628
if err != nil {
2625
- log .Printf ("humanReviewersInMetas : got unexpected error from foreach.LineStr: %v" , err )
2629
+ log .Printf ("reviewersInMetas : got unexpected error from foreach.LineStr: %v" , err )
2626
2630
}
2627
2631
}
2628
- return ids , count >= minHumans
2632
+ return ids
2629
2633
}
2630
2634
2631
2635
func getCodeOwners (ctx context.Context , paths []string ) ([]* owners.Entry , error ) {
0 commit comments