Search code examples
gitgithub-actionsgithub-api

How to automatically squash and merge using github actions?


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. 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?


Solution

  • 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' }
                })