# 关于代码所有者

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

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

## 关于代码所有者

当有人打开修改代码的拉取请求时，将自动请求代码所有者进行审查。 不会自动请求代码所有者审查拉取请求草稿。 有关草稿拉取请求的详细信息，请参阅 [拉取请求](/zh/enterprise-server@3.21/pull-requests/reference/pull-requests#draft-pull-requests)。 当您将拉取请求草稿标记为准备审核时，代码所有者会自动收到通知。 如果将拉取请求转换为草稿，则已订阅通知的用户不会自动取消订阅。 有关详细信息，请参阅“[更改拉取请求的阶段](/zh/enterprise-server@3.21/pull-requests/how-tos/create-pull-requests/changing-the-stage-of-a-pull-request)”。

当具有管理员或所有者权限的人员启用必需审查时，他们也可选择性要求代码所有者批准后，作者才可合并仓库中的拉取请求。 有关详细信息，请参阅“[关于受保护分支](/zh/enterprise-server@3.21/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches#require-pull-request-reviews-before-merging)”。

如果文件具有代码所有者，则在打开拉取请求之前可以看到代码所有者是谁。 在存储库中，可以浏览到该文件，并将鼠标悬停在 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-shield-lock" aria-label="Owned by USER or TEAM (from CODEOWNERS line NUMBER)" role="img"><path d="m8.533.133 5.25 1.68A1.75 1.75 0 0 1 15 3.48V7c0 1.566-.32 3.182-1.303 4.682-.983 1.498-2.585 2.813-5.032 3.855a1.697 1.697 0 0 1-1.33 0c-2.447-1.042-4.049-2.357-5.032-3.855C1.32 10.182 1 8.566 1 7V3.48a1.75 1.75 0 0 1 1.217-1.667l5.25-1.68a1.748 1.748 0 0 1 1.066 0Zm-.61 1.429.001.001-5.25 1.68a.251.251 0 0 0-.174.237V7c0 1.36.275 2.666 1.057 3.859.784 1.194 2.121 2.342 4.366 3.298a.196.196 0 0 0 .154 0c2.245-.957 3.582-2.103 4.366-3.297C13.225 9.666 13.5 8.358 13.5 7V3.48a.25.25 0 0 0-.174-.238l-5.25-1.68a.25.25 0 0 0-.153 0ZM9.5 6.5c0 .536-.286 1.032-.75 1.3v2.45a.75.75 0 0 1-1.5 0V7.8A1.5 1.5 0 1 1 9.5 6.5Z"></path></svg> 上，以查看包含代码所有权详细信息的提示框。

![显示文件的标头的屏幕截图。 光标悬停在盾牌图标上，显示工具提示“由 USER 或 TEAM 拥有。”](/assets/images/enterprise/repository/code-owner-for-a-file.png)

## CODEOWNERS 文件位置

要使用 CODEOWNERS 文件，请在存储库要添加代码所有者的分支的 `CODEOWNERS`、根目录或 `.github/` 目录中创建一个名为的 `docs/` 新文件。 如果 `CODEOWNERS` 文件存在于其中一个以上的位置， GitHub 则将按该顺序搜索这些文件，并使用它找到的第一个位置。

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

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

## CODEOWNERS 和分支

若要触发审查请求，拉取请求将使用拉取请求基本分支的 `CODEOWNERS` 版本。 基本分支是合并拉取请求时拉取请求将会修改的分支。

如果从分支创建拉取请求，而基本分支位于上游存储库中，则拉取请求将使用上游存储库中该分支的 `CODEOWNERS` 文件。 如果基本分支是您派生库中的一个分支，那么拉取请求将使用您派生库中该分支的 `CODEOWNERS` 文件，但这只有在代码所有者被专门授予 `write` 访问权限后，才会触发审查请求。

当你将鼠标悬停在 <svg version="1.1" width="16" height="16" viewBox="0 0 16 16" class="octicon octicon-shield-lock" aria-label="Owned by USER or TEAM (from CODEOWNERS line NUMBER)" role="img"><path d="m8.533.133 5.25 1.68A1.75 1.75 0 0 1 15 3.48V7c0 1.566-.32 3.182-1.303 4.682-.983 1.498-2.585 2.813-5.032 3.855a1.697 1.697 0 0 1-1.33 0c-2.447-1.042-4.049-2.357-5.032-3.855C1.32 10.182 1 8.566 1 7V3.48a1.75 1.75 0 0 1 1.217-1.667l5.25-1.68a1.748 1.748 0 0 1 1.066 0Zm-.61 1.429.001.001-5.25 1.68a.251.251 0 0 0-.174.237V7c0 1.36.275 2.666 1.057 3.859.784 1.194 2.121 2.342 4.366 3.298a.196.196 0 0 0 .154 0c2.245-.957 3.582-2.103 4.366-3.297C13.225 9.666 13.5 8.358 13.5 7V3.48a.25.25 0 0 0-.174-.238l-5.25-1.68a.25.25 0 0 0-.153 0ZM9.5 6.5c0 .536-.286 1.032-.75 1.3v2.45a.75.75 0 0 1-1.5 0V7.8A1.5 1.5 0 1 1 9.5 6.5Z"></path></svg> 上以查看谁负责某个文件时，你将看到来自 `CODEOWNERS` 文件的信息，具体取决于你当前查看的是哪个仓库中的哪个分支。

## CODEOWNERS 文件大小

CODEOWNERS 文件大小必须低于 3MB。 将不会加载超过此限制的 CODEOWNERS 文件，这意味着不会显示代码所有者信息，并且不会要求相应的代码所有者查看拉取请求中的更改。

要减小 CODEOWNERS 文件的大小，请考虑使用通配符模式将多个条目合并为单个条目。

## CODEOWNERS 语法

> [!WARNING]
> gitignore 文件中的一些语法规则在 CODEOWNERS 文件中不适用：
>
> * 使用 `#` 对以 `\` 开头的模式进行转义，就可将其视为模式而不是注释，这种方法是无效的
> * 使用 `!` 否定模式不起作用
> * 使用 `[ ]` 定义字符范围不起作用

CODEOWNERS 文件使用遵循 [gitignore](https://git-scm.com/docs/gitignore#_pattern_format) 文件中所用的大多数相同规则的模式。 模式后跟一个或多个 GitHub 用户名或团队名称，使用标准的 `@username` 或 `@org/team-name` 格式。 用户和团队必须具有对存储库的显式 `write` 访问权限，即使团队成员已具有访问权限也是如此。

> [!NOTE]
> 使用团队时，只有组织团队才能成为代码所有者。 不支持将企业团队设为代码所有者。 请参阅“[企业中的团队](/zh/enterprise-server@3.21/admin/concepts/enterprise-fundamentals/teams-in-an-enterprise)”。

如果要匹配两个或多个具有相同模式的代码所有者，所有的代码所有者必须位于同一行。 如果代码所有者不在同一行，模式会仅匹配最后提到的代码所有者。

你也可以使用已添加到其帐户的电子邮件地址来引用用户，例如 `user@example.com`。

CODEOWNERS 路径区分大小写，因为 GitHub 使用区分大小写的文件系统。 由于 CODEOWNERS 由 GitHub 评估，即使是大小写不敏感的系统（例如 macOS）也必须使用 CODEOWNERS 文件中大小写正确的路径和文件。

如果 CODEOWNERS 文件中的任何行包含无效语法，则将跳过该行。 导航到存储库中的 CODEOWNERS 文件后，可以看到突出显示的所有错误。 存储库的 CODEOWNERS 文件中的错误列表也可以通过 API 访问。 有关详细信息，请参阅“[存储库的 REST API 终结点](/zh/enterprise-server@3.21/rest/repos/repos#list-codeowners-errors)”。

如果指定的用户或团队不存在或者访问权限不足，则不会分配代码所有者。

### CODEOWNERS 文件示例

```text
# 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 #This is an inline comment.

# 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

# Teams can be specified as code owners as well. Teams should
# be identified in the format @org/team-name. Teams must have
# explicit write access to the repository. In this example,
# the octocats team in the octo-org organization owns all .txt files.
*.txt @octo-org/octocats

# 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

# In this example, any change inside the `/scripts` directory
# will require approval from @doctocat or @octocat.
/scripts/ @doctocat @octocat

# In this example, @octocat owns any file in a `/logs` directory such as
# `/build/logs`, `/scripts/logs`, and `/deeply/nested/logs`. Any changes
# in a `/logs` directory will require approval from @octocat.
**/logs @octocat

# In this example, @octocat owns any file in the `/apps`
# directory in the root of your repository except for the `/apps/github`
# subdirectory, as its owners are left empty. Without an owner, changes
# to `apps/github` can be made with the approval of any user who has
# write access to the repository.
/apps/ @octocat
/apps/github

# In this example, @octocat owns any file in the `/apps`
# directory in the root of your repository except for the `/apps/github`
# subdirectory, as this subdirectory has its own owner @doctocat
/apps/ @octocat
/apps/github @doctocat
```

## CODEOWNERS 和分支保护

存储库所有者可以更新分支保护规则，确保已更改文件的所有者评审了已更改的代码。 编辑分支保护规则，并启用“需要代码所有者评审”选项。 有关详细信息，请参阅“[关于受保护分支](/zh/enterprise-server@3.21/repositories/configuring-branches-and-merges-in-your-repository/managing-protected-branches/about-protected-branches)”。

> [!NOTE]
> 当需要由代码所有者进行审阅时，任何所有者的批准均足以满足此要求\_\_。 例如，假设 CODEOWNERS 文件包含以下行：
>
> ```text
> *.js     @global-owner1 @global-owner2
> ```
>
> 这意味着，对 JavaScript 文件的更改可以由 `@global-owner1`*或*`@global-owner2` 批准，但不需要 *两者的* 批准。

要完全保护存储库免遭未经授权的更改，还需为 CODEOWNERS 文件本身定义所有者。 最安全的方法是在存储库的 `.github` 目录中定义 CODEOWNERS 文件，同时将存储库所有者定义为 CODEOWNERS 文件 (`/.github/CODEOWNERS @owner_username`) 或整个目录 (`/.github/ @owner_username`) 的所有者。

作为分支保护规则的替代方法，可以创建规则集。 规则集在分支保护规则（例如状态）和更好的可发现性（无需管理员访问权限）上具有一些优势。 还可以同时应用多个规则集。 有关详细信息，请参阅“[关于规则集](/zh/enterprise-server@3.21/repositories/configuring-branches-and-merges-in-your-repository/managing-rulesets/about-rulesets)”。

## 其他阅读材料

* [创建新文件](/zh/enterprise-server@3.21/repositories/working-with-files/managing-files/creating-new-files)
* [邀请协作者参加个人仓库](/zh/enterprise-server@3.21/repositories/managing-your-repositorys-settings-and-features/repository-access-and-collaboration/inviting-collaborators-to-a-personal-repository)
* [管理个人对组织仓库的访问](/zh/enterprise-server@3.21/organizations/managing-user-access-to-your-organizations-repositories/managing-repository-roles/managing-an-individuals-access-to-an-organization-repository)
* [管理团队对组织存储库的访问](/zh/enterprise-server@3.21/organizations/managing-user-access-to-your-organizations-repositories/managing-repository-roles/managing-team-access-to-an-organization-repository)
* [查看拉取请求审查](/zh/enterprise-server@3.21/pull-requests/how-tos/review-pull-requests/viewing-a-pull-request-review)