Skip to content

Commit c4d5b43

Browse files
committed
plugin/label: Suggest members to assign label
Provide a breadcrumb to users if they are unable to assign a label *and* there are no allowed_teams configured. Signed-off-by: Stephen Finucane <[email protected]>
1 parent 8160c62 commit c4d5b43

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

pkg/plugins/label/label.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package label
1818

1919
import (
2020
"fmt"
21+
"math/rand/v2"
2122
"regexp"
2223
"strings"
2324

@@ -334,7 +335,14 @@ func canUserSetLabel(ghc githubClient, org string, user string, label string, re
334335

335336
msg := fmt.Sprintf("The label(s) `%s` cannot be applied or removed, because you are not in one of the allowed teams and are not an allowed user.", label)
336337
if len(config.AllowedTeams) > 0 {
337-
msg += fmt.Sprintf(" Must be a member of one of these teams: %v", strings.Join(config.AllowedTeams, ", "))
338+
msg += fmt.Sprintf(" Must be a member of one of these teams: %s", strings.Join(config.AllowedTeams, ", "))
339+
} else if len(config.AllowedUsers) > 0 {
340+
randomUsers := []string{}
341+
// Just in case we have some very large teams, we only show up to 20 users.
342+
for userIdx := range rand.Perm(len(config.AllowedUsers))[:min(len(config.AllowedUsers), 20)] {
343+
randomUsers = append(randomUsers, config.AllowedUsers[userIdx])
344+
}
345+
msg += fmt.Sprintf(" Consider assigning one of the following members: %s", strings.Join(randomUsers, ","))
338346
}
339347
return false, msg, nil
340348
}

pkg/plugins/label/label_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ func TestHandleComment(t *testing.T) {
718718
restrictedLabels: map[string][]plugins.RestrictedLabel{"org": {{Label: "restricted-label", AllowedUsers: []string{orgMemberAlt}}}},
719719
action: github.GenericCommentActionCreated,
720720
expectedBotComment: true,
721-
expectedCommentText: "The label(s) `restricted-label` cannot be applied or removed, because you are not in one of the allowed teams and are not an allowed user.",
721+
expectedCommentText: "The label(s) `restricted-label` cannot be applied or removed, because you are not in one of the allowed teams and are not an allowed user. Consider assigning one of the following members: Mallory",
722722
},
723723
{
724724
name: "Restricted label addition, user is in allowed_teams",
@@ -759,7 +759,7 @@ func TestHandleComment(t *testing.T) {
759759
restrictedLabels: map[string][]plugins.RestrictedLabel{"org": {{Label: "restricted-label", AllowedUsers: []string{orgMemberAlt}}}},
760760
action: github.GenericCommentActionCreated,
761761
expectedBotComment: true,
762-
expectedCommentText: "The label(s) `restricted-label` cannot be applied or removed, because you are not in one of the allowed teams and are not an allowed user.",
762+
expectedCommentText: "The label(s) `restricted-label` cannot be applied or removed, because you are not in one of the allowed teams and are not an allowed user. Consider assigning one of the following members: Mallory",
763763
},
764764
{
765765
name: "Restricted label removal, user is in allowed_teams",

0 commit comments

Comments
 (0)