|
| 1 | +name: Get PR Deployments |
| 2 | +description: Action that returns how many deployments were made in a PR (both from commits and !redeploys) |
| 3 | +author: Tommy Gatti |
| 4 | +inputs: |
| 5 | + pr: |
| 6 | + required: true |
| 7 | + description: The pull request number to check for deployments of all kinds |
| 8 | + repository: |
| 9 | + required: false |
| 10 | + default: ${{ github.repository }} |
| 11 | + description: The repository to check for deployments |
| 12 | + token: |
| 13 | + required: false |
| 14 | + default: ${{ github.token }} |
| 15 | + description: The GitHub token to use for API requests |
| 16 | +outputs: |
| 17 | + deployments: |
| 18 | + description: The total number of deployments for the given PR (from commits and !redeploys) |
| 19 | + value: ${{ steps.total.outputs.number }} |
| 20 | +runs: |
| 21 | + using: composite |
| 22 | + # Essentially, count all the deployment entries that match the given branch, as well as |
| 23 | + # all the `!redeploy` comments, to get the next deployment number. |
| 24 | + # See https://docs.github.com/en/rest/deployments/deployments?apiVersion=2022-11-28#list-deployments |
| 25 | + steps: |
| 26 | + - name: Get PR HEAD |
| 27 | + id: pr |
| 28 | + shell: bash |
| 29 | + env: |
| 30 | + GH_TOKEN: ${{ inputs.token }} |
| 31 | + run: | |
| 32 | + head=$(gh pr view ${{ inputs.pr }} --repo ${{ inputs.repository }} --json headRefName --jq .headRefName) |
| 33 | + echo "HEAD of PR #${{ inputs.pr }} in ${{ inputs.repository }} is $head" |
| 34 | + echo "head=$head" >> $GITHUB_OUTPUT |
| 35 | +
|
| 36 | + - name: Get commit deployments for PR |
| 37 | + id: commit |
| 38 | + shell: bash |
| 39 | + env: |
| 40 | + GH_TOKEN: ${{ inputs.token }} |
| 41 | + # We --slurp the results because --paginate introduces potentially multiple array results |
| 42 | + run: | |
| 43 | + deployments=$(gh api \ |
| 44 | + -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \ |
| 45 | + --paginate --slurp \ |
| 46 | + /repos/${{ inputs.repository }}/deployments \ |
| 47 | + | jq '[select(.[][].ref == "${{ steps.pr.outputs.head }}")] | length' |
| 48 | + ) |
| 49 | + echo "Found $deployments deployments for PR #${{ inputs.pr }} in ${{ inputs.repository }}" |
| 50 | + echo "deployments=$deployments" >> $GITHUB_OUTPUT |
| 51 | +
|
| 52 | + - name: Get !redeploys from comments |
| 53 | + id: comment |
| 54 | + shell: bash |
| 55 | + env: |
| 56 | + GH_TOKEN: ${{ inputs.token }} |
| 57 | + run: | |
| 58 | + redeployments=$(gh pr view ${{ inputs.pr }} --repo ${{ github.repository }} \ |
| 59 | + --json comments \ |
| 60 | + --jq '[.comments[] | select(.body | startswith("!redeploy"))] | length' |
| 61 | + ) |
| 62 | + echo "Found $redeployments redeploy comments for PR #${{ inputs.pr }} in ${{ inputs.repository }}" |
| 63 | + echo "redeployments=$redeployments" >> $GITHUB_OUTPUT |
| 64 | +
|
| 65 | + - name: Return total deployments |
| 66 | + id: total |
| 67 | + shell: bash |
| 68 | + run: | |
| 69 | + total=$(( ${{ steps.commit.outputs.deployments }} + ${{ steps.comment.outputs.redeployments }} )) |
| 70 | + echo "Found $total deployments across commits (${{ steps.commit.outputs.deployments }}) and redeploys (${{ steps.comment.outputs.redeployments }}) for PR #${{ inputs.pr }} in ${{ inputs.repository }}" |
| 71 | + echo "number=$total" >> $GITHUB_OUTPUT |
0 commit comments