How do I block merging pull requests using Codacy as a quality gate?#
You can block merging pull requests until they pass the Codacy quality gate. This ensures the quality of the changes to your codebase, such as to prevent introducing security issues or adding code that isn't covered by tests.
To block merging pull requests that don't meet the quality standards of your team you must complete these main steps:
- Adding coverage to your repository (optional)
- Reviewing and adjusting the Codacy analysis settings
- Deciding which pull requests fail the Codacy quality gate
- Enabling the Codacy pull request status checks
- Configuring your Git provider to block merging pull requests
The next sections include detailed instructions on how to complete each step.
1. Adding coverage to your repository (optional)#
If you want to use code coverage to block merging pull requests that don't meet your quality standards, make sure that you add coverage to your repository.
It's important that you set up coverage beforehand because Codacy can only report the coverage status for pull requests after receiving reports for the last commits on both the pull request branch and the target branch.
2. Reviewing and adjusting the Codacy analysis settings#
We recommend that you spend some time reviewing the Codacy analysis settings to focus on the tools and code patterns that bring more value to your team. Importantly, this eliminates the risk of having false positives block the work of your team once you start using Codacy as a quality gate.
Ignore any files and directories that aren't relevant for the Codacy analysis, such as generated code or any third-party libraries included in your repositories.
Configure the tools and code patterns that Codacy uses to analyze your repository.
To ensure that multiple repositories consistently follow the same global tool and code pattern configurations, use an organization coding standard instead.
3. Configuring the quality gate rules#
Review and adjust the quality settings of your repository to decide which pull requests should fail the Codacy quality gate.
Depending on factors such as the current code quality of your repository and the maturity of your team practices, consider the balance between implementing stricter quality gates and the possibility of delaying or blocking the development progress.
The default values of the quality gate rules are designed to help maintain the current code quality of your repository. In particular, the default value for the coverage rule might be demanding.
Because of this, Codacy generally recommends that on a first stage you configure rules that focus on stopping new critical issues from entering your code base, such as:
- High severity issues
- Security issues
- Considerable drops in code coverage
If you want to use code coverage to block merging pull requests that don't meet your standards, make sure that you enable the rule Diff coverage is under or Coverage variation is under. This is required for Codacy to report the coverage status directly on your pull requests.
4. Enabling the Codacy pull request status checks#
Set up your repository so that Codacy reports the results of the analysis directly on your pull requests as status checks:
- Enable the Git provider integration
- Enable the option Status checks (GitHub) or Pull request status (GitLab and Bitbucket)
5. Configuring your Git provider to block merging pull requests#
At this stage we recommend that you:
- Validate that Codacy is reporting the intended status on your pull requests
- Double check you repository's tool and code pattern settings and quality gate settings
This is important to eliminate any false positives that could inadvertently block the work of your team.
Finally, follow the instructions from your Git provider to block merging pull requests if they don't pass the Codacy status check:
- GitHub: Set Codacy as a required status check
- GitLab: Only allow merge requests to be merged if the pipeline succeeds
- Bitbucket: configure Bitbucket to prevent a merge with unresolved merge checks
Share your feedback 📢
Did this page help you?
Thanks for the feedback! Is there anything else you'd like to tell us about this page?
255 characters left
We're sorry to hear that. Please let us know what we can improve:
255 characters left
Alternatively, you can create a more detailed issue on our GitHub repository.
Thanks for helping improve the Codacy documentation.
Edit this page on GitHub if you notice something wrong or missing.
If you have a question or need help please contact firstname.lastname@example.org.