此版本的 GitHub Enterprise 已停止服务 2021-06-09. 即使针对重大安全问题,也不会发布补丁。 要获得更好的性能、改进的安全性和新功能,请升级到 GitHub Enterprise 的最新版本。 如需升级方面的帮助,请联系 GitHub Enterprise 支持

关于代码所有者

您可以使用 CODEOWNERS 文件定义负责仓库代码的个人或团队。

您可以在具有 GitHub Free 和组织的 GitHub Free 的公共仓库,以及具有 GitHub Pro、GitHub Team、GitHub Enterprise Cloud、 和 GitHub Enterprise Server 的公共和私有仓库中定义代码所有者。

具有管理员或所有者权限的人员可以在仓库中创建 CODEOWNERS 文件。

您选择作为代码所有者的人员必须具有仓库的写入权限。 当代码所有者是团队时,该团队必须拥有写入权限,即使团队的所有成员都已经直接通过组织成员资格或通过另一个团队成员资格直接拥有写入权限。

关于代码所有者

当有人打开修改代码的拉取请求时,将自动请求代码所有者进行审查。 不会自动请求代码所有者审查拉取请求草稿。 有关拉取请求草稿的更多信息,请参阅“关于拉取请求”。 不会自动请求代码所有者审查拉取请求草稿。 如果将拉取请求转换为草稿,则已订阅通知的用户不会自动取消订阅。 更多信息请参阅“更改拉取请求的阶段”。

当具有管理员或所有者权限的人员启用必需审查时,他们也可选择性要求代码所有者批准后,作者才可合并仓库中的拉取请求。 更多信息请参阅“关于受保护分支”。

如果团队启用了代码审查分配,则个别审批无法满足受保护分支中代码所有者审批的要求。 更多信息请参阅“管理团队的代码审查分配”。

CODEOWNERS 文件位置

要使用 CODEOWNERS 文件,请在仓库中您要添加代码所有者的分支的根目录 docs/.github/ 中,创建一个名为 CODEOWNERS 的新文件。

每个 CODEOWNERS 文件将为仓库中的一个分支分配代码所有者。 因此,您可以为不同的分支分配不同的代码所有者,例如为默认分支的代码基础分配 @octo-org/codeowners-team,为 gh-pages 分支的 GitHub Pages 站点分配 @octocat

为使代码所有者接收审查请求,CODEOWNERS 文件必须在拉取请求的基本分支上。 例如,如果您将 @octocat 分配为仓库 gh-pages 分支上 .js 文件的代码所有者,则在头部分支与 gh-pages 之间打开更改 .js 文件的拉取请求时,@octocat 将会收到审查请求。

CODEOWNERS 语法

CODEOWNERS 文件使用遵循 gitignore 文件中所用大多数规则的模式,但有一些例外。 模式后接一个或多个使用标准 @username@org/team-name 格式的 GitHub 用户名或团队名称。 您也可以通过已经添加到其 GitHub Enterprise Server 帐户的电子邮件地址来指代用户,如 user@example.com

如果 CODEOWNERS 文件中的任何行包含无效语法,则该文件将不会被检测并且不会用于请求审查。

CODEOWNERS 文件示例

# This is a comment.
# Each line is a file pattern followed by one or more owners.

# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence,
# @global-owner1 and @global-owner2 will be requested for
# review when someone opens a pull request.
*       @global-owner1 @global-owner2

# Order is important; the last matching pattern takes the most
# precedence. When someone opens a pull request that only
# modifies JS files, only @js-owner and not the global
# owner(s) will be requested for a review.
*.js    @js-owner

# You can also use email addresses if you prefer. They'll be
# used to look up users just like we do for commit author
# emails.
*.go docs@example.com

# In this example, @doctocat owns any files in the build/logs
# directory at the root of the repository and any of its
# subdirectories.
/build/logs/ @doctocat

# The `docs/*` pattern will match files like
# `docs/getting-started.md` but not further nested files like
# `docs/build-app/troubleshooting.md`.
docs/*  docs@example.com

# In this example, @octocat owns any file in an apps directory
# anywhere in your repository.
apps/ @octocat

# In this example, @doctocat owns any file in the `/docs`
# directory in the root of your repository and any of its
# subdirectories.
/docs/ @doctocat

语法例外

gitignore 文件有一些语法规则在 CODEOWNERS 文件中不起作用:

  • 使用 \ 对以 # 开头的模式转义,使其被当作模式而不是注释
  • 使用 ! 否定模式
  • 使用 [ ] 定义字符范围

延伸阅读