# 自定义 Dependabot 拉取请求以适应你的流程

了解如何定制你的 Dependabot 拉取请求以更好地适应你自己的内部工作流。

可通过多种方式自定义你的 Dependabot 拉取请求，以便它们更好地适应你自己的内部流程。

例如，为了将 Dependabot 的拉取请求集成到 CI/CD 管道中，它可以将自定义标签应用于拉取请求，你随后可以使用拉取请求来触发操作工作流。\*\*\*\*

可以组合使用几种不同的自定义选项，并根据包生态系统进行定制。

## 自动添加被分派人

默认情况下，Dependabot 会提出没有任何被分派人的拉取请求。

若要自动将拉取请求分配给指定的安全团队，可以使用 `assignees` 为每个包生态系统设置这些值。

下面的示例 `dependabot.yml` 文件更改了 npm 配置，使所有因版本更新和安全更新而打开的拉取请求都具有：

* 被自动分配到拉取请求的个人（“`user-name`”）。

```yaml copy
# `dependabot.yml` file with
#  assignee for all npm pull requests

version: 2
updates:
  # Keep npm dependencies up to date
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    # Raise all npm pull requests with assignees
    assignees:
      - "user-name"
```

## 自动添加审阅者

默认情况下，Dependabot 会提出没有任何审阅者的拉取请求。

为了确保项目的安全更新得到相应团队的及时处理，可以使用 CODEOWNERS 文件自动将审阅者添加到 Dependabot 拉取请求。 请参阅“[关于代码所有者](/zh/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners)”。

## 使用自定义标签标记拉取请求

默认情况下，Dependabot 会提出带有 `dependencies` 标签的拉取请求。

Dependabot 还会为拉取请求应用生态系统标签，例如 `java`、`npm` 或 `github-actions`。 Dependabot 将 `dependencies` 标签和生态系统标签添加到所有拉取请求中，包括单个生态系统更新，以改进筛选和分类的效率。

Dependabot 会创建应用于拉取请求的默认标签（如果它们在存储库中不存在）。 如果要使用自定义标签而不是默认值，可以在每个包生态系统的文件中`labels`设置`dependabot.yml`选项;这将替代默认值。 有关详细信息，请参阅 [管理标签](/zh/issues/using-labels-and-milestones-to-track-work/managing-labels) 和 [`labels`](/zh/code-security/dependabot/working-with-dependabot/dependabot-options-reference#labels--)。

如果存储库中存在语义版本 (SemVer) 标签，Dependabot 也会自动应用它们以指示版本更新的类型（`major`、`minor` 或 `patch`）。 除了您定义的任何自定义标签外，还会应用这些标签。

可以使用 `labels` 替代默认标签，并为每个包生态系统指定自己的自定义标签。 例如，如果你想要执行以下操作，这将非常有用：

* 使用标签将优先级分配给某些拉取请求。
* 使用标签触发另一个工作流，例如自动将拉取请求添加到项目板上。

下面的示例 `dependabot.yml` 文件更改了 npm 配置，使所有针对 npm 的版本和安全更新打开的拉取请求都具有自定义标签。

```yaml copy
# `dependabot.yml` file with
# customized npm configuration

version: 2
updates:
  # Keep npm dependencies up to date
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    # Raise all npm pull requests with custom labels
    labels:
      - "npm dependencies"
      - "triage-board"
```

数据重用.dependabot.选项影响安全更新 %}

另请参阅[`labels`](/zh/code-security/dependabot/working-with-dependabot/dependabot-options-reference#labels--)。

## 为提交消息添加前缀

默认情况下，Dependabot 会尝试检测您的提交消息首选项并使用类似的模式。 此外，Dependabot 会根据提交消息填充拉取请求的标题。

可以为特定包生态系统的 Dependabot 的提交消息（以及拉取请求标题）指定你自己的前缀。 例如，如果您正在运行处理提交消息或拉取请求标题的自动化程序，这可能会非常有用。

若要显式指定首选项，请将 `commit-message` 与以下受支持的选项一起使用：

* `prefix`:
  * 指定所有提交消息的前缀。
  * 前缀也会添加到 pull request 标题的开头。
* `prefix-development`:
  * 为更新开发依赖项的所有提交消息指定单独的前缀，由包管理器或生态系统定义。
  * 支持 `bundler`、`composer`、`mix`、`maven`、`npm` 和 `pip`。
* `include: "scope"`:
  * 指定任何前缀后面跟随在提交中更新的依赖项类型（`deps` 或 `deps-dev`）。

下面的示例展示了根据包生态系统定制的几个不同的选项：

```yaml copy
# Customize commit messages

version: 2
updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    commit-message:
      # Prefix all commit messages with "npm: "
      prefix: "npm"

  - package-ecosystem: "docker"
    directory: "/"
    schedule:
      interval: "weekly"
    commit-message:
      # Prefix all commit messages with "[docker] " (no colon, but a trailing whitespace)
      prefix: "[docker] "

  - package-ecosystem: "composer"
    directory: "/"
    schedule:
      interval: "weekly"
    # Prefix all commit messages with "Composer" plus its scope, that is, a
    # list of updated dependencies
    commit-message:
      prefix: "Composer"
      include: "scope"

  - package-ecosystem: "pip"
    directory: "/"
    schedule:
      interval: "weekly"
    # Include a list of updated dependencies
    # with a prefix determined by the dependency group
    commit-message:
      prefix: "pip prod"
      prefix-development: "pip dev"
```

数据重用.dependabot.选项影响安全更新 %}

