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

dont evaluate unless we need to #472

Merged

Conversation

devinburnette
Copy link
Contributor

closes #470

As mentioned in #470 there are certain situations where we could run into API limitations on the number of status checks posted to a SHA / context pair. This should cut down on some unnecessary API calls being made currently during evaluation.

Copy link
Member

@bluekeyes bluekeyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for proposing a fix! I think the comment editing edge case is the main thing to address, but I had some minor style suggests too.

@@ -100,6 +100,10 @@ func (h *IssueComment) Handle(ctx context.Context, eventType, deliveryID string,
return nil
}

if !h.affectsApproval(event.GetComment().GetBody(), fc.Config.ApprovalRules) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this misses an edge case with edited comments. The previous detectAndLogTampering call exits early if the edit was by the same user that authored the original comment. Since affectsApproval is only looking at the current body, if a user edits their own comment to remove an approval, I think this will cause us to skip evaluating the PR (only the old body affects approval, and we don't check that.)

To catch that case, I think we need to look at the previous content for edited events as well, possibly assuming that a comment affects approval if the old content is missing (e.g. in #379, where a GHE bug removed this field.)

As a general rule, I want to make sure this type of optimization will over-evaluate if there are errors or uncertainty, rather than under-evaluate.


func (h *PullRequestReview) affectsApproval(reviewState string, rules []*approval.Rule) bool {
for _, rule := range rules {
if reviewState == string(rule.Options.GetMethods().GithubReviewState) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it might be better to cast the parameter before calling this function, rather than casting the rule value

if !h.affectsApproval(event.GetReview().GetState(), fc.Config.ApprovalRules) {
return nil
}

return h.Evaluate(ctx, installationID, common.TriggerReview, pull.Locator{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about all the boilerplate you have to copy to get access to the config prior to calling Evaluate. But now that we have a FetchedConfig, this should probably call ValidateFetchedConfig and EvaluateFetchedConfig instead, since Evaluate repeats the client creation and config fetching.

for _, rule := range rules {
if rule.Options.GetMethods().CommentMatches(actualComment) {
if rule.Options.GetMethods().CommentMatches(originalBody) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about this? this will look at all events but only run them through evaluation if the current or previous matches a rule pattern.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is still missing a case: when the action is edited we need to test both the original body and the new body to catch events where an approval was removed or added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, i see what you mean now.

case "deleted":
originalBody = event.GetComment().GetBody()

default:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tried to dry this up a bit.

Copy link
Member

@bluekeyes bluekeyes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started on some refactoring that I think will make this a bit cleaner (and will also create merge conflicts). If you don't mind, I can take over this PR once my change is in and get it updated and merged.

for _, rule := range rules {
if rule.Options.GetMethods().CommentMatches(actualComment) {
if rule.Options.GetMethods().CommentMatches(originalBody) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is still missing a case: when the action is edited we need to test both the original body and the new body to catch events where an approval was removed or added.

Comment on lines 49 to 57
client, err := h.NewInstallationClient(installationID)
if err != nil {
return err
}

v4client, err := h.NewInstallationV4Client(installationID)
if err != nil {
return err
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #476, I did some refactoring to reduce this boiler plate

server/handler/pull_request_review.go Show resolved Hide resolved
Also add some log messages if we're skipping due to lack of impact
@bluekeyes bluekeyes dismissed their stale review September 28, 2022 23:48

Changes implemented

@bluekeyes bluekeyes requested a review from a team September 28, 2022 23:51
@bluekeyes bluekeyes merged commit 99d45bc into palantir:develop Sep 29, 2022
@devinburnette devinburnette deleted the devin-dont-evaluate-everytime branch September 29, 2022 02:16
@devinburnette
Copy link
Contributor Author

lgtm, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

maximum number of statuses for SHA and context
3 participants