blob: 1e45c52d38e7aeb8572774c79807c4b61d8648d6 [file] [edit]
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Privileged half of the pull request labeler.
#
# Runs after `labeler-trigger.yml` completes. The artifact produced by that
# workflow is UNTRUSTED (a pull request can modify the trigger workflow), so the
# PR number is only used after binding it to the triggering run's head commit.
# The label configuration is read from the default branch, not the PR.
#
# Pull requests from first-time contributors need a maintainer to approve their
# `pull_request` runs. Until then, the trigger run is created already concluded
# as `action_required`, without running anything or uploading an artifact. That
# run fires the `requested` event (but not `completed`), so on `requested` with
# `action_required` the PR is looked up from the run's head branch and SHA
# instead. Otherwise those PRs would get no `team-*` labels, and would not show
# up in the triage queries where maintainers find runs to approve.
name: "Pull Request Labeler"
on:
# Safe because this only consumes an artifact from the triggering run as
# untrusted data (validated and bound to the PR's head SHA); no PR code is
# checked out or executed.
workflow_run: # zizmor: ignore[dangerous-triggers]
workflows: [Pull Request Labeler Trigger]
types: [requested, completed]
# Declare default permissions as read only.
permissions: read-all
jobs:
triage:
if: >-
github.repository == 'flutter/flutter' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.path == '.github/workflows/labeler-trigger.yml' &&
((github.event.action == 'completed' && github.event.workflow_run.conclusion == 'success') ||
(github.event.action == 'requested' && github.event.workflow_run.conclusion == 'action_required'))
permissions:
actions: read
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Download labeler metadata
if: github.event.action == 'completed'
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: labeler-metadata
path: ${{ runner.temp }}/labeler-metadata
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Validate PR
id: validate
env:
EVENT_ACTION: ${{ github.event.action }}
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
RUN_HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
RUN_HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }}
RUN_HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }}
RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
set -euo pipefail
if [ "$EVENT_ACTION" = "requested" ]; then
# Awaiting approval: no artifact, so find the open PR for the run's
# head branch. This only uses data from GitHub's event payload.
PR_NUMBER=$(gh api -X GET "repos/$REPO/pulls" \
-f state=open -f head="$RUN_HEAD_OWNER:$RUN_HEAD_BRANCH" |
jq -r --arg sha "$RUN_HEAD_SHA" '[.[] | select(.head.sha == $sha)] | .[0].number // empty')
if [ -z "$PR_NUMBER" ]; then
echo "::notice::No open PR found for $RUN_HEAD_OWNER:$RUN_HEAD_BRANCH at $RUN_HEAD_SHA; skipping."
exit 0
fi
else
PR_NUMBER=$(jq -r '.pr_number // empty' "$RUNNER_TEMP/labeler-metadata/metadata.json")
fi
if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then
echo "::error::Invalid PR number."
exit 1
fi
# Bind the PR number to the pull request that actually triggered the run.
gh api "repos/$REPO/pulls/$PR_NUMBER" > "$RUNNER_TEMP/pr.json"
if [ "$(jq -r '.head.sha' "$RUNNER_TEMP/pr.json")" != "$RUN_HEAD_SHA" ] ||
[ "$(jq -r '.head.repo.full_name // empty' "$RUNNER_TEMP/pr.json")" != "$RUN_HEAD_REPO" ]; then
# Most likely a newer push; its own run will label the PR.
echo "::notice::PR #$PR_NUMBER has changed since the trigger ran; skipping."
exit 0
fi
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
# Source available at https://github.com/actions/labeler/blob/main/README.md
- uses: actions/labeler@bf12e9b00b37c5c0ca2b87b79b2daf7891dbda13
if: steps.validate.outputs.pr_number != ''
with:
pr-number: ${{ steps.validate.outputs.pr_number }}
sync-labels: true