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

feat: merge and fail should update check run status #876

Merged
merged 25 commits into from
May 18, 2023

Conversation

zolamk
Copy link
Contributor

@zolamk zolamk commented May 14, 2023

Description

In order to support merge policies the $merge and $fail built-ins need to be able to set the status of the check run, this pull request updates reviewpad so that it receives a check run id that the built-ins can use to control the status of the check.

🤖 Generated by Copilot at b5f2400

This pull request adds support for GitHub check runs for reviewpad policies. It modifies the lang/aladino package and its plugins to handle check run IDs, conclusions, and outputs in the interpreter and the built-in functions. It also updates the runner.go file and the engine package to pass and return the check run information. It adjusts the test files and the cli package to reflect the changes in the function signatures.

Closes #874

Code review and merge strategy

Ask: this pull request requires a code review before merge

How

🤖 Generated by Copilot at b5f2400

  • Add support for GitHub check runs for reviewpad policies (link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link)
  • Add a new parameter checkRunId to the Run function signature in the runner.go file and pass it to the runReviewpadFile and runReviewpadCommand functions (link, link)
  • Add a new parameter checkRunId to the runReviewpadFile function signature in the runner.go file and pass it to the NewInterpreter function in the lang/aladino package (link)
  • Add a new parameter checkRunId to the runReviewpadCommand function signature in the runner.go file and pass it to the runReviewpadCommandDryRun function (link)
  • Add a new parameter checkRunId to the runReviewpadCommandDryRun function signature in the runner.go file and pass it to the reviewpad.Run function in the cli package (link)
  • Add a new parameter checkRunId to the reviewpad.Run function signature in the cli package and pass it to the NewInterpreter function in the engine package (link)
  • Add a new parameter checkRunId to the NewInterpreter function signature in the engine package and pass it to the NewEvalEnv function in the lang/aladino package (link)
  • Add a new parameter checkRunId to the NewEvalEnv function signature in the lang/aladino package and assign it to the CheckRunID field of the BaseEnv struct (link, link)
  • Add two new fields CheckRunID and CheckRunConclusion to the BaseEnv struct in the lang/aladino package to store the check run ID and conclusion that correspond to the aladino interpreter's execution (link)
  • Add three new methods GetCheckRunID, SetCheckRunConclusion, and GetCheckRunConclusion to the Env interface in the lang/aladino package to access and modify the check run ID and conclusion (link)
  • Implement the GetCheckRunID, SetCheckRunConclusion, and GetCheckRunConclusion methods for the BaseEnv struct in the lang/aladino package (link, link)
  • Implement the GetCheckRunConclusion method for the Interpreter struct in the lang/aladino package to return the check run conclusion from the aladino environment (link)
  • Add a new method GetCheckRunConclusion to the Interpreter interface in the engine package to return the conclusion of the check run that corresponds to the interpreter's execution (link)
  • Modify the runReviewpadFile function in the runner.go file to return the check run conclusion from the aladino interpreter as the third value (link, link, link, link, link, link, link)
  • Modify the runReviewpadCommandDryRun function in the runner.go file to return an empty string as the third value, which corresponds to the check run conclusion (link, link, link)
  • Modify the runReviewpadCommand function in the runner.go file to return an empty string as the third value, which corresponds to the check run conclusion (link, link, link)
  • Remove the code that checks for fatal errors in the aladino environment and stops the execution in the Run method of the Interpreter struct in the lang/aladino package, as the fatal errors are handled by the fail built-in function (link)
  • Modify the failCode function in the plugins/aladino/actions/fail.go file to set the check run conclusion to "failure" and update the check run status and output with the fatal messages from the aladino environment, if the check run ID is not nil (link)
  • Modify the mergeCode function in the plugins/aladino/actions/merge.go file to set the check run conclusion to "success" and update the check run status and output with a success message, if the check run ID is not nil and there are no fatal messages from the aladino environment (link)
  • Add new imports to the plugins/aladino/actions/fail.go and plugins/aladino/actions/merge.go files for the new logic that updates the check run status and output (link, link)
  • Add a new variable targetEntity to the mergeCode function in the plugins/aladino/actions/merge.go file to hold the target entity information for the check run output (link)
  • Add a new argument nil to the NewInterpreter function calls in the test functions in the lang/aladino package, which corresponds to the checkRunID parameter that was added to the NewInterpreter function signature (link, link, link)
  • Add a new argument nil to the NewEvalEnv function calls in the test functions in the lang/aladino package, which corresponds to the checkRunID parameter that was added to the NewEvalEnv function signature (link, link, link, link)
  • Add a new argument nil to the NewInterpreter function call in the mockEnvWith function in the lang/aladino package, which corresponds to the checkRunID parameter that was added to the NewInterpreter function signature (link)
  • Add a new argument nil to the mockAladinoInterpreter function call in the engine package, which corresponds to the checkRunId parameter that was added to the NewInterpreter function signature (link)

