使用 github 操作创建拉取请求

问题描述 投票:0回答:5

我试图让它在这个动作中起作用,但我也很困惑在触发 peter-evans PR 之前它之间缺少什么。

场景非常简单,我喜欢在任何 feature/* 分支上推送以自动创建 PR,但我却遇到了奇怪的场景,其中开发更改应用在 feature/* 分支之上。有人可以给我提示吗?

name: Pull Request Action
on:
  push:
    branches:
      - feature/*

jobs:
  create-pull-request:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
          ref: develop
      
      - name: Create Pull Request
        uses: peter-evans/[email protected]
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: Simple demo
          title: '[Example] Simple demo'
          body: >
            This PR is auto-generated by 
            [create-pull-request](https://github.com/peter-evans/create-pull-request).
          labels: feature, automated pr
          branch: feature/workflow-demo
github github-actions pull-request
5个回答
40
投票

我知道这个问题已经有一年了,询问的是

create-pull-request
操作,但是对于那些不想使用第三方操作的人来说,如果您使用 Github 托管的运行器,Github 操作现在本地支持 Github 命令行。请参阅:在工作流中使用 Github CLI

这使得使用 gh pr create 命令创建拉取请求变得超级容易

像这样的东西:

  steps:
    - name: create pull request
      run: gh pr create -B base_branch -H branch_to_merge --title 'Merge branch_to_merge into base_branch' --body 'Created by Github action'
      env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

31
投票

只需将其发布为替代解决方案。如果您不想使用任何第 3 方操作,您可以使用

actions/github-script
实现此目的,它只需要更多的编码。

就目前而言,如果功能分支已经有一个开放的 PR,则该操作将出错。如果这是一个问题,您可以使用

github.rest.pulls.list
方法检查现有的 PR,同时通过
head
base
进行过滤,因此它只会返回一个或没有 PR。

name: Pull Request Action
on:
  push:
    branches:
      - feature/*
      
jobs:
  create-pull-request:
    runs-on: ubuntu-latest
    steps:
      - name: Create Pull Request
        uses: actions/github-script@v6
        with:
          script: |
            const { repo, owner } = context.repo;
            const result = await github.rest.pulls.create({
              title: '[Example] Simple demo',
              owner,
              repo,
              head: '${{ github.ref_name }}',
              base: 'develop',
              body: [
                'This PR is auto-generated by',
                '[actions/github-script](https://github.com/actions/github-script).'
              ].join('\n')
            });
            github.rest.issues.addLabels({
              owner,
              repo,
              issue_number: result.data.number,
              labels: ['feature', 'automated pr']
            });

6
投票

通读自述文件,Peter Evans 的操作不符合您要实现的目标。但是你可以使用 repo-sync 的 pull-request 操作

name: Pull Request Action
on:
  push:
    branches:
      - feature/*

jobs:
  create-pull-request:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2
      - name: pull-request
        uses: repo-sync/pull-request@v2
        with:
          destination_branch: "develop"
          github_token: ${{ secrets.GITHUB_TOKEN }}
          pr_label: "feature, automated pr"
          pr_title: "[Example] Simple demo"

0
投票

添加到@Grabofus 的答案中,如果已经存在针对相同

head
base
的现有且开放的PR,则该操作将失败。这可以通过在创建新 PR 之前检查现有 PR 来解决:

  create_pull_request:
    name: 'Create PR'
    if: github.ref == 'refs/heads/beta'
    runs-on: ubuntu-latest
    needs:
      - deploy
    permissions:
      pull-requests: write
    steps:
      - name: 'Create PR'
        uses: actions/github-script@v6
        with:
          script: |
            const { repo, owner } = context.repo;
            const pulls = await github.rest.pulls.list({
              owner: owner,
              repo: repo,
              head: context.ref,
              base: 'main',
              state: 'open',
            });
            
            if (pulls.data.length < 1) {
              await github.rest.pulls.create({
                title: '[CI] Merge beta into main',
                owner: owner,
                repo: repo,
                head: context.ref,
                base: 'main',
                body: [
                  'This PR is auto-generated by',
                  '[actions/github-script](https://github.com/actions/github-script)',
                ].join('\n'),
              });
            } else {
              const existingPR = pulls.data[0];
              await github.rest.pulls.update({
                owner: owner,
                repo: repo,
                pull_number: existingPR.number,
                body: [
                  existingPR.body,
                  `Updated by Job ${context.job}`,
                ].join('\n'),
              });
            }

除了像上面的例子那样更新描述,你当然也可以决定什么都不做,或者例如添加一个替代评论,如下所示:

            } else {
              const existingPR = pulls.data[0];
              await github.rest.issues.createComment({
                owner: owner,
                repo: repo,
                issue_number: existingPR.number,
                body: [
                  `Updated by Job ${context.job}`,
                ].join('\n'),
              });
            }

-2
投票

您可能需要在那里指定基本分支:

- name: Create Pull Request
    uses: peter-evans/[email protected]
    with:
      token: ${{ secrets.GH_TOKEN }}
      commit-message: Auto Pull Request
      title: Your desired title
      body: Auto-created Pull Request
      branch: ${{ github.ref }} # The branch where you commit
      base: develop # Don't forget to specify the right base branch here

我有这个,当它不存在时它会创建 PR。我记得在我指定自己

base
branch
值之前它在一开始并没有完全正确地工作,这在文档中不是很清楚。

© www.soinside.com 2019 - 2024. All rights reserved.