From d769688788745be4cc6084ee3042a1f62f78b612 Mon Sep 17 00:00:00 2001 From: lotyp Date: Thu, 5 Sep 2024 13:13:18 +0300 Subject: [PATCH] feat: composite actions to work with github PRs --- .github/workflows/build-image.yml | 2 +- .../workflows/create-changesets-release.yml | 2 +- .github/workflows/integrate.yml | 8 +- .github/workflows/shellcheck.yml | 2 +- .github/workflows/triage.yml | 2 +- .pre-commit-config.yaml | 2 +- .../pull-request/add-assignee/action.yml | 74 +++++++++++++++++++ .../github/pull-request/approve/action.yml | 65 ++++++++++++++++ actions/github/pull-request/merge/action.yml | 71 ++++++++++++++++++ .../pull-request/request-review/action.yml | 74 +++++++++++++++++++ 10 files changed, 293 insertions(+), 9 deletions(-) create mode 100644 actions/github/pull-request/add-assignee/action.yml create mode 100644 actions/github/pull-request/approve/action.yml create mode 100644 actions/github/pull-request/merge/action.yml create mode 100644 actions/github/pull-request/request-review/action.yml diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml index 5c314f2..58412b3 100644 --- a/.github/workflows/build-image.yml +++ b/.github/workflows/build-image.yml @@ -49,7 +49,7 @@ jobs: runs-on: ${{ inputs.os }} steps: - name: 📦 Check out the codebase - uses: actions/checkout@v4 + uses: actions/checkout@v4.1.7 - name: 🛠️ Install goss and dgoss uses: e1himself/goss-installation-action@v1.2.1 diff --git a/.github/workflows/create-changesets-release.yml b/.github/workflows/create-changesets-release.yml index 05865b5..5bb4dd4 100644 --- a/.github/workflows/create-changesets-release.yml +++ b/.github/workflows/create-changesets-release.yml @@ -31,7 +31,7 @@ jobs: steps: - name: 📦 Check out the codebase - uses: actions/checkout@v4 + uses: actions/checkout@v4.1.7 with: fetch-depth: 0 diff --git a/.github/workflows/integrate.yml b/.github/workflows/integrate.yml index 5421bca..78e0ed2 100644 --- a/.github/workflows/integrate.yml +++ b/.github/workflows/integrate.yml @@ -19,10 +19,10 @@ jobs: pull-requests: read steps: - name: 📦 Check out the codebase - uses: actions/checkout@v4.1.5 + uses: actions/checkout@v4.1.7 - name: 🧐 Lint commits using "commitlint" - uses: wagoid/commitlint-github-action@v6.0.1 + uses: wagoid/commitlint-github-action@v6.1.1 with: configFile: ${{ github.workspace }}/.github/.commitlint.config.mjs failOnWarnings: false @@ -37,7 +37,7 @@ jobs: group: coding-standards-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} steps: - name: 📦 Check out the codebase - uses: actions/checkout@v4.1.5 + uses: actions/checkout@v4.1.7 - name: 🧐 Lint YAML files uses: ibiqlik/action-yamllint@v3.1.1 @@ -54,7 +54,7 @@ jobs: group: markdown-linting-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} steps: - name: 📦 Check out the codebase - uses: actions/checkout@v4.1.5 + uses: actions/checkout@v4.1.7 - name: 🧐 Lint Markdown files uses: DavidAnson/markdownlint-cli2-action@v16.0.0 diff --git a/.github/workflows/shellcheck.yml b/.github/workflows/shellcheck.yml index e9b669b..de895d8 100644 --- a/.github/workflows/shellcheck.yml +++ b/.github/workflows/shellcheck.yml @@ -25,7 +25,7 @@ jobs: runs-on: ${{ inputs.os }} steps: - name: 📦 Check out the codebase - uses: actions/checkout@v4 + uses: actions/checkout@v4.1.7 with: fetch-depth: 0 diff --git a/.github/workflows/triage.yml b/.github/workflows/triage.yml index b21bf3f..784149b 100644 --- a/.github/workflows/triage.yml +++ b/.github/workflows/triage.yml @@ -16,7 +16,7 @@ name: 🏷️ Add labels jobs: label: - uses: wayofdev/gh-actions/.github/workflows/apply-labels.yml@v3.1.0 + uses: wayofdev/gh-actions/.github/workflows/apply-labels.yml@v3.1.1 with: os: ubuntu-latest secrets: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a4fa11f..e71e1ea 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: fix-encoding-pragma - repo: https://github.com/commitizen-tools/commitizen - rev: v3.24.0 + rev: v3.29.0 hooks: - id: commitizen stages: diff --git a/actions/github/pull-request/add-assignee/action.yml b/actions/github/pull-request/add-assignee/action.yml new file mode 100644 index 0000000..05a7929 --- /dev/null +++ b/actions/github/pull-request/add-assignee/action.yml @@ -0,0 +1,74 @@ +--- + +# 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/issues/assignees#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@v7.0.1 + with: + github-token: "${{ inputs.github-token }}" + script: | + if ( + context.eventName == 'pull_request' || + context.eventName == 'pull_request_target' + ) { + 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@v7.0.1 + env: + ASSIGNEE: "${{ inputs.assignee }}" + 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 = [ + process.env.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); + } diff --git a/actions/github/pull-request/approve/action.yml b/actions/github/pull-request/approve/action.yml new file mode 100644 index 0000000..1b06596 --- /dev/null +++ b/actions/github/pull-request/approve/action.yml @@ -0,0 +1,65 @@ +--- + +# 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/pulls/reviews#create-a-review-for-a-pull-request +# 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: ✅ Approve pull request + +description: Approves a pull request + +inputs: + github-token: + description: GitHub token of a user with permission to approve a pull request + required: true + +runs: + using: 'composite' + + steps: + - name: 🤔 Determine pull request number + uses: actions/github-script@v7.0.1 + with: + github-token: "${{ inputs.github-token }}" + script: | + if ( + context.eventName == 'pull_request' || + context.eventName == 'pull_request_target' + ) { + 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: ✅ Approve pull request + uses: actions/github-script@v7.0.1 + 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; + } + + try { + await github.rest.pulls.createReview({ + event: "APPROVE", + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: process.env.PULL_REQUEST_NUMBER, + }); + } catch (error) { + core.setFailed(error.message); + } diff --git a/actions/github/pull-request/merge/action.yml b/actions/github/pull-request/merge/action.yml new file mode 100644 index 0000000..601fbe6 --- /dev/null +++ b/actions/github/pull-request/merge/action.yml @@ -0,0 +1,71 @@ +--- + +# 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/pulls/pulls#merge-a-pull-request +# 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: 📥 Merge pull request + +description: Merges a pull request + +inputs: + github-token: + description: GitHub token of a user with permission to merge a pull request + required: true + merge-method: + default: merge + description: 'Which merge method to use, one "merge", "rebase", "squash"' + required: true + +runs: + using: 'composite' + + steps: + - name: 🤔 Determine pull request number + uses: actions/github-script@v7.0.1 + with: + github-token: "${{ inputs.github-token }}" + script: | + if ( + context.eventName == 'pull_request' || + context.eventName == 'pull_request_target' + ) { + 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: 📥 Merge pull request + uses: actions/github-script@v7.0.1 + env: + MERGE_METHOD: "${{ inputs.merge-method }}" + 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; + } + + try { + await github.rest.pulls.merge({ + merge_method: process.env.MERGE_METHOD, + owner: context.repo.owner, + pull_number: process.env.PULL_REQUEST_NUMBER, + repo: context.repo.repo, + }); + } catch (error) { + core.setFailed(error.message); + } diff --git a/actions/github/pull-request/request-review/action.yml b/actions/github/pull-request/request-review/action.yml new file mode 100644 index 0000000..b6a2de2 --- /dev/null +++ b/actions/github/pull-request/request-review/action.yml @@ -0,0 +1,74 @@ +--- + +# 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/pulls/review-requests#request-reviewers-for-a-pull-request +# 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: Request review from reviewer for pull request + +description: Requests a review from a reviewer for a pull request + +inputs: + github-token: + description: GitHub token of a user with permission to request reviewers for a pull request + required: true + reviewer: + description: Username of user to request review from for a pull request + required: true + +runs: + using: 'composite' + + steps: + - name: 🤔 Determine pull request number + uses: actions/github-script@v7.0.1 + with: + github-token: "${{ inputs.github-token }}" + script: | + if ( + context.eventName == 'pull_request' || + context.eventName == 'pull_request_target' + ) { + 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: "Request reviewer" + uses: "actions/github-script@v7.0.1" + env: + REVIEWER: "${{ inputs.reviewer }}" + 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 reviewers = [ + process.env.REVIEWER, + ]; + + try { + await github.rest.pulls.requestReviewers({ + owner: context.repo.owner, + pull_number: process.env.PULL_REQUEST_NUMBER, + repo: context.repo.repo, + reviewers: reviewers, + }); + } catch (error) { + core.setFailed(error.message); + }