From 4cab89bb6819e2592dd4ae8e3a74a78e0e09c382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Mon, 24 Jan 2022 18:23:10 +0100 Subject: [PATCH] Enhancement: Add github/add-assignee-to-pull-request action --- .github/workflows/merge.yaml | 17 +---- CHANGELOG.md | 5 ++ .../add-assignee-to-pull-request/action.yaml | 67 +++++++++++++++++++ 3 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 actions/github/add-assignee-to-pull-request/action.yaml diff --git a/.github/workflows/merge.yaml b/.github/workflows/merge.yaml index 8e8c57b..74998b8 100644 --- a/.github/workflows/merge.yaml +++ b/.github/workflows/merge.yaml @@ -43,23 +43,10 @@ jobs: }); - name: "Assign @ergebnis-bot" - uses: "actions/github-script@v5" + uses: "./.github/actions/github/add-assignee-to-pull-request" with: github-token: "${{ secrets.ERGEBNIS_BOT_TOKEN }}" - script: | - const pullRequest = context.payload.workflow_run.pull_requests[0]; - const repository = context.repo; - - const assignees = [ - "ergebnis-bot", - ]; - - await github.rest.issues.addAssignees({ - owner: repository.owner, - repo: repository.repo, - assignees: assignees, - issue_number: pullRequest.number - }); + assignee: "ergebnis-bot" - name: "Approve pull request" uses: "actions/github-script@v5" diff --git a/CHANGELOG.md b/CHANGELOG.md index 6acb40b..5e2cc22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), For a full diff see [`1.2.1...main`][1.2.1...main]. +### Added + +- Added composite action `github/add-assignee-to-pull-request` ([#59]), by [@localheinz] + ## [`1.2.1`][1.2.1] For a full diff see [`1.2.0...1.2.1`][1.2.0...1.2.1]. @@ -55,5 +59,6 @@ For a full diff see [`1.0.0...main`][1.0.0...main]. [#48]: https://github.com/ergebnis/.github/pull/48 [#52]: https://github.com/ergebnis/.github/pull/52 [#54]: https://github.com/ergebnis/.github/pull/54 +[#59]: https://github.com/ergebnis/.github/pull/59 [@localheinz]: https://github.com/localheinz diff --git a/actions/github/add-assignee-to-pull-request/action.yaml b/actions/github/add-assignee-to-pull-request/action.yaml new file mode 100644 index 0000000..6c7d353 --- /dev/null +++ b/actions/github/add-assignee-to-pull-request/action.yaml @@ -0,0 +1,67 @@ +# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs +# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions +# https://docs.github.com/en/rest/reference/issues#add-assignees-to-an-issue +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request +# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run + +name: "Add assignee to pull request" + +description: "Adds an assignee to a pull request" + +inputs: + assignee: + description: "Username of user to add as an assignee to a pull request" + required: true + github-token: + description: "GitHub token of a user with permission to add assignees to a pull request" + required: true + +runs: + using: "composite" + + steps: + - name: "Determine pull request number" + uses: "actions/github-script@v5" + with: + github-token: "${{ inputs.github-token }}" + script: | + if (context.eventName == 'pull_request') { + core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number); + + return; + } + + if (context.eventName == 'workflow_run') { + core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number); + + return; + } + + core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`); + + - name: "Add assignee to pull request" + uses: "actions/github-script@v5" + with: + github-token: "${{ inputs.github-token }}" + script: | + if (!process.env.PULL_REQUEST_NUMBER) { + core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.") + + return; + } + + const assignees = [ + "${{ inputs.assignee }}", + ]; + + try { + await github.rest.issues.addAssignees({ + assignees: assignees, + issue_number: process.env.PULL_REQUEST_NUMBER, + owner: context.repo.owner, + repo: context.repo.repo, + }); + } catch (error) { + core.setFailed(error.message); + }