如何使用GitHub Actions 自动压缩并合并?

huangapple go评论65阅读模式
英文:

How to automatically squash and merge using github actions?

问题

以下是翻译好的代码部分:

我正在通过GitHub Actions 进行一些自动化操作,希望在所有者提交了一条“comment”时,触发现有拉取请求上的“squash-and-merge”。

我的代码如下:

```yaml
  merge-release:
    if: |
      ${{ github.repository == 'repo/to/avoid/execution/from/forks' }} &&
      ${{ github.triggering_actor == 'xyz' }} &&
      ${{ github.event.client_payload.slash_command.command == "approve-merge" }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          script: |
            const { Octokit } = require("@octokit/rest");
            const octokit = new Octokit({ auth: '${{ secrets.GITHUB_TOKEN }}' });
            await octokit.request('PUT /repos/${{ github.action_repository }}/pulls/${{ github.event.number }}/merge', {
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: '${{ github.event.number }}',
              commit_title: 'Example',
              commit_message: 'Example.',
              headers: { 'X-GitHub-Api-Version': '2022-11-28' }
            })

根据这些资源,看起来应该可以做到:

  1. https://stackoverflow.com/questions/67477142/is-it-possible-to-squash-commits-via-github-api
  2. https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#merge-a-pull-request

然而,我的代码出现以下错误:

错误:找不到模块 '@octokit/rest'
需要堆栈:
- /home/runner/work/_actions/actions/github-script/v6/dist/index.js
    在 Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) 处
    在 resolve (node:internal/modules/cjs/helpers:108:19) 处
    在 Object.apply (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15188:43) 处
    在 eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15143:16), <anonymous>:3:21) 处
    在 callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15144:12) 处
    在 main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15236:26) 处
    在 /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15217:1 处
    在 /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15268:3 处
    在 Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15271:12) 处
    在 Module._compile (node:internal/modules/cjs/loader:1105:14) 处,{
  code: 'MODULE_NOT_FOUND',
  requireStack: [
错误:未处理的错误:错误:找不到模块 '@octokit/rest'
需要堆栈:
- /home/runner/work/_actions/actions/github-script/v6/dist/index.js
    '/home/runner/work/_actions/actions/github-script/v6/dist/index.js'
  ]
}

我在这里漏掉了什么?还有其他方法可以实现吗?


请注意,这只是代码的翻译部分,不包括问题的回答。如果您需要更多帮助,请提出具体问题。

<details>
<summary>英文:</summary>

I&#39;m doing some automation via github actions and would like to trigger a `squash-and-merge` on an existing pull request when a `comment` from the owner is submitted.

My code looks like this:

merge-release:
if: |
${{ github.repository == 'repo/to/avoid/execution/from/forks' }} &&
${{ (github.triggering_actor == 'xyz' }} &&
${{ github.event.client_payload.slash_command.command == "approve-merge" }}
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
script: |
const { Octokit } = require("@octokit/rest");
const octokit = new Octokit({ auth: '${{ secrets.GITHUB_TOKEN }}' });
await octokit.request('PUT /repos/${{ github.action_repository }}/pulls/${{ github.event.number }}/merge', {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: '${{ github.event.number }}',
commit_title: 'Example',
commit_message: 'Example.',
headers: { 'X-GitHub-Api-Version': '2022-11-28' }
})


Based on these resources it looks like you should be able to do it:
1. https://stackoverflow.com/questions/67477142/is-it-possible-to-squash-commits-via-github-api 
2. https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#merge-a-pull-request

However my code fails with:

Error: Cannot find module '@octokit/rest'
Require stack:

  • /home/runner/work/_actions/actions/github-script/v6/dist/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at resolve (node:internal/modules/cjs/helpers:108:19)
    at Object.apply (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15188:43)
    at eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15143:16), <anonymous>:3:21)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15144:12)
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15236:26)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15217:1
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15268:3
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15271:12)
    at Module._compile (node:internal/modules/cjs/loader:1105:14) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [
    Error: Unhandled error: Error: Cannot find module '@octokit/rest'
    Require stack:
  • /home/runner/work/_actions/actions/github-script/v6/dist/index.js
    '/home/runner/work/_actions/actions/github-script/v6/dist/index.js'
    ]
    }

What am I missing here? Is there another way to do it?

</details>


# 答案1
**得分**: 1

看起来你需要用 npm i @octokit/rest 安装 @octokit/rest。

merge-release:
    if: |
      ${{ github.repository == 'repo/to/avoid/execution/from/forks' }} &&
      ${{ github.triggering_actor == 'xyz' }} &&
      ${{ github.event.client_payload.slash_command.command == "approve-merge" }}
    runs-on: ubuntu-latest
    steps:
      - run: npm i @octokit/rest <===== 这里
      - uses: actions/github-script@v6
        with:
          script: |
            const { Octokit } = require("@octokit/rest");
            const octokit = new Octokit({ auth: '${{ secrets.GITHUB_TOKEN }}' });
            await octokit.request('PUT /repos/${{ github.action_repository }}/pulls/${{ github.event.number }}/merge', {
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: '${{ github.event.number }}',
              commit_title: 'Example',
              commit_message: 'Example.',
              headers: { 'X-GitHub-Api-Version': '2022-11-28' }
            })
英文:

Looks like you need to install @octokit/rest with npm i @octokit/rest.

merge-release:
    if: |
      ${{ github.repository == &#39;repo/to/avoid/execution/from/forks&#39; }} &amp;&amp;
      ${{ (github.triggering_actor == &#39;xyz&#39; }} &amp;&amp;
      ${{ github.event.client_payload.slash_command.command == &quot;approve-merge&quot; }}
    runs-on: ubuntu-latest
    steps:
      - run: npm i @octokit/rest &lt;===== here
      - uses: actions/github-script@v6
        with:
          script: |
            const { Octokit } = require(&quot;@octokit/rest&quot;);
            const octokit = new Octokit({ auth: &#39;${{ secrets.GITHUB_TOKEN }}&#39; });
            await octokit.request(&#39;PUT /repos/${{ github.action_repository }}/pulls/${{ github.event.number }}/merge&#39;, {
              owner: context.repo.owner,
              repo: context.repo.repo,
              pull_number: &#39;${{ github.event.number }}&#39;,
              commit_title: &#39;Example&#39;,
              commit_message: &#39;Example.&#39;,
              headers: { &#39;X-GitHub-Api-Version&#39;: &#39;2022-11-28&#39; }
            })

huangapple
  • 本文由 发表于 2023年5月17日 09:41:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定