codeowners 指定目录所属
最后更新于:2022-04-02 02:52:31
[TOC]
## 概述
- GitHub 和 GitLab 都支持这一特性
- 使用CODEOWNERS文件来指定负责存储库中某些文件的用户或共享组
- 当有人提交 pull request 或 push 代码到受保护的分支时,代码所有者会自动被要求进行审查
## gitlab 流程
> [参考](https://www.kancloud.cn/apachecn/gitlab-doc-zh/1948642)
1. 您可以在三个位置选择并添加CODEOWNERS文件:
```
到存储库的根目录
在.gitlab/目录中
在docs/目录中
```
冲突时,以下方为主
```
README.md @user1
# This line would also match the file README.md
*.md @user2
```
将显示README.md的用户为@user2 .
2. Approvals by Code Owners
将"代码所有者"设置为项目后,可以将其配置为用于合并请求批准:
* As[merge request eligible approvers](https://www.kancloud.cn/apachecn/gitlab-doc-zh/merge_requests/merge_request_approvals.html#code-owners-as-eligible-approvers).
* 根据需要批准[分支机构](https://www.kancloud.cn/apachecn/gitlab-doc-zh/protected_branches.html#protected-branches-approval-by-code-owners-premium).
**注意**:为了批准合并请求,需要开发人员或更高[权限](https://www.kancloud.cn/apachecn/permissions.html).
设置后,"代码所有者"将显示在合并请求小部件中:
![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/4f/87/4f8706ad076d7045001703617710109a_2040x686.png)
3. CODEOWNERS 文件的语法
可以使用与.gitignore文件中使用的相同类型的模式来指定文件,然后使用一个或多个用户的@username或电子邮件或应作为文件所有者的一个或多个组的@name进行指定. 必须将组添加为项目的成员 ,否则它们将被忽略
CODEOWNERS
```
# This is an example of a code owners file
# lines starting with a `#` will be ignored.
# app/ @commented-rule
# We can specify a default match using wildcards:
* @default-codeowner
# We can also specify "multiple tab or space" separated codeowners:
* @multiple @code @owners
# Rules defined later in the file take precedence over the rules
# defined before.
# This will match all files for which the file name ends in `.rb`
*.rb @ruby-owner
# Files with a `#` can still be accessed by escaping the pound sign
\#file_with_pound.rb @owner-file-with-pound
# Multiple codeowners can be specified, separated by spaces or tabs
# In the following case the CODEOWNERS file from the root of the repo
# has 3 code owners (@multiple @code @owners)
CODEOWNERS @multiple @code @owners
# Both usernames or email addresses can be used to match
# users. Everything else will be ignored. For example this will
# specify `@legal` and a user with email `janedoe@gitlab.com` as the
# owner for the LICENSE file
LICENSE @legal this_does_not_match janedoe@gitlab.com
# Group names can be used to match groups and nested groups to specify
# them as owners for a file
README @group @group/with-nested/subgroup
# Ending a path in a `/` will specify the code owners for every file
# nested in that directory, on any level
/docs/ @all-docs
# Ending a path in `/*` will specify code owners for every file in
# that directory, but not nested deeper. This will match
# `docs/index.md` but not `docs/projects/index.md`
/docs/* @root-docs
# This will make a `lib` directory nested anywhere in the repository
# match
lib/ @lib-owner
# This will only match a `config` directory in the root of the
# repository
/config/ @config-owner
# If the path contains spaces, these need to be escaped like this:
path\ with\ spaces/ @space-owner
```
';