diff --git a/plugins/aladino/actions/review.go b/plugins/aladino/actions/review.go index cf852e265..154796478 100644 --- a/plugins/aladino/actions/review.go +++ b/plugins/aladino/actions/review.go @@ -61,19 +61,21 @@ func reviewCode(e aladino.Env, args []aladino.Value) error { } if latestReview != nil { - // The last push was made before the last review so a new review is not needed - if lastPushDate.Before(*latestReview.SubmittedAt) { - log.Infof("skipping review because there were no updates since the last review") - return nil - } - latestReviewEvent, err := mapReviewStateToEvent(latestReview.State) if err != nil { return err } + // If the latest review is the same as the one we want to create, and the last push date is before the latest review + // then we skip the review creation. + if latestReviewEvent == reviewEvent && latestReview.Body == reviewBody && lastPushDate.Before(*latestReview.SubmittedAt) { + log.Infof("skipping review because there were no updates since the last review") + return nil + } + log.Infof("latest review from %v is %v with body %v", authenticatedUserLogin, latestReviewEvent, latestReview.Body) } + log.Infof("creating review %v with body %v", reviewEvent, reviewBody) return t.Review(reviewEvent, reviewBody) diff --git a/plugins/aladino/actions/review_test.go b/plugins/aladino/actions/review_test.go index 181e98c39..b0025efa3 100644 --- a/plugins/aladino/actions/review_test.go +++ b/plugins/aladino/actions/review_test.go @@ -536,6 +536,46 @@ func TestReview(t *testing.T) { inputReviewBody: "test", wantErr: fmt.Errorf("review: unsupported review state NOT_SUPPORTED"), }, + "when latest review is the same as the one we want to create": { + clientOptions: []mock.MockBackendOption{}, + mockedLatestReviewFromReviewerGQLQueryBody: `{ + "data": { + "repository": { + "pullRequest": { + "reviews": { + "nodes": [{ + "author": { + "login": "test" + }, + "body": "test", + "state": "APPROVED", + "submittedAt": "2022-11-26T19:01:12Z" + }] + } + } + } + } + }`, + mockedLastPullRequestPushDateGQLQueryBody: `{ + "data": { + "repository": { + "pullRequest": { + "timelineItems": { + "nodes": [{ + "__typename": "PullRequestCommit", + "commit": { + "pushedDate": "2022-11-11T13:36:05Z", + "committedDate": "2022-11-11T13:36:05Z" + } + }] + } + } + } + } + }`, + inputReviewEvent: "APPROVE", + inputReviewBody: "test", + }, } for name, test := range tests { diff --git a/runner.go b/runner.go index 15f54264b..7288ac3af 100644 --- a/runner.go +++ b/runner.go @@ -335,9 +335,13 @@ func Run( command := eventDetails.Payload.(*github.IssueCommentEvent).GetComment().GetBody() if utils.IsReviewpadCommandDryRun(command) { return runReviewpadCommandDryRun(log, collector, gitHubClient, targetEntity, reviewpadFile, env) - } else { - return runReviewpadCommand(ctx, log, collector, gitHubClient, targetEntity, env, aladinoInterpreter, command) } + + if utils.IsReviewpadCommandRun(command) { + return runReviewpadFile(log, collector, gitHubClient, targetEntity, eventDetails, reviewpadFile, dryRun, safeMode, env, aladinoInterpreter) + } + + return runReviewpadCommand(ctx, log, collector, gitHubClient, targetEntity, env, aladinoInterpreter, command) } else { return runReviewpadFile(log, collector, gitHubClient, targetEntity, eventDetails, reviewpadFile, dryRun, safeMode, env, aladinoInterpreter) } diff --git a/utils/pr.go b/utils/pr.go index cbdec09f7..ad34a2117 100644 --- a/utils/pr.go +++ b/utils/pr.go @@ -25,3 +25,7 @@ func IsReviewpadCommand(eventDetails *handler.EventDetails) bool { func IsReviewpadCommandDryRun(command string) bool { return strings.TrimPrefix(command, "/reviewpad ") == "dry-run" } + +func IsReviewpadCommandRun(command string) bool { + return strings.TrimPrefix(command, "/reviewpad ") == "run" +} diff --git a/utils/pr_test.go b/utils/pr_test.go index f56db22a5..c1a00973b 100644 --- a/utils/pr_test.go +++ b/utils/pr_test.go @@ -120,3 +120,29 @@ func TestIsReviewPadCommand(t *testing.T) { }) } } + +func TestIsReviewpadCommandRun(t *testing.T) { + tests := map[string]struct { + command string + wantVal bool + }{ + "when command is empty": { + wantVal: false, + }, + "when command is not /reviewpad run": { + command: "/reviewpad assign-reviewers", + wantVal: false, + }, + "when command is /reviewpad run": { + command: "/reviewpad run", + wantVal: true, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + val := utils.IsReviewpadCommandRun(test.command) + assert.Equal(t, test.wantVal, val) + }) + } +}