Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to auto-assign users when specific label is added to label plugin #24458

Merged
merged 5 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions prow/plugins/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,16 @@ func (l Label) IsRestrictedLabelInAdditionalLables(restricted string) bool {
}

type RestrictedLabel struct {
Label string `json:"label"`
AllowedTeams []string `json:"allowed_teams,omitempty"`
AllowedUsers []string `json:"allowed_users,omitempty"`
Label string `json:"label"`
AllowedTeams []string `json:"allowed_teams,omitempty"`
AllowedUsers []string `json:"allowed_users,omitempty"`
AssignOn []AssignOnLabel `json:"assign_on,omitempty"`
}

// AssignOnLabel specifies the label that would trigger the RestrictedLabel.AllowedUsers'
// to be assigned on the PR.
type AssignOnLabel struct {
Label string `json:"label"`
}

// Trigger specifies a configuration for a single trigger.
Expand Down
49 changes: 43 additions & 6 deletions prow/plugins/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (

func init() {
plugins.RegisterGenericCommentHandler(PluginName, handleGenericComment, helpProvider)
plugins.RegisterIssueHandler(PluginName, handleIssue, helpProvider)
}

func configString(labels []string) string {
Expand All @@ -63,13 +64,21 @@ func helpProvider(config *plugins.Configuration, _ []config.OrgRepo) (*pluginhel
yamlSnippet, err := plugins.CommentMap.GenYaml(&plugins.Configuration{
Label: plugins.Label{
AdditionalLabels: []string{"api-review", "community/discussion"},
RestrictedLabels: map[string][]plugins.RestrictedLabel{
"*": {{
Label: "restricted-label",
AllowedTeams: []string{"authorized-team"},
AllowedUsers: []string{"alice", "bob"},
AssignOn: []plugins.AssignOnLabel{{Label: "other-label"}},
}},
},
},
})
if err != nil {
logrus.WithError(err).Warnf("cannot generate comments for %s plugin", PluginName)
}
pluginHelp := &pluginhelp.PluginHelp{
Description: "The label plugin provides commands that add or remove certain types of labels. Labels of the following types can be manipulated: 'area/*', 'committee/*', 'kind/*', 'language/*', 'priority/*', 'sig/*', 'triage/*', and 'wg/*'. More labels can be configured to be used via the /label command.",
Description: "The label plugin provides commands that add or remove certain types of labels. Labels of the following types can be manipulated: 'area/*', 'committee/*', 'kind/*', 'language/*', 'priority/*', 'sig/*', 'triage/*', and 'wg/*'. More labels can be configured to be used via the /label command. Restricted labels are only able to be added by the teams and users present in their configuration, and those users can be automatically assigned when another label is added using the assign_on config.",
Config: map[string]string{
"": configString(labels),
},
Expand All @@ -79,14 +88,18 @@ func helpProvider(config *plugins.Configuration, _ []config.OrgRepo) (*pluginhel
Usage: "/[remove-](area|committee|kind|language|priority|sig|triage|wg|label) <target>",
Description: "Applies or removes a label from one of the recognized types of labels.",
Featured: false,
WhoCanUse: "Anyone can trigger this command on issues and PRs. `triage/accepted` can only be added by org members.",
WhoCanUse: "Anyone can trigger this command on issues and PRs. `triage/accepted` can only be added by org members. Restricted labels are only able to be added by teams and users in their configuration.",
Examples: []string{"/kind bug", "/remove-area prow", "/sig testing", "/language zh", "/label foo-bar-baz"},
})
return pluginHelp, nil
}

func handleGenericComment(pc plugins.Agent, e github.GenericCommentEvent) error {
return handle(pc.GitHubClient, pc.Logger, pc.PluginConfig.Label, &e)
return handleComment(pc.GitHubClient, pc.Logger, pc.PluginConfig.Label, &e)
}

func handleIssue(pc plugins.Agent, e github.IssueEvent) error {
return handleLabelAdd(pc.GitHubClient, pc.Logger, pc.PluginConfig.Label, &e)
}

type githubClient interface {
Expand All @@ -97,6 +110,7 @@ type githubClient interface {
GetRepoLabels(owner, repo string) ([]github.Label, error)
GetIssueLabels(org, repo string, number int) ([]github.Label, error)
TeamBySlugHasMember(org string, teamSlug string, memberLogin string) (bool, error)
AssignIssue(owner, repo string, number int, assignees []string) error
}

// Get Labels from Regexp matches
Expand Down Expand Up @@ -128,7 +142,7 @@ func getLabelsFromGenericMatches(matches [][]string, labelFilter func(string) bo
return labels
}

func handle(gc githubClient, log *logrus.Entry, config plugins.Label, e *github.GenericCommentEvent) error {
func handleComment(gc githubClient, log *logrus.Entry, config plugins.Label, e *github.GenericCommentEvent) error {
if e.Action != github.GenericCommentActionCreated {
return nil
}
Expand Down Expand Up @@ -216,7 +230,7 @@ func handle(gc githubClient, log *logrus.Entry, config plugins.Label, e *github.
}

if err := gc.AddLabel(org, repo, e.Number, labelToAdd); err != nil {
log.WithError(err).Errorf("GitHub failed to add the following label: %s", labelToAdd)
log.WithError(err).WithField("label", labelToAdd).Error("GitHub failed to add the label")
}
}

Expand All @@ -243,7 +257,7 @@ func handle(gc githubClient, log *logrus.Entry, config plugins.Label, e *github.
}

if err := gc.RemoveLabel(org, repo, e.Number, labelToRemove); err != nil {
log.WithError(err).Errorf("GitHub failed to remove the following label: %s", labelToRemove)
log.WithError(err).WithField("label", labelToRemove).Error("GitHub failed to remove the label")
}
}

Expand Down Expand Up @@ -297,3 +311,26 @@ func canUserSetLabel(ghc githubClient, org string, user string, label string, re

return false, fmt.Sprintf("Can not set label %s: Must be member in one of these teams: %v", label, config.AllowedTeams), nil
}

func handleLabelAdd(gc githubClient, log *logrus.Entry, config plugins.Label, e *github.IssueEvent) error {
if e.Action != github.IssueActionLabeled {
return nil
}

org := e.Repo.Owner.Login
repo := e.Repo.Name
number := e.Issue.Number
restrictedLabels := config.RestrictedLabelsFor(e.Repo.Owner.Login, e.Repo.Name)

for _, restrictedLabel := range restrictedLabels {
for _, assignOn := range restrictedLabel.AssignOn {
if strings.EqualFold(e.Label.Name, assignOn.Label) {
// It's okay to re-assign users so no need to check if they are assigned
if err := gc.AssignIssue(org, repo, number, restrictedLabel.AllowedUsers); err != nil {
log.WithError(err).WithField("label", restrictedLabel.Label).Error("GitHub failed to assign reviewers for the label")
}
}
}
}
return nil
}
Loading