Skip to content

Commit

Permalink
Expose a utility to determine approval config for a repo
Browse files Browse the repository at this point in the history
Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
  • Loading branch information
stevekuznetsov committed Nov 7, 2019
1 parent 30114c2 commit 9a5e062
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
48 changes: 6 additions & 42 deletions prow/plugins/approve/approve.go
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/sirupsen/logrus"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/test-infra/prow/config"
"k8s.io/test-infra/prow/github"
"k8s.io/test-infra/prow/labels"
Expand Down Expand Up @@ -117,9 +116,9 @@ func helpProvider(config *plugins.Configuration, enabledRepos []string) (*plugin
var opts *plugins.Approve
switch len(parts) {
case 1:
opts = optionsForRepo(config, repo, "")
opts = config.ApproveFor(repo, "")
case 2:
opts = optionsForRepo(config, parts[0], parts[1])
opts = config.ApproveFor(parts[0], parts[1])
default:
return nil, fmt.Errorf("invalid repo in enabledRepos: %q", repo)
}
Expand Down Expand Up @@ -183,7 +182,7 @@ func handleGenericComment(log *logrus.Entry, ghc githubClient, oc ownersClient,
return err
}

opts := optionsForRepo(config, ce.Repo.Owner.Login, ce.Repo.Name)
opts := config.ApproveFor(ce.Repo.Owner.Login, ce.Repo.Name)
if !isApprovalCommand(botName, opts.LgtmActsAsApprove, &comment{Body: ce.Body, Author: ce.User.Login}) {
return nil
}
Expand Down Expand Up @@ -240,7 +239,7 @@ func handleReview(log *logrus.Entry, ghc githubClient, oc ownersClient, githubCo
return err
}

opts := optionsForRepo(config, re.Repo.Owner.Login, re.Repo.Name)
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
Expand All @@ -265,7 +264,7 @@ func handleReview(log *logrus.Entry, ghc githubClient, oc ownersClient, githubCo
ghc,
repo,
githubConfig,
optionsForRepo(config, re.Repo.Owner.Login, re.Repo.Name),
config.ApproveFor(re.Repo.Owner.Login, re.Repo.Name),
&state{
org: re.Repo.Owner.Login,
repo: re.Repo.Name,
Expand Down Expand Up @@ -317,7 +316,7 @@ func handlePullRequest(log *logrus.Entry, ghc githubClient, oc ownersClient, git
ghc,
repo,
githubConfig,
optionsForRepo(config, pre.Repo.Owner.Login, pre.Repo.Name),
config.ApproveFor(pre.Repo.Owner.Login, pre.Repo.Name),
&state{
org: pre.Repo.Owner.Login,
repo: pre.Repo.Name,
Expand Down Expand Up @@ -624,41 +623,6 @@ func addApprovers(approversHandler *approvers.Approvers, approveComments []*comm
}
}

// optionsForRepo gets the plugins.Approve struct that is applicable to the indicated repo.
func optionsForRepo(config *plugins.Configuration, org, repo string) *plugins.Approve {
fullName := fmt.Sprintf("%s/%s", org, repo)

a := func() *plugins.Approve {
// First search for repo config
for _, c := range config.Approve {
if !sets.NewString(c.Repos...).Has(fullName) {
continue
}
return &c
}

// If you don't find anything, loop again looking for an org config
for _, c := range config.Approve {
if !sets.NewString(c.Repos...).Has(org) {
continue
}
return &c
}

// Return an empty config, and use plugin defaults
return &plugins.Approve{}
}()
if a.DeprecatedImplicitSelfApprove == nil && a.RequireSelfApproval == nil && config.UseDeprecatedSelfApprove {
no := false
a.DeprecatedImplicitSelfApprove = &no
}
if a.DeprecatedReviewActsAsApprove == nil && a.IgnoreReviewState == nil && config.UseDeprecatedReviewApprove {
no := false
a.DeprecatedReviewActsAsApprove = &no
}
return a
}

type comment struct {
Body string
Author string
Expand Down
37 changes: 37 additions & 0 deletions prow/plugins/config.go
Expand Up @@ -686,6 +686,43 @@ func (r RequireMatchingLabel) Describe() string {
return str.String()
}

// ApproveFor finds the Approve for a repo, if one exists.
// Approval configuration can be listed for a repository
// or an organization.
func (c *Configuration) ApproveFor(org, repo string) *Approve {
fullName := fmt.Sprintf("%s/%s", org, repo)

a := func() *Approve {
// First search for repo config
for _, approve := range c.Approve {
if !sets.NewString(approve.Repos...).Has(fullName) {
continue
}
return &approve
}

// If you don't find anything, loop again looking for an org config
for _, approve := range c.Approve {
if !sets.NewString(approve.Repos...).Has(org) {
continue
}
return &approve
}

// Return an empty config, and use plugin defaults
return &Approve{}
}()
if a.DeprecatedImplicitSelfApprove == nil && a.RequireSelfApproval == nil && c.UseDeprecatedSelfApprove {
no := false
a.DeprecatedImplicitSelfApprove = &no
}
if a.DeprecatedReviewActsAsApprove == nil && a.IgnoreReviewState == nil && c.UseDeprecatedReviewApprove {
no := false
a.DeprecatedReviewActsAsApprove = &no
}
return a
}

// TriggerFor finds the Trigger for a repo, if one exists
// a trigger can be listed for the repo itself or for the
// owning organization
Expand Down

0 comments on commit 9a5e062

Please sign in to comment.