Skip to content

Commit 529c4e3

Browse files
toothrotgopherbot
authored andcommitted
cmd/gopherbot: omit owner from assign reviewers
Assign reviewers correctly omits the owner from the list of gerrit reviewers, but not from the list of metadata on the CL. When adding a "Run-TryBot +1" vote to a CL, the owner is listed as a Reviewer in the metadata. This change updates both cases to use the same filter, simplifying the humanReviewersOnChange to check for bots and owner the same way for metadata and reviewers. Fixes golang/go#53077 Change-Id: Ia3d4939f6fb8d81cfb371683025b5e3ce0282fd7 Reviewed-on: https://go-review.googlesource.com/c/build/+/417479 Run-TryBot: Jenny Rakoczy <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Jenny Rakoczy <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent f9dbc65 commit 529c4e3

File tree

2 files changed

+89
-96
lines changed

2 files changed

+89
-96
lines changed

cmd/gopherbot/gopherbot.go

+51-47
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ const (
7575
gopherbotGitHubID = 8566911
7676
)
7777

78+
const (
79+
gobotGerritID = "5976"
80+
gerritbotGerritID = "12446"
81+
kokoroGerritID = "37747"
82+
)
83+
7884
// GitHub Label IDs for the golang/go repo.
7985
const (
8086
needsDecisionID = 373401956
@@ -2322,24 +2328,29 @@ func (b *gopherbot) whoNeedsAccess(ctx context.Context) error {
23222328
// used as a key to deletedChanges and the ID returned by cl.ChangeID() can be
23232329
// associated with multiple changes (cherry-picks, for example).
23242330
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-
)
23302331
// The CL's owner will be GerritBot if it is imported from a PR.
23312332
// In that case, if the CL's author has a Gerrit account, they will be
23322333
// added as a reviewer (golang.org/issue/30265). Otherwise, no reviewers
23332334
// 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
23362337
minHumans := 1
23372338
if isPR {
23382339
minHumans = 2
23392340
}
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+
}
23402350

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
23432354
}
23442355

23452356
reviewers, err := b.gerrit.ListReviewers(ctx, change.ID())
@@ -2350,22 +2361,29 @@ func (b *gopherbot) humanReviewersOnChange(ctx context.Context, change gerritCha
23502361
log.Printf("Could not list reviewers on change %q: %v", change.ID(), err)
23512362
return nil, true
23522363
}
2353-
var ids []string
2364+
ids = []string{}
23542365
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.
23622369
continue
23632370
}
2364-
ids = append(ids, strconv.FormatInt(r.NumericID, 10))
2371+
ids = append(ids, id)
23652372
}
23662373
return ids, len(ids) >= minHumans
23672374
}
23682375

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+
23692387
// hasServiceUserTag reports whether the account has a SERVICE_USER tag.
23702388
func hasServiceUserTag(a gerrit.AccountInfo) bool {
23712389
for _, t := range a.Tags {
@@ -2579,17 +2597,8 @@ var reviewerRe = regexp.MustCompile(`.* <(?P<id>\d+)@.*>`)
25792597

25802598
const gerritInstanceID = "@62eb7196-b449-3ce5-99f1-c037f21e1705"
25812599

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 {
25932602
var ids []string
25942603
for _, m := range metas {
25952604
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
26002609
if !strings.HasPrefix(ln, "Reviewer:") && !strings.HasPrefix(ln, "CC:") {
26012610
return nil
26022611
}
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
26082620
}
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
26202623
}
2624+
ids = append(ids, match[i])
26212625
}
26222626
return nil
26232627
})
26242628
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)
26262630
}
26272631
}
2628-
return ids, count >= minHumans
2632+
return ids
26292633
}
26302634

