英文:
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' }
})
根据这些资源,看起来应该可以做到:
- https://stackoverflow.com/questions/67477142/is-it-possible-to-squash-commits-via-github-api
- 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'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 == '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 <===== here
- 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' }
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论