Skip to content

Commit

Permalink
feat: improve review creation and enable reviewpad run command (#724)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferreiratiago authored Feb 24, 2023
1 parent 893d923 commit 5878720
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 8 deletions.
14 changes: 8 additions & 6 deletions plugins/aladino/actions/review.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions plugins/aladino/actions/review_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions utils/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
26 changes: 26 additions & 0 deletions utils/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}

0 comments on commit 5878720

Please sign in to comment.