|
| 1 | +name: "PR Is Mergeable" |
| 2 | +description: "Checks whether the provided PR is approved and all status checks either succeeded or have been skipped" |
| 3 | + |
| 4 | +inputs: |
| 5 | + pr-ref: |
| 6 | + description: "The reference (i.e. either number or a branch) of the PR to check" |
| 7 | + required: true |
| 8 | + fail-on-unmergeable: |
| 9 | + description: "Whether to fail the action if the PR is not mergeable" |
| 10 | + required: false |
| 11 | + default: "true" |
| 12 | + repo: |
| 13 | + description: "The repository of the PR" |
| 14 | + required: false |
| 15 | + default: ${{ github.repository }} |
| 16 | + token: |
| 17 | + description: "The GitHub access token (with PR read permissions) to access the PR" |
| 18 | + required: false |
| 19 | + default: ${{ github.token }} |
| 20 | + excluded-check-runs: |
| 21 | + description: "A comma-separated list of workflow names that are excluded from the Check Runs check" |
| 22 | + required: false |
| 23 | + default: ${{ github.workflow }} |
| 24 | + |
| 25 | +outputs: |
| 26 | + pr-number: |
| 27 | + description: "The number of the PR that was checked" |
| 28 | + value: ${{ steps.check.outputs.PR_NUMBER }} |
| 29 | + is-mergeable: |
| 30 | + description: "Whether the PR is mergeable" |
| 31 | + value: ${{ steps.check.outputs.RESULT }} |
| 32 | + |
| 33 | +runs: |
| 34 | + using: composite |
| 35 | + steps: |
| 36 | + - name: "Print Action Start" |
| 37 | + run: echo ">>>>> Starting PR Is Mergeable Action; inputs = ${{ toJson(inputs) }}" |
| 38 | + shell: bash |
| 39 | + |
| 40 | + - name: "Check Whether PR Is Mergeable" |
| 41 | + id: check |
| 42 | + run: | |
| 43 | + PR_JSON=$(gh pr view "${{ inputs.pr-ref }}" --repo "${{ inputs.repo }}" --json number,mergeable,reviewDecision,statusCheckRollup) |
| 44 | + |
| 45 | + PR_NUMBER=$(jq -r '.number' <<< "$PR_JSON") |
| 46 | + PR_MERGEABLE=$(jq -r '.mergeable' <<< "$PR_JSON") |
| 47 | + PR_DECISION=$(jq -r '.reviewDecision' <<< "$PR_JSON") |
| 48 | + |
| 49 | + echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 50 | + echo "[DEBUG] PR #$PR_NUMBER (in ${{ inputs.repo }}) is mergeable: $PR_MERGEABLE with decision $PR_DECISION" |
| 51 | + |
| 52 | + if [[ "$PR_DECISION" != "APPROVED" ]]; then |
| 53 | + echo "PR #$PR_NUMBER (in ${{ inputs.repo }}) has not been approved." |
| 54 | + echo "RESULT=false" >> $GITHUB_OUTPUT |
| 55 | + exit 0 |
| 56 | + fi |
| 57 | + |
| 58 | + if [[ "$PR_MERGEABLE" != "MERGEABLE" ]]; then |
| 59 | + echo "PR #$PR_NUMBER (in ${{ inputs.repo }}) is not mergeable (i.e. there are conflicts)." |
| 60 | + echo "RESULT=false" >> $GITHUB_OUTPUT |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | + |
| 64 | + PR_CHECKS=$(jq -r '.statusCheckRollup' <<< "$PR_JSON") |
| 65 | + |
| 66 | + # check runs are things like our CI pipeline |
| 67 | + FAILED_CHECK_RUNS=$(jq -r '.[] | select(.__typename == "CheckRun" and .conclusion != "SUCCESS" and .conclusion != "NEUTRAL")' <<< "$PR_CHECKS") |
| 68 | + IFS=',' read -ra EXCLUDED_WORKFLOWS <<< "${{ inputs.excluded-check-runs }}" |
| 69 | + for EXCLUDED_WORKFLOW in "${EXCLUDED_WORKFLOWS[@]}"; do |
| 70 | + if [[ -z "$FAILED_CHECK_RUNS" ]]; then |
| 71 | + break |
| 72 | + fi |
| 73 | + |
| 74 | + FAILED_CHECK_RUNS=$(jq -r 'select(.workflowName != "$EXCLUDED_WORKFLOW")' <<< "$FAILED_CHECK_RUNS") |
| 75 | + done |
| 76 | + |
| 77 | + if [[ -n "$FAILED_CHECK_RUNS" ]]; then |
| 78 | + echo "PR #$PR_NUMBER (in ${{ inputs.repo }}) contains failed check runs: " |
| 79 | + echo "$FAILED_CHECK_RUNS" |
| 80 | + echo "RESULT=false" >> $GITHUB_OUTPUT |
| 81 | + exit 0 |
| 82 | + fi |
| 83 | + |
| 84 | + # context checks are things like the license agreement check |
| 85 | + FAILED_CONTEXT_CHECKS=$(jq -r '.[] | select(.__typename == "StatusContext" and .state != "SUCCESS" and .state != "NEUTRAL")' <<< "$PR_CHECKS") |
| 86 | + if [[ -n "$FAILED_CONTEXT_CHECKS" ]]; then |
| 87 | + echo "PR #$PR_NUMBER (in ${{ inputs.repo }}) contains failed context checks: " |
| 88 | + echo "$FAILED_CONTEXT_CHECKS" |
| 89 | + echo "RESULT=false" >> $GITHUB_OUTPUT |
| 90 | + exit 0 |
| 91 | + fi |
| 92 | + |
| 93 | + echo "RESULT=true" >> $GITHUB_OUTPUT |
| 94 | + shell: bash |
| 95 | + env: |
| 96 | + GH_TOKEN: ${{ inputs.token }} |
| 97 | + |
| 98 | + - name: "Fail If PR Is Not Mergeable" |
| 99 | + if: ${{ inputs.fail-on-unmergeable == 'true' && steps.check.outputs.RESULT != 'true' }} |
| 100 | + run: exit 1 |
| 101 | + shell: bash |
| 102 | + |
| 103 | + - name: "Print Action End" |
| 104 | + if: always() |
| 105 | + run: echo "<<<<< Finished PR Is Mergeable Action" |
| 106 | + shell: bash |
0 commit comments