Skip to content

Commit

Permalink
Add ability to auto-assign users when specific label is added to labe…
Browse files Browse the repository at this point in the history
…l plugin
  • Loading branch information
smg247 committed Nov 24, 2021
1 parent 1d59b91 commit c991fcd
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 57 deletions.
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'
// 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 {
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

0 comments on commit c991fcd

Please sign in to comment.