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

Fix evaluation of disapproval reviews and comments #567

Merged
merged 1 commit into from
Apr 19, 2023
Merged
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
22 changes: 15 additions & 7 deletions server/handler/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

"github.com/google/go-github/v50/github"
"github.com/palantir/go-githubapp/githubapp"
"github.com/palantir/policy-bot/policy/approval"
"github.com/palantir/policy-bot/policy"
"github.com/palantir/policy-bot/policy/common"
"github.com/palantir/policy-bot/pull"
"github.com/pkg/errors"
Expand Down Expand Up @@ -92,7 +92,7 @@ func (h *IssueComment) Handle(ctx context.Context, eventType, deliveryID string,
return nil
}

if !h.affectsApproval(event, evalCtx.Config.Config.ApprovalRules) {
if !h.affectsApproval(event, evalCtx.Config.Config) {
logger.Debug().Msg("Skipping evaluation because this comment does not impact approval")
return nil
}
Expand All @@ -119,7 +119,7 @@ func (h *IssueComment) detectAndLogTampering(ctx context.Context, evalCtx *EvalC
return false
}

if h.affectsApproval(event, evalCtx.Config.Config.ApprovalRules) {
if h.affectsApproval(event, evalCtx.Config.Config) {
msg := fmt.Sprintf("Entity %s edited approval comment by %s", eventAuthor, commentAuthor)
logger.Warn().Str(LogKeyAudit, "issue_comment").Msg(msg)

Expand All @@ -131,7 +131,7 @@ func (h *IssueComment) detectAndLogTampering(ctx context.Context, evalCtx *EvalC
return true
}

func (h *IssueComment) affectsApproval(event github.IssueCommentEvent, rules []*approval.Rule) bool {
func (h *IssueComment) affectsApproval(event github.IssueCommentEvent, config *policy.Config) bool {
var body, originalBody string
switch event.GetAction() {
case "edited":
Expand All @@ -142,9 +142,17 @@ func (h *IssueComment) affectsApproval(event github.IssueCommentEvent, rules []*
originalBody = body
}

for _, rule := range rules {
methods := rule.Options.GetMethods()
if methods.CommentMatches(body) || (body != originalBody && methods.CommentMatches(originalBody)) {
var methods []*common.Methods
for _, rule := range config.ApprovalRules {
methods = append(methods, rule.Options.GetMethods())
}
if disapproval := config.Policy.Disapproval; disapproval != nil {
methods = append(methods, disapproval.Options.GetDisapproveMethods())
methods = append(methods, disapproval.Options.GetRevokeMethods())
}

for _, m := range methods {
if m.CommentMatches(body) || (body != originalBody && m.CommentMatches(originalBody)) {
return true
}
}
Expand Down
19 changes: 14 additions & 5 deletions server/handler/pull_request_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

"github.com/google/go-github/v50/github"
"github.com/palantir/go-githubapp/githubapp"
"github.com/palantir/policy-bot/policy/approval"
"github.com/palantir/policy-bot/policy"
"github.com/palantir/policy-bot/policy/common"
"github.com/palantir/policy-bot/pull"
"github.com/pkg/errors"
Expand Down Expand Up @@ -72,7 +72,7 @@ func (h *PullRequestReview) Handle(ctx context.Context, eventType, deliveryID st
}

reviewState := pull.ReviewState(event.GetReview().GetState())
if !h.affectsApproval(reviewState, evalCtx.Config.Config.ApprovalRules) {
if !h.affectsApproval(reviewState, evalCtx.Config.Config) {
logger.Debug().Msg("Skipping evaluation because this review does not impact approval")
return nil
}
Expand All @@ -86,9 +86,18 @@ func (h *PullRequestReview) Handle(ctx context.Context, eventType, deliveryID st
return nil
}

func (h *PullRequestReview) affectsApproval(reviewState pull.ReviewState, rules []*approval.Rule) bool {
for _, rule := range rules {
if reviewState == rule.Options.GetMethods().GithubReviewState {
func (h *PullRequestReview) affectsApproval(reviewState pull.ReviewState, config *policy.Config) bool {
states := make(map[pull.ReviewState]struct{})
for _, rule := range config.ApprovalRules {
states[rule.Options.GetMethods().GithubReviewState] = struct{}{}
}
if disapproval := config.Policy.Disapproval; disapproval != nil {
states[disapproval.Options.GetDisapproveMethods().GithubReviewState] = struct{}{}
states[disapproval.Options.GetRevokeMethods().GithubReviewState] = struct{}{}
}

for state := range states {
if state == reviewState {
return true
}
}
Expand Down