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

Implement ConfigTree in approve plugin #12440

Closed
wants to merge 5 commits into from
Closed
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
43 changes: 17 additions & 26 deletions prow/plugins/approve/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,12 @@ func helpProvider(config *plugins.Configuration, enabledRepos []config.OrgRepo)

approveConfig := map[string]string{}
for _, repo := range enabledRepos {
opts := config.ApproveFor(repo.Org, repo.Repo)
approveConfig[repo.String()] = fmt.Sprintf("Pull requests %s require an associated issue.<br>Pull request authors %s implicitly approve their own PRs.<br>The /lgtm [cancel] command(s) %s act as approval.<br>A GitHub approved or changes requested review %s act as approval or cancel respectively.", doNot(opts.IssueRequired), doNot(opts.HasSelfApproval()), willNot(opts.LgtmActsAsApprove), willNot(opts.ConsiderReviewState()))
opts := config.Approve.RepoOptions(repo.Org, repo.Repo)
approveConfig[repo.String()] = fmt.Sprintf("Pull requests %s require an associated issue.<br>Pull request authors %s implicitly approve their own PRs.<br>The /lgtm [cancel] command(s) %s act as approval.<br>A GitHub approved or changes requested review %s act as approval or cancel respectively.", doNot(opts.AreIssueRequired()), doNot(opts.HasSelfApproval()), willNot(opts.ShouldLgtmActsAsApprove()), willNot(opts.ConsiderReviewState()))
}

yamlSnippet, err := plugins.CommentMap.GenYaml(&plugins.Configuration{
Approve: []plugins.Approve{
{
Repos: []string{
"ORGANIZATION",
"ORGANIZATION/REPOSITORY",
},
RequireSelfApproval: new(bool),
IgnoreReviewState: new(bool),
},
},
Approve: plugins.ConfigTree[plugins.Approve]{},
})
if err != nil {
logrus.WithError(err).Warnf("cannot generate comments for %s plugin", PluginName)
Expand All @@ -148,17 +139,18 @@ func helpProvider(config *plugins.Configuration, enabledRepos []config.OrgRepo)
}

func handleGenericCommentEvent(pc plugins.Agent, ce github.GenericCommentEvent) error {
opts := pc.PluginConfig.Approve.BranchOptions(ce.Repo.Owner.Login, ce.Repo.Name, ce.Repo.DefaultBranch)
return handleGenericComment(
pc.Logger,
pc.GitHubClient,
pc.OwnersClient,
pc.Config.GitHubOptions,
pc.PluginConfig,
opts,
&ce,
)
}