另请参阅[`commit-message`](/zh/code-security/dependabot/working-with-dependabot/dependabot-options-reference#commit-message--)。

## 将拉取请求与里程碑关联

里程碑可帮助你跟踪拉取请求（或问题）组实现项目目标或发布的进度。 借助 Dependabot，你可以使用 `milestone` 选项将依赖项更新的拉取请求与特定里程碑相关联。

必须指定里程碑的数字标识符，而不是其标签。 若要查找数字标识符，请查看页面 URL 的最后一部分，在 `milestone` 之后。 例如，对于 `https://github.com/<org>/<repo>/milestone/3`，“`3`”即是里程碑的数字标识符。

```yaml copy
# Specify a milestone for pull requests

version: 2
updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    # Associate pull requests with milestone "4"
    milestone: 4
```

数据重用.dependabot.选项影响安全更新 %}

另请参阅“[`milestone`](/zh/code-security/dependabot/working-with-dependabot/dependabot-options-reference#milestone--)”和“[关于里程碑](/zh/issues/using-labels-and-milestones-to-track-work/about-milestones)”。

## 更改拉取请求分支名称中的分隔符

Dependabot 为每个拉取请求生成一个分支。 每个分支名称都包括 `dependabot`，以及要更新的包管理器和依赖项的名称。 默认情况下，分支名称的这些部分由 `/` 符号分隔，例如：

* `dependabot/npm_and_yarn/next_js/acorn-6.4.1`

为了保持可支持性或与你现有流程的一致性，可能需要确保你的分支名称与你团队的现有约定保持一致。 在这种情况下，可以使用 `pull-request-branch-name.separator` 来指定不同的分隔符，选择 `_`、 `/`或 `"-"`。

在下面的示例中，npm 配置将默认分隔符从 `/` 更改为 `"-"`，以便如下所示：

* 默认 (`/`)：`dependabot/npm_and_yarn/next_js/acorn-6.4.1`
* 自定义 (`"-"`)：`dependabot-npm_and_yarn-next_js-acorn-6.4.1`

请注意，连字符符号 (`"-"`) 必须用引号括起来，以免它被解释为启动空 YAML 列表。

```yaml copy
# Specify a different separator for branch names

version: 2
updates:
  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
    pull-request-branch-name:
      # Change the default separator (/) to a hyphen (-)
      separator: "-"
```

数据重用.dependabot.选项影响安全更新 %}

另请参阅[`pull-request-branch-name.separator`](/zh/code-security/dependabot/working-with-dependabot/dependabot-options-reference#pull-request-branch-nameseparator--)。

## 针对非默认分支定向拉取请求

默认情况下，Dependabot 会检查默认分支上的清单文件，并对默认分支提出更新的拉取请求。

通常情况下，保留 Dependabot 针对默认分支的检查和更新是有意义的。 但是，在某些情况下，你可能需要指定不同的目标分支。 例如，如果你的团队流程要求你首先在非生产分支上测试和验证更新，你则可以使用 `target-branch` 来指定不同的分支，让 Dependabot 对其提出拉取请求。

> \[!NOTE]
> Dependabot **仅针对默认分支**提出安全更新的拉取请求。 如果使用 `target-branch`，那么该包管理器的所有配置设置将仅适用于版本更新，而不适用于安全更新。\_\_

```yaml copy
# Specify a non-default branch for pull requests for pip

version: 2
updates:
  - package-ecosystem: "pip"
    directory: "/"
    schedule:
      interval: "weekly"
    # Raise pull requests for version updates
    # to pip against the `develop` branch
    target-branch: "develop"
    # Labels on pull requests for version updates only
    labels:
      - "pip dependencies"

  - package-ecosystem: "npm"
    directory: "/"
    schedule:
      interval: "weekly"
      # Check for npm updates on Sundays
      day: "sunday"
    # Labels on pull requests for security and version updates
    labels:
      - "npm dependencies"
```

另请参阅[`target-branch`](/zh/code-security/dependabot/working-with-dependabot/dependabot-options-reference#target-branch-)。