Skip to content

Commit

Permalink
refactor: merge and fail check run messages (#899)
Browse files Browse the repository at this point in the history
Merged by Reviewpad
  • Loading branch information
zolamk committed May 22, 2023
1 parent 8562dbf commit 36dfadf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion plugins/aladino/actions/failCheckStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func failCheckStatusCode(e aladino.Env, args []aladino.Value) error {
Status: github.String("completed"),
Conclusion: github.String("failure"),
Output: &github.CheckRunOutput{
Title: github.String("Reviewpad policy failed"),
Title: github.String("Reviewpad failed"),
Summary: github.String(summary.String()),
},
})
Expand Down
32 changes: 22 additions & 10 deletions plugins/aladino/actions/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func Merge() *aladino.BuiltInAction {

func mergeCode(e aladino.Env, args []aladino.Value) error {
t := e.GetTarget().(*target.PullRequestTarget)
targetEntity := e.GetTarget().GetTargetEntity()
log := e.GetLogger().WithField("builtin", "merge")

if t.PullRequest.Status != pbc.PullRequestStatus_OPEN || t.PullRequest.IsDraft {
Expand All @@ -43,28 +42,41 @@ func mergeCode(e aladino.Env, args []aladino.Value) error {

if e.GetCheckRunID() != nil {
e.SetCheckRunConclusion("success")
_, _, err := e.GetGithubClient().GetClientREST().Checks.UpdateCheckRun(e.GetCtx(), targetEntity.Owner, targetEntity.Repo, *e.GetCheckRunID(), github.UpdateCheckRunOptions{
Name: "reviewpad",
Status: github.String("completed"),
Conclusion: github.String("success"),
Output: &github.CheckRunOutput{
Title: github.String("Reviewpad about to merge"),
Summary: github.String("Reviewpad is about to merge this pull request."),
},
})
err := updateCheckRunWithSummary(e, "Reviewpad is about to merge this pull request")
if err != nil {
return err
}
}

mergeErr := t.Merge(mergeMethod)
if mergeErr != nil {
if e.GetCheckRunID() != nil {
err := updateCheckRunWithSummary(e, "The merge cannot be completed due to non-compliance with certain GitHub branch protection rules")
if err != nil {
return err
}
}

e.GetLogger().WithError(mergeErr).Warnln("failed to merge pull request")
}

return nil
}

func updateCheckRunWithSummary(e aladino.Env, summary string) error {
targetEntity := e.GetTarget().GetTargetEntity()
_, _, err := e.GetGithubClient().GetClientREST().Checks.UpdateCheckRun(e.GetCtx(), targetEntity.Owner, targetEntity.Repo, *e.GetCheckRunID(), github.UpdateCheckRunOptions{
Name: "reviewpad",
Status: github.String("completed"),
Conclusion: github.String("success"),
Output: &github.CheckRunOutput{
Title: github.String("Reviewpad about to merge"),
Summary: github.String(summary),
},
})
return err
}

func parseMergeMethod(args []aladino.Value) (string, error) {
if len(args) == 0 {
return "merge", nil
Expand Down

0 comments on commit 36dfadf

Please sign in to comment.