@reviewpad reviewpad bot added the ask Pull request requires a code review before merge label May 14, 2023
@zolamk zolamk marked this pull request as ready for review May 15, 2023 09:16
@reviewpad reviewpad bot added large large pull request critical Modifications to critical changes plugins Modifications to the plugins directory labels May 15, 2023
@reviewpad reviewpad bot requested a review from shay2025 May 15, 2023 09:17
Copy link
Member

@ferreiratiago ferreiratiago 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 this @zolamk

Can you please share what was the rationale for providing Reviewpad with the check id instead of the check itself?

My understanding was to provide the check itself as an interface that could provide the required methods to update it. The proposed way binds the check to GitHub and disables the ability to easily work for different code hosts.

What is your understanding of this?

@reviewpad reviewpad bot added requires-author-attention Pull request requires the author to take action waiting-approval Pull request is waiting for approval labels May 15, 2023
@zolamk
Copy link
Contributor Author

zolamk commented May 16, 2023

Thanks for this @zolamk

Can you please share what was the rationale for providing Reviewpad with the check id instead of the check itself?

My understanding was to provide the check itself as an interface that could provide the required methods to update it. The proposed way binds the check to GitHub and disables the ability to easily work for different code hosts.

What is your understanding of this?

The check run id is an integer both in github and gitlabs case, that's why I am passing the id to reviewpad and then the host service could implement a set of calls to work with check runs

@zolamk zolamk added the do-not-merge Pull request cannot be merged label May 16, 2023
Copy link

@reviewpad reviewpad bot left a comment

Choose a reason for hiding this comment

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

Some checks are failing. Please fix them.

@zolamk zolamk removed the do-not-merge Pull request cannot be merged label May 16, 2023
Status: github.String("completed"),
Conclusion: github.String("success"),
Output: &github.CheckRunOutput{
Title: github.String("Reviewpad about to merge"),
Copy link
Contributor

Choose a reason for hiding this comment

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

What about Reviewpad Merge Notification?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@fmallet what do you think the title of the check run output should when the merge built-in is about to merge the pull request

plugins/aladino/actions/merge.go Show resolved Hide resolved
@reviewpad
Copy link

reviewpad bot commented May 16, 2023

Pull request is not up to date with the base branch. Reviewpad will rebase it. After the rebase, please trigger Reviewpad by running the command /reviewpad run in a comment.

@zolamk
Copy link
Contributor Author

zolamk commented May 16, 2023

/reviewpad run

@zolamk zolamk added the do-not-merge Pull request cannot be merged label May 17, 2023
@zolamk zolamk marked this pull request as draft May 17, 2023 08:44
@marcelosousa
Copy link
Member

/reviewpad summarize

@reviewpad
Copy link

reviewpad bot commented May 17, 2023

/reviewpad summarize

@marcelosousa an error occurred running your command

Errors:

  • summarize failed - please contact us on Discord

@zolamk zolamk marked this pull request as ready for review May 17, 2023 14:10
Copy link

@reviewpad reviewpad bot left a comment

Choose a reason for hiding this comment

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

The pull request has git conflicts. Please fix them.

Copy link

@reviewpad reviewpad bot left a comment

Choose a reason for hiding this comment

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

Pull request is not rebaseable. Please rebase it manually.

Copy link

@reviewpad reviewpad bot left a comment

Choose a reason for hiding this comment

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

Pull request is not rebaseable. Please rebase it manually.

@reviewpad
Copy link

reviewpad bot commented May 18, 2023

Pull request is not up to date with the base branch. Reviewpad will rebase it. After the rebase, please trigger Reviewpad by running the command /reviewpad run in a comment.

1 similar comment
@reviewpad
Copy link

reviewpad bot commented May 18, 2023

Pull request is not up to date with the base branch. Reviewpad will rebase it. After the rebase, please trigger Reviewpad by running the command /reviewpad run in a comment.

Comment on lines 31 to 50
if e.GetCheckRunID() != nil {
e.SetCheckRunConclusion("failure")

summary := strings.Builder{}

for _, msg := range e.GetBuiltInsReportedMessages()[aladino.SEVERITY_FAIL] {
summary.WriteString(fmt.Sprintf("- %s\n", msg))
}

_, _, 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("failure"),
Output: &github.CheckRunOutput{
Title: github.String("Reviewpad policy failed"),
Summary: github.String(summary.String()),
},
})
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.

