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 #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 pkg/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 @@ -433,7 +424,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 @@ -452,7 +443,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
37 changes: 13 additions & 24 deletions pkg/plugins/approve/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1261,10 +1261,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 @@ -1548,17 +1547,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 @@ -1767,19 +1763,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 @@ -1923,7 +1916,7 @@ func TestHandlePullRequest(t *testing.T) {
Host: "github.com",
},
},
&plugins.Configuration{},
&plugins.Approve{},
&test.prEvent,
)

Expand Down Expand Up @@ -1965,13 +1958,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