Skip to content

Commit 0d56fde

Browse files
Replace pull_request_target workflows with pull_request + workflow_run (flutter#195536)
This resolves flutter#193185. GitHub is restricting `pull_request_target`, which runs with secrets and a write token on events from forks. Each affected workflow is split in two: * An unprivileged `pull_request` workflow with a read-only token and no secrets. Cheap gates (label name, base branch, bot author) are job-level `if:`s so non-matching events never allocate a runner. When it applies, it uploads a small artifact with context about the triggering event (PR number, and the cp channel). * A privileged `workflow_run` workflow that runs from the default branch with access to secrets. It downloads the artifact and does the actual work: commenting, labeling, reverting, cherry-picking, deleting branches. A PR can modify the `pull_request` workflow, so the artifact is untrusted. The privileged side ties the PR number to the triggering run's head SHA and repo. It then reads all other state from the GitHub API, including re-checking any gate whose failure would have made the trigger skip. Checks that end in a comment either way are done only on the privileged side. | Unprivileged (pull_request) | Privileged (workflow_run) | |---------------------------------|---------------------------| | revert-trigger.yml | revert.yml | | cp-trigger.yml | easy-cp.yml | | labeler-trigger.yml | labeler.yml | | delete-bot-branches-trigger.yml | delete-bot-branches.yml | | freeze.yml | freeze-comment.yml | | no-main-prs.yaml | no-main-prs-comment.yaml | freeze.yml and no-main-prs.yaml keep their names because they still provide the PR check. no-main-prs.yaml now gates on `base_ref == 'main'` at the job level, so it no longer uses a runner on every PR.
1 parent 7fb7236 commit 0d56fde

17 files changed

Lines changed: 1073 additions & 187 deletions

‎.agents/skills/flutter-cherry-pick/scripts/flutter_cp.dart‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,56 @@ class CherryPickHelper {
8888
]);
8989
}
9090

