Skip to main content

Setting code quality thresholds for pull requests

Create a Code Quality gate for pull requests to increase the quality of code merged into your repository.

谁可以使用此功能?

具有管理员角色的存储库所有者、组织所有者、安全管理员和用户

GitHub Code Quality 可用于 GitHub Team 或 GitHub Enterprise Cloud 上的组织拥有的存储库

注意

GitHub Code Quality 当前正处于 公共预览版 阶段,并可能会发生变化。 在 公共预览版期间,Code Quality 不会计费,尽管 Code Quality 扫描将使用 GitHub Actions 分钟。

Introduction

You can block pull requests that don't meet your code quality standards by adding the Require code quality results branch rule to a ruleset and specifying the severity level you require. If a pull request doesn't reach this threshold, it can't be merged.

Prerequisites

注意

The threshold will have an impact only if the repository has code in one or more of the supported languages, see Enabling GitHub Code Quality.

Confirming Code Quality runs successfully on pull requests

Before you add or update a ruleset to include a threshold for Code Quality, confirm that the 代码质量 workflow is running and reporting results back to pull requests. Otherwise, the ruleset could block the merging of all pull requests.

  1. Open a recent pull request and scroll to the "Checks" summary at the bottom of the pull request.
  2. Confirm that the "CodeQL - 代码质量" check ran successfully and reported its status.

For more information, see 基于 CodeQL 的代码质量分析.

Adding or updating a ruleset to include Code Quality

  1. Navigate to the "Settings" tab of your repository.
  2. In the left sidebar, under "Code and automation", expand Rules, then click Rulesets.
  3. If you don't already have a ruleset to protect your default branch, expand New ruleset and click New branch ruleset. Alternatively, open your existing ruleset for the default branch and move to step 5.
  4. If you are creating a new ruleset:
    • Define a name for the ruleset.
    • Set the "Enforcement status" to "Active."
    • Under "Target branches" add a target of "Include default branch."
  5. Under "Branch rules", enable "Require code quality results".
  6. Set "Severity" to define the lowest severity of code quality results that must be resolved before a pull request can be merged into the default branch. For example:
    • Set "Errors" to block pull requests with unresolved code quality errors being merged.
    • Set "Warnings and higher" to block pull requests with unresolved code quality warnings or errors being merged.
    • Set "Notes and higher" to block pull requests with unresolved code quality notes, warnings or errors being merged.
    • Set "All" to block pull requests with any unresolved code quality results being merged.
  7. When you have finished defining or editing the ruleset, click Create or Save changes.

Next steps

Learn how GitHub Code Quality works on pull requests to prevent code quality issues from reaching your default branch. See Fixing code quality findings before merging your pull request.