如何自动合并 Dependabot 更新(配置版本 2)?

15

在看完“Dependabot正式移植到GitHub!”后,我需要更新我的dependabot配置文件以使用版本2格式。

我的.dependabot/config.yaml看起来像这样:

version: 1
update_configs:
  - package_manager: "python"
    directory: "/"
    update_schedule: "live"
    automerged_updates:
      - match:
          dependency_type: "all"
          update_type: "all"

我已经让以下代码正常工作:

version: 2
updates:
- package-ecosystem: pip
  directory: "/"
  schedule:
    interval: daily

但我似乎无法再次添加自动合并选项(在使用dependabot验证器进行检查时)?


1
看起来他们现在可能会暂停这个功能:https://github.com/dependabot/dependabot-core/issues/1973 - andyandy
1
你应该将这个作为一个答案添加。 - riQQ
另外,提醒一下,这个功能将永远不会添加到 GitHub 上的 Dependabot。请查看 @milton-castro 的答案。 - Jarmos
看起来 GitHub 并不打算将它直接作为 Dependabot 的功能添加进去,但他们仍然正式记录了如何通过 Actions 工作流程 来实现它。 - kojiro
3个回答

12

现在这是一个正式记录的功能。您可以通过GitHub Actions工作流程批准Dependabot拉取请求并将其设置为自动合并...

name: Dependabot auto-approve
on: pull_request_target
    
permissions:
  contents: write
  pull-requests: write
    
jobs:
  dependabot:
    runs-on: ubuntu-latest
    if: ${{ github.actor == 'dependabot[bot]' }}
    steps:
      - name: Dependabot metadata
        id: metadata
        uses: dependabot/fetch-metadata@v1.1.1
        with:
          github-token: "${{ secrets.GITHUB_TOKEN }}"
      - name: Enable auto-merge for Dependabot PRs
        if: ${{contains(steps.metadata.outputs.dependency-names, 'my-dependency') && steps.metadata.outputs.update-type == 'version-update:semver-patch'}}
        run: gh pr merge --auto --merge "$PR_URL"
        env:
          PR_URL: ${{github.event.pull_request.html_url}}
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}

更新:CODEOWNERS现在确实允许否定

如果您使用代码所有者并且分支受保护,则可能会发现仍然需要等待代码所有者审核才能合并。您可以创建一个.github/CODEOWNERS文件,并按如下方式设置,以便对于除相关文件外的所有文件都要求代码所有者审核:

* owner1 owner2 @org/team1
setup.cfg  # setup.cfg is not owned

4
两点注意事项:1.你需要在仓库设置中启用自动合并,2.你需要使用“必须通过检查才能合并”来保护目标分支。如果你不同时执行这两个步骤,上述命令行代码(由于某种原因)会在运行后立即合并拉取请求。 - mxcl
1
@mxcl 工作流程可以排序并设置依赖关系。假设有一个构建和自动合并的工作流程,您可以设置自动合并在构建工作流程结果成功后运行。 - Hantsy
@mxcl,您能否提供一个“合并前必须通过检查”的目标分支保护的文档或实现链接? - ak76
它们被隐藏在名为“受保护的分支”的功能下。https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/defining-the-mergeability-of-pull-requests/about-protected-branches#require-status-checks-before-merging - kojiro

10
以下是翻译的结果:

这里有一个解决方案,不需要任何额外的市场安装(最初在此处找到)。只需要创建一个新的 GitHub 工作流程(例如 .github/workflows/dependabotautomerge.yml),其中包含:

name: "Dependabot Automerge - Action"

on:
  pull_request:

jobs:
  worker:
    runs-on: ubuntu-latest

    if: github.actor == 'dependabot[bot]'
    steps:
      - name: automerge
        uses: actions/github-script@0.2.0
        with:
          script: |
            github.pullRequests.createReview({
              owner: context.payload.repository.owner.login,
              repo: context.payload.repository.name,
              pull_number: context.payload.pull_request.number,
              event: 'APPROVE'
            })
            github.pullRequests.merge({
              owner: context.payload.repository.owner.login,
              repo: context.payload.repository.name,
              pull_number: context.payload.pull_request.number
            })
          github-token: ${{github.token}}

GitHub Marketplace上还提供了各种第三方解决方案。点击此处查看。


1
那段代码片段应该得到认可。这是原始来源:https://secopslab.medium.com/automerge-github-dependabot-alerts-with-github-actions-7cd6f5763750 - Jarmos
这是官方文档记录的方法的原始来源吗?https://dev59.com/rlIG5IYBdhLWcg3wkBsx#68365564 - kojiro
(可能不是,因为官方记录的方法实质上有很大不同。) - kojiro

6

GitHub的Dependabot已禁用自动合并:

在可预见的未来,GitHub本地的Dependabot将不支持自动合并。我们知道有些人已经构建了依赖于自动合并的工作流,但是现在,我们担心自动合并会被用于快速传播恶意软件包到生态系统中。我们建议在合并之前始终验证您的依赖关系。

有一些技巧可以完成这个任务,您可以查看GitHub的 dependabot-core 问题#1973 获取一些想法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接