func handleGenericComment(log *logrus.Entry, ghc githubClient, oc ownersClient, githubConfig config.GitHubOptions, config *plugins.Configuration, ce *github.GenericCommentEvent) error {
func handleGenericComment(log *logrus.Entry, ghc githubClient, oc ownersClient, githubConfig config.GitHubOptions, opts *plugins.Approve, ce *github.GenericCommentEvent) error {
funcStart := time.Now()
defer func() {
log.WithField("duration", time.Since(funcStart).String()).Debug("Completed handleGenericComment")
Expand All @@ -173,8 +165,7 @@ func handleGenericComment(log *logrus.Entry, ghc githubClient, oc ownersClient,
return err
}

opts := config.ApproveFor(ce.Repo.Owner.Login, ce.Repo.Name)
if !isApprovalCommand(botUserChecker, opts.LgtmActsAsApprove, &comment{Body: ce.Body, Author: ce.User.Login}) {
if !isApprovalCommand(botUserChecker, *opts.LgtmActsAsApprove, &comment{Body: ce.Body, Author: ce.User.Login}) {
log.Debug("Comment does not constitute approval, skipping event.")
return nil
}
Expand Down Expand Up @@ -213,17 +204,18 @@ func handleGenericComment(log *logrus.Entry, ghc githubClient, oc ownersClient,
// handleReviewEvent should only handle reviews that have no approval command.
// Reviews with approval commands will be handled by handleGenericCommentEvent.
func handleReviewEvent(pc plugins.Agent, re github.ReviewEvent) error {
opts := pc.PluginConfig.Approve.BranchOptions(re.Repo.Owner.Login, re.Repo.Name, re.Repo.DefaultBranch)
return handleReview(
pc.Logger,
pc.GitHubClient,
pc.OwnersClient,
pc.Config.GitHubOptions,
pc.PluginConfig,
opts,
&re,
)
}

func handleReview(log *logrus.Entry, ghc githubClient, oc ownersClient, githubConfig config.GitHubOptions, config *plugins.Configuration, re *github.ReviewEvent) error {
func handleReview(log *logrus.Entry, ghc githubClient, oc ownersClient, githubConfig config.GitHubOptions, opts *plugins.Approve, re *github.ReviewEvent) error {
funcStart := time.Now()
defer func() {
log.WithField("duration", time.Since(funcStart).String()).Debug("Completed handleReview")
Expand All @@ -238,12 +230,10 @@ func handleReview(log *logrus.Entry, ghc githubClient, oc ownersClient, githubCo
return err
}

opts := config.ApproveFor(re.Repo.Owner.Login, re.Repo.Name)

// Check for an approval command is in the body. If one exists, let the
// genericCommentEventHandler handle this event. Approval commands override
// review state.
if isApprovalCommand(botUserChecker, opts.LgtmActsAsApprove, &comment{Body: re.Review.Body, Author: re.Review.User.Login}) {
if isApprovalCommand(botUserChecker, *opts.LgtmActsAsApprove, &comment{Body: re.Review.Body, Author: re.Review.User.Login}) {
log.Debug("Review constitutes approval, skipping event.")
return nil
}
Expand Down Expand Up @@ -282,17 +272,18 @@ func handleReview(log *logrus.Entry, ghc githubClient, oc ownersClient, githubCo
}

func handlePullRequestEvent(pc plugins.Agent, pre github.PullRequestEvent) error {
opts := pc.PluginConfig.Approve.BranchOptions(pre.Repo.Owner.Login, pre.Repo.Name, pre.Repo.DefaultBranch)
return handlePullRequest(
pc.Logger,
pc.GitHubClient,
pc.OwnersClient,
pc.Config.GitHubOptions,
pc.PluginConfig,
opts,
&pre,
)
}

func handlePullRequest(log *logrus.Entry, ghc githubClient, oc ownersClient, githubConfig config.GitHubOptions, config *plugins.Configuration, pre *github.PullRequestEvent) error {
func handlePullRequest(log *logrus.Entry, ghc githubClient, oc ownersClient, githubConfig config.GitHubOptions, opts *plugins.Approve, pre *github.PullRequestEvent) error {
funcStart := time.Now()
defer func() {
log.WithField("duration", time.Since(funcStart).String()).Debug("Completed handlePullRequest")
Expand Down Expand Up @@ -325,7 +316,7 @@ func handlePullRequest(log *logrus.Entry, ghc githubClient, oc ownersClient, git
ghc,
repo,
githubConfig,
config.ApproveFor(pre.Repo.Owner.Login, pre.Repo.Name),
opts,
&state{
org: pre.Repo.Owner.Login,
repo: pre.Repo.Name,
Expand Down Expand Up @@ -432,7 +423,7 @@ func handle(log *logrus.Entry, ghc githubClient, repo approvers.Repo, githubConf
if err != nil {
log.WithError(err).Errorf("Failed to find associated issue from PR body: %v", err)
}
approversHandler.RequireIssue = opts.IssueRequired
approversHandler.RequireIssue = *opts.IssueRequired
approversHandler.ManuallyApproved = humanAddedApproved(ghc, log, pr.org, pr.repo, pr.number, hasApprovedLabel)

// Author implicitly approves their own PR if config allows it
Expand All @@ -451,7 +442,7 @@ func handle(log *logrus.Entry, ghc githubClient, repo approvers.Repo, githubConf
sort.SliceStable(comments, func(i, j int) bool {
return comments[i].CreatedAt.Before(comments[j].CreatedAt)
})
approveComments := filterComments(comments, approvalMatcher(botUserChecker, opts.LgtmActsAsApprove, opts.ConsiderReviewState()))
approveComments := filterComments(comments, approvalMatcher(botUserChecker, *opts.LgtmActsAsApprove, opts.ConsiderReviewState()))
addApprovers(&approversHandler, approveComments, pr.author, opts.ConsiderReviewState())
log.WithField("duration", time.Since(start).String()).Debug("Completed filtering approval comments in handle")

Expand Down
55 changes: 14 additions & 41 deletions prow/plugins/approve/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,7 @@ func TestPluginConfig(t *testing.T) {
}
pa.Set(np)

orgs := map[string]bool{}
repos := map[string]bool{}
for _, config := range pa.Config().Approve {
for _, entry := range config.Repos {
if strings.Contains(entry, "/") {
if repos[entry] {
t.Errorf("The repo %q is duplicated in the 'approve' plugin configuration.", entry)
}
repos[entry] = true
} else {
if orgs[entry] {
t.Errorf("The org %q is duplicated in the 'approve' plugin configuration.", entry)
}
orgs[entry] = true
}
}
}
// no need to check for duplicates as the ConfigTree uses maps
}

func newTestComment(user, body string) github.IssueComment {
Expand Down Expand Up @@ -1281,10 +1265,9 @@ Approvers can cancel approval by writing ` + "`/approve cancel`" + ` in a commen
LinkURL: test.githubLinkURL,
},
&plugins.Approve{
Repos: []string{"org/repo"},
RequireSelfApproval: &rsa,
IssueRequired: test.needsIssue,
LgtmActsAsApprove: test.lgtmActsAsApprove,
IssueRequired: &test.needsIssue,
LgtmActsAsApprove: &test.lgtmActsAsApprove,
IgnoreReviewState: &irs,
CommandHelpLink: "https://go.k8s.io/bot-commands",
PrProcessLink: "https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process",
Expand Down Expand Up @@ -1560,17 +1543,14 @@ func TestHandleGenericComment(t *testing.T) {
Host: "github.com",
},
}
config := &plugins.Configuration{}
config.Approve = append(config.Approve, plugins.Approve{
Repos: []string{test.commentEvent.Repo.Owner.Login},
LgtmActsAsApprove: test.lgtmActsAsApprove,
})
err := handleGenericComment(
logrus.WithField("plugin", "approve"),
fghc,
fakeOwnersClient{},
githubConfig,
config,
&plugins.Approve{
LgtmActsAsApprove: &test.lgtmActsAsApprove,
},
&test.commentEvent,
)

Expand Down Expand Up @@ -1779,19 +1759,16 @@ func TestHandleReview(t *testing.T) {
Host: "github.com",
},
}
config := &plugins.Configuration{}
irs := !test.reviewActsAsApprove
config.Approve = append(config.Approve, plugins.Approve{
Repos: []string{test.reviewEvent.Repo.Owner.Login},
LgtmActsAsApprove: test.lgtmActsAsApprove,
IgnoreReviewState: &irs,
})
err := handleReview(
logrus.WithField("plugin", "approve"),
fghc,
fakeOwnersClient{},
githubConfig,
config,
&plugins.Approve{
LgtmActsAsApprove: &test.lgtmActsAsApprove,
IgnoreReviewState: &irs,
},
&test.reviewEvent,
)

Expand Down Expand Up @@ -1935,7 +1912,7 @@ func TestHandlePullRequest(t *testing.T) {
Host: "github.com",
},
},
&plugins.Configuration{},
&plugins.Approve{},
&test.prEvent,
)

Expand Down Expand Up @@ -1977,13 +1954,9 @@ func TestHelpProvider(t *testing.T) {
{
name: "All configs enabled",
config: &plugins.Configuration{
Approve: []plugins.Approve{
{
Repos: []string{"org2/repo"},
IssueRequired: true,
RequireSelfApproval: &[]bool{true}[0],
LgtmActsAsApprove: true,
IgnoreReviewState: &[]bool{true}[0],
Approve: plugins.ConfigTree[plugins.Approve]{
Orgs: map[string]plugins.Org[plugins.Approve]{
"org2": {},
},
},
},
Expand Down
Loading