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 1 commit
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(s) that would trigger the RestrictedLabel.AllowedUsers'
smg247 marked this conversation as resolved.
Show resolved Hide resolved
// to be assigned on the PR.
type AssignOnLabel struct {
Label string `json:"label"`
}

// Trigger specifies a configuration for a single trigger.
Expand Down
24 changes: 22 additions & 2 deletions prow/plugins/label/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,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,7 +87,7 @@ 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
Expand All @@ -97,6 +105,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 @@ -218,6 +227,17 @@ 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)
}

// Assign configured users if present
for _, restrictedLabel := range restrictedLabels {
for _, assignOn := range restrictedLabel.AssignOn {
smg247 marked this conversation as resolved.
Show resolved Hide resolved
if strings.EqualFold(labelToAdd, assignOn.Label) {
if err := gc.AssignIssue(org, repo, e.Number, restrictedLabel.AllowedUsers); err != nil {
log.WithError(err).Errorf("GitHub failed to assign reviewers for the following label: %s", restrictedLabel.Label)
}
}
}
}
}

// Remove labels
Expand Down
Loading