Skip to main content

此版本的 GitHub Enterprise Server 将于以下日期停止服务 2026-04-09. 即使针对重大安全问题,也不会发布补丁。 为了获得更好的性能、更高的安全性和新功能,请升级到最新版本的 GitHub Enterprise。 如需升级帮助,请联系 GitHub Enterprise 支持

Set code scanning merge protection

Secure your codebase by blocking pull requests that fail code scanning checks.

谁可以使用此功能?

具有管理员角色的组织所有者、安全管理员和组织成员

Code scanning 可用于以下存储库类型:

  • GitHub.com 上的公共存储库
  • GitHub Team、GitHub Enterprise Cloud 或 GitHub Enterprise Server 上的组织拥有的存储库,已启用 GitHub Advanced Security

Creating a merge protection ruleset for a repository

  1. 在 GitHub 上,导航到存储库的主页面。

  2. 在仓库名称下,单击 “Settings”****。 如果看不到“设置”选项卡,请选择“”下拉菜单,然后单击“设置”。

    存储库标头的屏幕截图,其中显示了选项卡。 “设置”选项卡以深橙色边框突出显示。

  3. 在左侧边栏的“代码和自动化”下,单击“规则”,然后单击“规则集” 。

    存储库“设置”页边栏的屏幕截图。 “规则”子菜单已展开,“规则集”选项以橙色轮廓突出显示。

  4. Click New ruleset.

  5. To create a ruleset targeting branches, click New branch ruleset.

  6. 在“规则集名称”下,键入规则集的名称。

  7. (可选)若要更改默认强制实施状态,请单击 “Disabled”,并选择强制实施状态****。

  8. 在“分支保护”下,选择“要求 code scanning 结果

  9. 在“Required tools and alert thresholds”下,单击 “Add tool”****,然后从下拉列表中选择 code scanning 工具。 例如,“CodeQL”。

  10. code scanning 工具的名称旁边:

    • 单击“警报”,然后选择“”、“错误”、“错误和警告”或“全部”。
    • 单击“安全警报”,然后选择“”、“严重”、“高或更高”、“”或“全部”。

    “规则集”设置的“所需工具和警报阈值”部分的屏幕截图。

有关警报严重性和安全严重性级别的详细信息,请参阅“关于代码扫描警报”。

For more information about managing rulesets in a repository, see 管理存储库的规则集.

Creating a merge protection ruleset for all repositories in an organization

  1. 在 GitHub 的右上角,单击个人资料图片,然后单击“ Your organizations”****。

  2. 在组织旁边,单击“设置”。

  3. 在左边栏的“Code, planning, and automation”部分,单击“ Repository”,然后单击“Rulesets”********。

    组织的设置页面的屏幕截图。 在边栏中,标有“规则集”的链接以橙色轮廓突出显示。

  4. Click New ruleset.

  5. To create a ruleset targeting branches, click New branch ruleset.

  6. 在“规则集名称”下,键入规则集的名称。

  7. (可选)若要更改默认强制实施状态,请单击 “Disabled”,并选择强制实施状态****。

  8. 在“分支保护”下,选择“要求 code scanning 结果

  9. 在“Required tools and alert thresholds”下,单击 “Add tool”****,然后从下拉列表中选择 code scanning 工具。 例如,“CodeQL”。

  10. code scanning 工具的名称旁边:

    • 单击“警报”,然后选择“”、“错误”、“错误和警告”或“全部”。
    • 单击“安全警报”,然后选择“”、“严重”、“高或更高”、“”或“全部”。

    “规则集”设置的“所需工具和警报阈值”部分的屏幕截图。

有关警报严重性和安全严重性级别的详细信息,请参阅“关于代码扫描警报”。

For more information about managing rulesets for repositories in an organization, see 管理您组织存储库的规则集.

Creating a merge protection ruleset with the REST API

You can use the REST API to create a ruleset with the code_scanning rule, which allows you to define specific tools and set alert thresholds. For more information, see 规则的 REST API 终结点.