|
| 1 | +name: "Pull Request Labeler Apply" |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["Pull Request Labeler"] |
| 6 | + types: [completed] |
| 7 | + |
| 8 | +permissions: {} |
| 9 | + |
| 10 | +jobs: |
| 11 | + triage: |
| 12 | + if: > |
| 13 | + github.event.workflow_run.event == 'pull_request' && |
| 14 | + github.event.workflow_run.conclusion == 'success' |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + actions: read |
| 18 | + contents: read |
| 19 | + pull-requests: write |
| 20 | + steps: |
| 21 | + - name: Validate pull request from workflow_run |
| 22 | + id: validate |
| 23 | + env: |
| 24 | + GH_TOKEN: ${{ github.token }} |
| 25 | + REPO: ${{ github.repository }} |
| 26 | + RUN_ID: ${{ github.event.workflow_run.id }} |
| 27 | + run: | |
| 28 | + set -euo pipefail |
| 29 | +
|
| 30 | + run_json=$(gh api "repos/$REPO/actions/runs/$RUN_ID") |
| 31 | + event=$(jq -r '.event' <<<"$run_json") |
| 32 | + conclusion=$(jq -r '.conclusion' <<<"$run_json") |
| 33 | + head_sha=$(jq -r '.head_sha' <<<"$run_json") |
| 34 | + head_repo=$(jq -r '.head_repository.full_name // empty' <<<"$run_json") |
| 35 | +
|
| 36 | + if [ "$event" != "pull_request" ] || [ "$conclusion" != "success" ]; then |
| 37 | + echo "apply=false" >> "$GITHUB_OUTPUT" |
| 38 | + exit 0 |
| 39 | + fi |
| 40 | +
|
| 41 | + pr_number=$(jq -r 'if (.pull_requests | length) == 1 then .pull_requests[0].number else empty end' <<<"$run_json") |
| 42 | +
|
| 43 | + if [ -z "$pr_number" ]; then |
| 44 | + prs_json=$(gh api -H 'Accept: application/vnd.github+json' "repos/$REPO/commits/$head_sha/pulls") |
| 45 | + pr_number=$(jq -r 'map(select(.state == "open")) | if length == 1 then .[0].number else empty end' <<<"$prs_json") |
| 46 | + fi |
| 47 | +
|
| 48 | + if [ -z "$pr_number" ]; then |
| 49 | + head_owner=$(jq -r '.head_repository.owner.login // empty' <<<"$run_json") |
| 50 | + head_branch=$(jq -r '.head_branch // empty' <<<"$run_json") |
| 51 | + if [ -n "$head_owner" ] && [ -n "$head_branch" ]; then |
| 52 | + prs_json=$(gh api --method GET "repos/$REPO/pulls" -f state=open -f head="$head_owner:$head_branch") |
| 53 | + pr_number=$(jq -r 'if length == 1 then .[0].number else empty end' <<<"$prs_json") |
| 54 | + fi |
| 55 | + fi |
| 56 | +
|
| 57 | + if [ -z "$pr_number" ]; then |
| 58 | + echo "Could not identify a unique open pull request for workflow run $RUN_ID; skipping." |
| 59 | + echo "apply=false" >> "$GITHUB_OUTPUT" |
| 60 | + exit 0 |
| 61 | + fi |
| 62 | +
|
| 63 | + pr_json=$(gh api "repos/$REPO/pulls/$pr_number") |
| 64 | + state=$(jq -r '.state' <<<"$pr_json") |
| 65 | + base_repo=$(jq -r '.base.repo.full_name' <<<"$pr_json") |
| 66 | + pr_head_sha=$(jq -r '.head.sha' <<<"$pr_json") |
| 67 | + pr_head_repo=$(jq -r '.head.repo.full_name // empty' <<<"$pr_json") |
| 68 | +
|
| 69 | + if [ "$state" != "open" ] || [ "$base_repo" != "$REPO" ]; then |
| 70 | + echo "Pull request #$pr_number is no longer an open pull request against $REPO; skipping." |
| 71 | + echo "apply=false" >> "$GITHUB_OUTPUT" |
| 72 | + exit 0 |
| 73 | + fi |
| 74 | +
|
| 75 | + if [ "$pr_head_sha" != "$head_sha" ] || [ "$pr_head_repo" != "$head_repo" ]; then |
| 76 | + echo "Pull request #$pr_number changed since workflow run $RUN_ID; skipping stale labeling." |
| 77 | + echo "apply=false" >> "$GITHUB_OUTPUT" |
| 78 | + exit 0 |
| 79 | + fi |
| 80 | +
|
| 81 | + echo "number=$pr_number" >> "$GITHUB_OUTPUT" |
| 82 | + echo "apply=true" >> "$GITHUB_OUTPUT" |
| 83 | +
|
| 84 | + - uses: actions/labeler@v4 |
| 85 | + if: steps.validate.outputs.apply == 'true' |
| 86 | + with: |
| 87 | + repo-token: "${{ github.token }}" |
| 88 | + pr-number: ${{ steps.validate.outputs.number }} |
0 commit comments