|
| 1 | +# Copyright 2026 The Flutter Authors. All rights reserved. |
| 2 | +# Use of this source code is governed by a BSD-style license that can be |
| 3 | +# found in the LICENSE file. |
| 4 | + |
| 5 | +# Privileged half of the pull request labeler. |
| 6 | +# |
| 7 | +# Edit `.github/labeler.yml` to configure labels. |
| 8 | +# |
| 9 | +# Runs after `labeler-trigger.yml` completes. The artifact produced by that |
| 10 | +# workflow is UNTRUSTED (a pull request can modify the trigger workflow), so the |
| 11 | +# PR number is only used after binding it to the triggering run's head commit. |
| 12 | +# The label configuration is read from the default branch, not the PR. |
| 13 | +# |
| 14 | +# Pull requests from first-time contributors need a maintainer to approve their |
| 15 | +# `pull_request` runs. Until then, the trigger run is created already concluded |
| 16 | +# as `action_required`, without running anything or uploading an artifact. That |
| 17 | +# run fires the `requested` event (but not `completed`), so on `requested` with |
| 18 | +# `action_required` the PR is looked up from the run's head branch and SHA |
| 19 | +# instead. Otherwise those PRs would not be labeled until approved, and would |
| 20 | +# not show up in the triage queries where maintainers find runs to approve. |
| 21 | + |
| 22 | +name: Pull Request Labeler |
| 23 | + |
| 24 | +on: |
| 25 | + # Safe because this only consumes an artifact from the triggering run as |
| 26 | + # untrusted data (validated and bound to the PR's head SHA); no PR code is |
| 27 | + # checked out or executed. |
| 28 | + workflow_run: # zizmor: ignore[dangerous-triggers] |
| 29 | + workflows: [Pull Request Labeler Trigger] |
| 30 | + types: [requested, completed] |
| 31 | + |
| 32 | +# Declare default permissions as read only. |
| 33 | +permissions: |
| 34 | + contents: read |
| 35 | + |
| 36 | +jobs: |
| 37 | + triage: |
| 38 | + if: >- |
| 39 | + github.repository == 'flutter/packages' && |
| 40 | + github.event.workflow_run.event == 'pull_request' && |
| 41 | + github.event.workflow_run.path == '.github/workflows/labeler-trigger.yml' && |
| 42 | + ((github.event.action == 'completed' && github.event.workflow_run.conclusion == 'success') || |
| 43 | + (github.event.action == 'requested' && github.event.workflow_run.conclusion == 'action_required')) |
| 44 | + permissions: |
| 45 | + actions: read |
| 46 | + contents: read |
| 47 | + pull-requests: write |
| 48 | + runs-on: ubuntu-latest |
| 49 | + steps: |
| 50 | + - name: Download labeler metadata |
| 51 | + if: github.event.action == 'completed' |
| 52 | + uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 |
| 53 | + with: |
| 54 | + name: labeler-metadata |
| 55 | + path: ${{ runner.temp }}/labeler-metadata |
| 56 | + run-id: ${{ github.event.workflow_run.id }} |
| 57 | + github-token: ${{ github.token }} |
| 58 | + |
| 59 | + - name: Validate PR |
| 60 | + id: validate |
| 61 | + env: |
| 62 | + EVENT_ACTION: ${{ github.event.action }} |
| 63 | + GH_TOKEN: ${{ github.token }} |
| 64 | + REPO: ${{ github.repository }} |
| 65 | + RUN_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} |
| 66 | + RUN_HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }} |
| 67 | + RUN_HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }} |
| 68 | + RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }} |
| 69 | + run: | |
| 70 | + set -euo pipefail |
| 71 | + if [ "$EVENT_ACTION" = "requested" ]; then |
| 72 | + # Awaiting approval: no artifact, so find the open PR for the run's |
| 73 | + # head branch. This only uses data from GitHub's event payload. |
| 74 | + PR_NUMBER=$(gh api -X GET "repos/$REPO/pulls" \ |
| 75 | + -f state=open -f head="$RUN_HEAD_OWNER:$RUN_HEAD_BRANCH" | |
| 76 | + jq -r --arg sha "$RUN_HEAD_SHA" '[.[] | select(.head.sha == $sha)] | .[0].number // empty') |
| 77 | + if [ -z "$PR_NUMBER" ]; then |
| 78 | + echo "::notice::No open PR found for $RUN_HEAD_OWNER:$RUN_HEAD_BRANCH at $RUN_HEAD_SHA; skipping." |
| 79 | + exit 0 |
| 80 | + fi |
| 81 | + else |
| 82 | + PR_NUMBER=$(jq -r '.pr_number // empty' "$RUNNER_TEMP/labeler-metadata/metadata.json") |
| 83 | + fi |
| 84 | + if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then |
| 85 | + echo "::error::Invalid PR number." |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +
|
| 89 | + # Bind the PR number to the pull request that actually triggered the run. |
| 90 | + gh api "repos/$REPO/pulls/$PR_NUMBER" > "$RUNNER_TEMP/pr.json" |
| 91 | + if [ "$(jq -r '.head.sha' "$RUNNER_TEMP/pr.json")" != "$RUN_HEAD_SHA" ] || |
| 92 | + [ "$(jq -r '.head.repo.full_name // empty' "$RUNNER_TEMP/pr.json")" != "$RUN_HEAD_REPO" ]; then |
| 93 | + # Most likely a newer push; its own run will label the PR. |
| 94 | + echo "::notice::PR #$PR_NUMBER has changed since the trigger ran; skipping." |
| 95 | + exit 0 |
| 96 | + fi |
| 97 | + echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT" |
| 98 | +
|
| 99 | + # Source available at https://github.com/actions/labeler/blob/main/README.md |
| 100 | + - uses: actions/labeler@b8dd2d9be0f68b860e7dae5dae7d772984eacd6d # v6.2.0 |
| 101 | + if: steps.validate.outputs.pr_number != '' |
| 102 | + with: |
| 103 | + pr-number: ${{ steps.validate.outputs.pr_number }} |
| 104 | + sync-labels: true |
0 commit comments