26312635
func getCodeOwners(ctx context.Context, paths []string) ([]*owners.Entry, error) {

cmd/gopherbot/gopherbot_test.go

+38-49
Original file line numberDiff line numberDiff line change
@@ -355,85 +355,74 @@ func TestRemoveLabels(t *testing.T) {
355355

356356
func TestHumanReviewersInMetas(t *testing.T) {
357357
testCases := []struct {
358+
desc string
358359
commitMsg string
359360
hasHuman bool
360-
atLeast int
361361
wantIDs []string
362362
}{
363-
{`Patch-set: 6
363+
{
364+
desc: "one human reviewer",
365+
commitMsg: `Patch-set: 6
364366
Reviewer: Andrew Bonventre <22285@62eb7196-b449-3ce5-99f1-c037f21e1705>
365367
`,
366-
true,
367-
1,
368-
[]string{"22285"},
368+
hasHuman: true,
369+
wantIDs: []string{"22285"},
369370
},
370-
{`Patch-set: 6
371+
{
372+
desc: "one human CC",
373+
commitMsg: `Patch-set: 6
371374
CC: Andrew Bonventre <22285@62eb7196-b449-3ce5-99f1-c037f21e1705>
372375
`,
373-
true,
374-
1,
375-
[]string{"22285"},
376+
hasHuman: true,
377+
wantIDs: []string{"22285"},
376378
},
377-
{`Patch-set: 6
379+
{
380+
desc: "gobot reviewer",
381+
commitMsg: `Patch-set: 6
378382
Reviewer: Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>
379383
`,
380-
false,
381-
1,
382-
[]string{},
384+
wantIDs: []string{"5976"},
383385
},
384-
{`Patch-set: 6
386+
{
387+
desc: "gobot reviewer and human CC",
388+
commitMsg: `Patch-set: 6
385389
Reviewer: Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>
386390
CC: Andrew Bonventre <22285@62eb7196-b449-3ce5-99f1-c037f21e1705>
387391
`,
388-
true,
389-
1,
390-
[]string{"22285"},
392+
hasHuman: true,
393+
wantIDs: []string{"5976", "22285"},
391394
},
392-
{`Patch-set: 6
395+
{
396+
desc: "gobot reviewer and human reviewer",
397+
commitMsg: `Patch-set: 6
393398
Reviewer: Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>
394399
Reviewer: Andrew Bonventre <22285@62eb7196-b449-3ce5-99f1-c037f21e1705>
395400
`,
396-
true,
397-
1,
398-
[]string{"22285"},
401+
hasHuman: true,
402+
wantIDs: []string{"5976", "22285"},
399403
},
400-
{`Patch-set: 6
401-
Reviewer: Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>
402-
Reviewer: Andrew Bonventre <22285@62eb7196-b449-3ce5-99f1-c037f21e1705>
403-
`,
404-
false,
405-
2,
406-
[]string{"22285"},
407-
},
408-
{`Patch-set: 6
404+
{
405+
desc: "gobot reviewer and two human reviewers",
406+
commitMsg: `Patch-set: 6
409407
Reviewer: Gobot Gobot <5976@62eb7196-b449-3ce5-99f1-c037f21e1705>
410408
Reviewer: Andrew Bonventre <22285@62eb7196-b449-3ce5-99f1-c037f21e1705>
411409
Reviewer: Rebecca Stambler <16140@62eb7196-b449-3ce5-99f1-c037f21e1705>
412410
`,
413-
true,
414-
2,
415-
[]string{"22285", "16140"},
411+
hasHuman: true,
412+
wantIDs: []string{"5976", "22285", "16140"},
416413
},
417414
}
418415

419416
for _, tc := range testCases {
420-
metas := []*maintner.GerritMeta{
421-
{Commit: &maintner.GitCommit{Msg: tc.commitMsg}},
422-
}
423-
ids, got := humanReviewersInMetas(metas, tc.atLeast)
424-
if want := tc.hasHuman; got != want {
425-
t.Errorf("Unexpected result for meta commit message: got %v; want %v for\n%s", got, want, tc.commitMsg)
426-
continue
427-
}
428-
if len(ids) != len(tc.wantIDs) {
429-
t.Errorf("Unexpected result for meta commit message: got %v reviewer IDs, want %v", len(ids), len(tc.wantIDs))
430-
continue
431-
}
432-
for i, id := range tc.wantIDs {
433-
if id != ids[i] {
434-
t.Errorf("Unexpected ID at %d: got %v, want %v", i, ids[i], id)
417+
t.Run(tc.desc, func(t *testing.T) {
418+
metas := []*maintner.GerritMeta{
419+
{Commit: &maintner.GitCommit{Msg: tc.commitMsg}},
435420
}
436-
}
421+
ids := reviewersInMetas(metas)
422+
if diff := cmp.Diff(tc.wantIDs, ids); diff != "" {
423+
t.Fatalf("reviewersInMetas() mismatch (-want +got):\n%s", diff)
424+
}
425+
})
437426
}
438427
}
439428

0 commit comments

Comments
 (0)