From b1c0f351e15a1bc039031213d733c53d3eb3e419 Mon Sep 17 00:00:00 2001 From: Hiroaki Goto Date: Sat, 7 Oct 2023 23:50:09 +0900 Subject: [PATCH] chore: add bot command /snapshot --- .github/workflows/bot.yml | 92 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/.github/workflows/bot.yml b/.github/workflows/bot.yml index e69de29b..48e0968f 100644 --- a/.github/workflows/bot.yml +++ b/.github/workflows/bot.yml @@ -0,0 +1,92 @@ +on: + issue_comment: + types: [created, edited] + +name: Bot + +jobs: + pr_pre_comment: + # This job only runs for pull request comments + name: PR snapshot comment before start + if: ${{ github.event.issue.pull_request && github.event.comment.body == '/snapshot' }} + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v6 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: "Start taking snapshots for this pull request.\n" + + `https://github.com/${context.repo.repo}/actions/runs/${context.runId}` + }); + pr_snapshot_commented: + # This job only runs for pull request comments + name: PR snapshot comment + needs: [pr_pre_comment] + if: ${{ github.event.issue.pull_request && github.event.comment.body == '/snapshot' }} + strategy: + # Do not run in parallel because we may create a new commit + max-parallel: 1 + matrix: + name: [windows, linux] + include: + - name: windows + os: windows-latest + - name: linux + os: ubuntu-latest + runs-on: ${{ matrix.os }} + env: + # Set TRYCMD=overwrite to update snapshot + TRYCMD: overwrite + steps: + - uses: actions/github-script@v6 + id: target-branch + with: + result-encoding: string + script: | + const pull_request = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number + }); + return pull_request.data.head.ref; + - uses: actions/checkout@v2 + with: + ref: ${{ steps.target-branch.outputs.result }} + - uses: actions-rs/toolchain@v1 + with: + components: rustfmt, clippy + # Pinned to the commit hash of v2.2.1 + - uses: Swatinem/rust-cache@6fd3edff6979b79f87531400ad694fb7f2c84b1f + with: + shared-key: pr-snapshot-commented-${{ matrix.name }} + - uses: actions-rs/cargo@v1 + # Generate new snapshots + with: + command: test + args: cli_tests + - run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git add . + git commit -m "test: update snapshot for ${{ matrix.name }}" + git push + exit + pr_post_comment: + # This job only runs for pull request comments + name: PR snapshot comment after completion + needs: [pr_snapshot_commented] + if: ${{ github.event.issue.pull_request && github.event.comment.body == '/snapshot' }} + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v6 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: 'Taking snapshots has been completed.' + });