/reviewpad explain

Copy link

Choose a reason for hiding this comment

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

This code checks if the current environment has a non-nil CheckRunID. If it does, it sets the check run conclusion to "failure" and constructs a summary of all the reported messages with a severity of "fail." Then, using the available GithubClient, it updates the check run with the required info, such as name, status, conclusion, and output. This section is responsible for updating the check run on the Github platform to reflect the policy failure and its reason.

@@ -23,6 +24,7 @@ func Merge() *aladino.BuiltInAction {

func mergeCode(e aladino.Env, args []aladino.Value) error {
Copy link
Member

Choose a reason for hiding this comment

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

/reviewpad explain

Copy link

Choose a reason for hiding this comment

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

This line defines a function called mergeCode with two parameters, an aladino.Env object named e and a slice of aladino.Value objects named args. The function returns an error value.

The mergeCode function is responsible for performing a merge in the context of a built-in action handler in the Reviewpad application. It fetches the target entity and checks the status of the pull request. If it's not open or is a draft, the function logs a message and returns early with a nil error. The function then parses the merge method from the provided arguments and checks if there are any failures reported by built-in checks. If all conditions are met, the function updates the check-run with a success status and proceeds to perform the merge using the desired merge method, logging a warning message if the merge fails.

}

defer config.CleanupPluginConfig()

aladinoInterpreter, err := aladino.NewInterpreter(ctx, log, dryRun, gitHubClient, codeHostClient, collector, targetEntity, eventDetails.Payload, plugins_aladino.PluginBuiltInsWithConfig(config))
aladinoInterpreter, err := aladino.NewInterpreter(ctx, log, dryRun, gitHubClient, codeHostClient, collector, targetEntity, eventDetails.Payload, plugins_aladino.PluginBuiltInsWithConfig(config), checkRunId)
Copy link
Member

Choose a reason for hiding this comment

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

/reviewpad explain

Copy link

Choose a reason for hiding this comment

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

This line creates a new instance of the Aladino interpreter by calling the aladino.NewInterpreter function and passing in several arguments which include the context, logger, dry-run flag, GitHub client, code host client, collector, target entity, payload from the event details, built-ins from the plugin configuration, and the check run ID. The Aladino interpreter is responsible for executing a given program and reporting the results.

@zolamk
Copy link
Contributor Author

zolamk commented May 18, 2023

/reviewpad run

@reviewpad reviewpad deleted a comment from reviewpad bot May 18, 2023
@reviewpad reviewpad deleted a comment from reviewpad bot May 18, 2023
@ferreiratiago
Copy link
Member

/reviewpad run

@reviewpad reviewpad bot removed the waiting-approval Pull request is waiting for approval label May 18, 2023
@reviewpad
Copy link

reviewpad bot commented May 18, 2023

The merge is blocked by the label do-not-merge.

@zolamk zolamk removed the do-not-merge Pull request cannot be merged label May 18, 2023
Copy link

@reviewpad reviewpad bot left a comment

Choose a reason for hiding this comment

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

Pull request is ready to be merged. Reviewpad will merge it for you.

@reviewpad reviewpad bot merged commit 77f1653 into main May 18, 2023
@reviewpad reviewpad bot deleted the feat/merge-fail-check branch May 18, 2023 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ask Pull request requires a code review before merge critical Modifications to critical changes large large pull request plugins Modifications to the plugins directory
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$merge and $fail built-ins should update check run status
4 participants