91+
/// Whether the original PR's merge commit contains the `cp-trigger.yml`
92+
/// workflow.
93+
///
94+
/// Adding a `cp:` label runs `cp-trigger.yml` from the PR's merge commit, so
95+
/// for PRs merged before that workflow existed, adding the label does nothing.
96+
Future<bool> hasCpTrigger() async {
97+
final String sha = switch (originalPrData) {
98+
{'mergeCommit': {'oid': final String oid}} => oid,
99+
_ => throw StateError('Could not find merge commit SHA in PR data'),
100+
};
101+
final ProcessResult result = await runCmd([
102+
'gh',
103+
'api',
104+
'repos/flutter/flutter/contents/.github/workflows/cp-trigger.yml?ref=$sha',
105+
'--silent',
106+
], allowFailure: true);
107+
return result.exitCode == 0;
108+
}
109+
110+
/// Runs the `easy-cp.yml` workflow directly via `workflow_dispatch`.
111+
///
112+
/// This is the fallback for PRs merged before `cp-trigger.yml` existed.
113+
/// Requires write access to flutter/flutter.
114+
Future<void> dispatchWorkflow() async {
115+
_info('Running easy-cp.yml for PR #$pr to $channel via workflow_dispatch...');
116+
await runCmd([
117+
'gh',
118+
'workflow',
119+
'run',
120+
'easy-cp.yml',
121+
'-R',
122+
'flutter/flutter',
123+
'-f',
124+
'pr_number=$pr',
125+
'-f',
126+
'channel=$channel',
127+
]);
128+
}
129+
130+
/// Starts the automated cherry-pick, by adding the `cp:` label or, for PRs
131+
/// merged before `cp-trigger.yml` existed, by dispatching `easy-cp.yml`.
132+
Future<void> triggerAutomatedCp() async {
133+
if (await hasCpTrigger()) {
134+
await addLabel();
135+
} else {
136+
_info('PR #$pr was merged before cp-trigger.yml existed; adding a label would do nothing.');
137+
await dispatchWorkflow();
138+
}
139+
}
140+
91141
/// Dynamically retrieves the release candidate branch name for the target channel.
92142
///
93143
/// Reads the version file `bin/internal/release-candidate-branch.version` from
@@ -172,7 +222,9 @@ class CherryPickHelper {
172222
/// the original PR number or title in its title.
173223
Future<int?> pollForPr(String candidateBranch) async {
174224
_info('Polling for automated cherry-pick PR targeting $candidateBranch...');
175-
const attempts = 8;
225+
// The automated cherry-pick runs as two chained workflows (cp-trigger.yml,
226+
// then easy-cp.yml), which usually takes 1-2 minutes.
227+
const attempts = 12;
176228
for (var i = 0; i < attempts; i++) {
177229
await Future<void>.delayed(const Duration(seconds: 15));
178230
_info('Polling attempt ${i + 1}/$attempts...');
@@ -379,7 +431,7 @@ void main(List<String> arguments) async {
379431
final String candidateBranch = await helper.getCandidateBranch();
380432

381433
if (action == 'start') {
382-
await helper.addLabel();
434+
await helper.triggerAutomatedCp();
383435
// Wait a bit before polling to let GitHub Actions trigger
384436
await Future<void>.delayed(const Duration(seconds: 5));
385437
final int? cpPr = await helper.pollForPr(candidateBranch);

‎.github/workflows/cp-trigger.yml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright 2023 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+
# Unprivileged half of the cherry-pick automation.
6+
#
7+
# This workflow runs on `pull_request` with a read-only token and no access to
8+
# secrets. When a `cp: beta` or `cp: stable` label is applied, it records the
9+
# pull request number and channel in a `cp-metadata` artifact. The privileged
10+
# `easy-cp.yml` workflow consumes that artifact via `workflow_run`, validates
11+
# the request, and either performs the cherry-pick or comments on the pull
12+
# request explaining why it can't.
13+
14+
name: Cherry-pick Trigger
15+
16+
on:
17+
pull_request:
18+
branches: [master]
19+
types: [labeled]
20+
21+
permissions:
22+
contents: read
23+
24+
jobs:
25+
record:
26+
name: Record cherry-pick request
27+
runs-on: ubuntu-latest
28+
# Evaluated before a runner is allocated, so other labels cost nothing.
29+
if: >-
30+
github.repository == 'flutter/flutter' &&
31+
(github.event.label.name == format('cp{0} beta', ':') || github.event.label.name == format('cp{0} stable', ':'))
32+
steps:
33+
- name: Record PR number and channel
34+
env:
35+
LABEL_NAME: ${{ github.event.label.name }}
36+
PR_NUMBER: ${{ github.event.pull_request.number }}
37+
run: |
38+
mkdir -p cp-metadata
39+
CHANNEL=$(echo "$LABEL_NAME" | cut -d ':' -f 2 | xargs)
40+
jq -n --arg pr "$PR_NUMBER" --arg channel "$CHANNEL" \
41+
'{pr_number: $pr, channel: $channel}' > cp-metadata/metadata.json
42+
43+
- name: Upload cherry-pick metadata
44+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
45+
with:
46+
name: cp-metadata
47+
path: cp-metadata/
48+
retention-days: 1
49+
if-no-files-found: error
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2013 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+
# Unprivileged half of the bot branch cleanup.
6+
#
7+
# This workflow runs on `pull_request` with a read-only token and no access to
8+
# secrets. When a `flutteractionsbot` pull request is closed, it records the
9+
# pull request number in a `delete-branch-metadata` artifact so that the
10+
# privileged `delete-bot-branches.yml` workflow can delete the head branch via
11+
# `workflow_run`.
12+
13+
name: Delete Bot Branches Trigger
14+
15+
on:
16+
pull_request:
17+
types: [closed]
18+
branches:
19+
- master
20+
- 'flutter-*-candidate*'
21+
22+
permissions:
23+
contents: read
24+
25+
jobs:
26+
record:
27+
name: Record bot PR
28+
runs-on: ubuntu-latest
29+
# Evaluated before a runner is allocated, so non-bot PRs cost nothing.
30+
if: |
31+
github.repository == 'flutter/flutter' &&
32+
github.event.pull_request.head.repo != null &&
33+
github.event.pull_request.head.repo.owner.login == 'flutteractionsbot' &&
34+
github.event.pull_request.head.ref != 'master' &&
35+
github.event.pull_request.head.ref != 'main'
36+
steps:
37+
- name: Record PR number
38+
env:
39+
PR_NUMBER: ${{ github.event.pull_request.number }}
40+
run: |
41+
mkdir -p delete-branch-metadata
42+
jq -n --arg pr "$PR_NUMBER" '{pr_number: $pr}' > delete-branch-metadata/metadata.json
43+
44+
- name: Upload metadata
45+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
46+
with:
47+
name: delete-branch-metadata
48+
path: delete-branch-metadata/
49+
retention-days: 1
50+
if-no-files-found: error

‎.github/workflows/delete-bot-branches.yml‎

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,97 @@
22
# Use of this source code is governed by a BSD-style license that can be
33
# found in the LICENSE file.
44

5+
# Privileged half of the bot branch cleanup.
6+
#
7+
# Runs after `delete-bot-branches-trigger.yml` completes. The artifact produced
8+
# by that workflow is UNTRUSTED (a pull request can modify the trigger
9+
# workflow), so the PR number is only used after binding it to the triggering
10+
# run's head commit, and the branch to delete is read from the GitHub API and
11+
# re-checked against the same conditions as the trigger.
12+
513
name: Delete Bot Branches
614

715
on:
8-
pull_request_target:
9-
types: [closed]
10-
branches:
11-
- master
12-
- 'flutter-*-candidate*'
16+
# Safe because this only consumes an artifact from the triggering run as
17+
# untrusted data (validated and bound to the PR's head SHA); no PR code is
18+
# checked out or executed.
19+
workflow_run: # zizmor: ignore[dangerous-triggers]
20+
workflows: [Delete Bot Branches Trigger]
21+
types: [completed]
1322

1423
permissions:
15-
contents: write
24+
contents: read
1625

1726
jobs:
1827
delete-branch:
1928
runs-on: ubuntu-latest
20-
if: |
21-
github.event.pull_request.head.repo != null &&
22-
github.event.pull_request.head.repo.owner.login == 'flutteractionsbot' &&
23-
github.event.pull_request.head.ref != 'master' &&
24-
github.event.pull_request.head.ref != 'main'
29+
# Runs where every job was skipped (i.e. a non-bot PR) conclude as
30+
# `skipped`, so this check avoids allocating a runner for them.
31+
if: >-
32+
github.event.workflow_run.conclusion == 'success' &&
33+
github.event.workflow_run.event == 'pull_request' &&
34+
github.event.workflow_run.path == '.github/workflows/delete-bot-branches-trigger.yml'
35+
permissions:
36+
actions: read
37+
contents: read
38+
pull-requests: read
2539
steps:
40+
- name: Download metadata
41+
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
42+
with:
43+
name: delete-branch-metadata
44+
path: ${{ runner.temp }}/delete-branch-metadata
45+
run-id: ${{ github.event.workflow_run.id }}
46+
github-token: ${{ github.token }}
47+
48+
- name: Validate PR
49+
id: validate
50+
env:
51+
GH_TOKEN: ${{ github.token }}
52+
REPO: ${{ github.repository }}
53+
RUN_HEAD_REPO: ${{ github.event.workflow_run.head_repository.full_name }}
54+
RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
55+
run: |
56+
set -euo pipefail
57+
PR_NUMBER=$(jq -r '.pr_number // empty' "$RUNNER_TEMP/delete-branch-metadata/metadata.json")
58+
if ! [[ "$PR_NUMBER" =~ ^[1-9][0-9]*$ ]]; then
59+
echo "::error::Artifact contains an invalid PR number."
60+
exit 1
61+
fi
62+
63+
gh api "repos/$REPO/pulls/$PR_NUMBER" > "$RUNNER_TEMP/pr.json"
64+
PR="$RUNNER_TEMP/pr.json"
65+
66+
# Bind the artifact to the pull request that actually triggered the run.
67+
if [ "$(jq -r '.head.sha' "$PR")" != "$RUN_HEAD_SHA" ] ||
68+
[ "$(jq -r '.head.repo.full_name // empty' "$PR")" != "$RUN_HEAD_REPO" ]; then
69+
echo "::error::PR #$PR_NUMBER does not match the triggering workflow run."
70+
exit 1
71+
fi
72+
73+
# Re-check the trigger conditions against the API.
74+
STATE=$(jq -r '.state' "$PR")
75+
OWNER=$(jq -r '.head.repo.owner.login // empty' "$PR")
76+
NAME=$(jq -r '.head.repo.name // empty' "$PR")
77+
BRANCH=$(jq -r '.head.ref' "$PR")
78+
if [ "$STATE" != "closed" ] || [ "$OWNER" != "flutteractionsbot" ] ||
79+
[ "$BRANCH" = "master" ] || [ "$BRANCH" = "main" ]; then
80+
echo "::error::PR #$PR_NUMBER is not a closed flutteractionsbot PR."
81+
exit 1
82+
fi
83+
84+
{
85+
echo "repo_owner=$OWNER"
86+
echo "repo_name=$NAME"
87+
echo "branch_name=$BRANCH"
88+
} >> "$GITHUB_OUTPUT"
89+
2690
- name: Delete branch
2791
env:
2892
GH_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
29-
REPO_OWNER: ${{ github.event.pull_request.head.repo.owner.login }}
30-
REPO_NAME: ${{ github.event.pull_request.head.repo.name }}
31-
BRANCH_NAME: ${{ github.event.pull_request.head.ref }}
93+
REPO_OWNER: ${{ steps.validate.outputs.repo_owner }}
94+
REPO_NAME: ${{ steps.validate.outputs.repo_name }}
95+
BRANCH_NAME: ${{ steps.validate.outputs.branch_name }}
3296
run: |
3397
echo "Deleting branch $BRANCH_NAME from $REPO_OWNER/$REPO_NAME..."
3498
gh api -X DELETE "/repos/$REPO_OWNER/$REPO_NAME/git/refs/heads/$BRANCH_NAME"

0 commit comments

Comments
 (0)