Skip to content

Commit b1c05d9

Browse files
authored
[ci] Checks unmerged batch release sync back before next release (flutter#12868)
To avoid the new batch release is created before sync back pr is merged. The workflow now checks for unmerged release branch before create a new one ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent c41644a commit b1c05d9

10 files changed

Lines changed: 498 additions & 55 deletions

‎.github/workflows/batch_release_pr.yml‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ on:
44
repository_dispatch:
55
types: [batch-release-pr]
66

7+
# Declare default permissions as read only.
8+
permissions: read-all
9+
710
env:
811
HEAD_BRANCH_NAME: ${{ github.event.client_payload.package }}-${{ github.run_id }}-${{ github.run_attempt }}
912

@@ -15,6 +18,7 @@ jobs:
1518
outputs:
1619
head_branch_created: ${{ steps.check-branch-exists.outputs.exists }}
1720
release_branch: ${{ steps.create-branch.outputs.release_branch }} # e.g. release-go_router-17.2.2, returned by branches-for-batch-release tool.
21+
blocking_branch: ${{ steps.check-in-flight.outputs.blocking_branch }} # Set only when an earlier release hasn't been merged back to main yet.
1822
steps:
1923
- name: checkout repository
2024
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
@@ -26,11 +30,20 @@ jobs:
2630
- name: Set up tools
2731
run: dart pub get
2832
working-directory: ${{ github.workspace }}/script/tool
33+
# A new release can't be cut while an earlier one is still in flight. This
34+
# only reports that; the unblock_sync and report_blocked_release jobs act
35+
# on it.
36+
- name: check for an in-flight release
37+
id: check-in-flight
38+
run: dart ./script/tool/lib/src/main.dart in-flight-release-check --packages=${GITHUB_EVENT_CLIENT_PAYLOAD_PACKAGE} --remote=origin
39+
env:
40+
GITHUB_EVENT_CLIENT_PAYLOAD_PACKAGE: ${{ github.event.client_payload.package }}
2941
# This step is to create branches for a batch release
3042
# Branches may not be created if there is nothing to release.
3143
# In that case, the workflow will exit and complete successfully.
3244
- name: create batch release branch
3345
id: create-branch
46+
if: steps.check-in-flight.outputs.blocking_branch == ''
3447
run: |
3548
gh auth setup-git
3649
git config --global user.name "flutteractionsbot"
@@ -76,4 +89,38 @@ jobs:
7689
GITHUB_EVENT_CLIENT_PAYLOAD_PACKAGE: ${{ github.event.client_payload.package }}
7790
GH_TOKEN: ${{ secrets.FLUTTERGITHUBBOT_TOKEN }}
7891

92+
# If a previous release hasn't been merged back yet, no new release can be
93+
# cut. Make sure the sync PR for it exists, and nudge it if it does.
94+
unblock_sync:
95+
needs: create_batch_release_branch
96+
if: needs.create_batch_release_branch.outputs.blocking_branch != ''
97+
permissions:
98+
contents: write
99+
pull-requests: write
100+
uses: ./.github/workflows/reusable_sync_to_main.yml
101+
with:
102+
branch-name: ${{ needs.create_batch_release_branch.outputs.blocking_branch }}
103+
comment-if-exists: >-
104+
This PR is blocking the next batch release: no new release branch can be
105+
cut until the version bump and changelog on
106+
`${{ needs.create_batch_release_branch.outputs.blocking_branch }}` are on
107+
`main`. That release is already published, so these changes have to
108+
land. If this PR can't be landed as-is, apply the same version bump and
109+
changelog to `main` by hand.
110+
secrets:
111+
BOT_TOKEN: ${{ secrets.FLUTTERGITHUBBOT_TOKEN }}
79112

113+
# Surface the skipped release, since a silently green scheduled run would hide
114+
# it until someone noticed the missing release.
115+
report_blocked_release:
116+
needs: [create_batch_release_branch, unblock_sync]
117+
if: ${{ !cancelled() && needs.create_batch_release_branch.outputs.blocking_branch != '' }}
118+
permissions: {} # Only writes a log annotation.
119+
runs-on: ubuntu-latest
120+
steps:
121+
- name: Report blocked release
122+
run: |
123+
echo "::error::No batch release was created: ${BLOCKING_BRANCH} has not been merged back into main."
124+
exit 1
125+
env:
126+
BLOCKING_BRANCH: ${{ needs.create_batch_release_branch.outputs.blocking_branch }}

‎.github/workflows/release_from_branches.yml‎

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,13 @@ jobs:
2121

2222
sync_to_main:
2323
needs: release
24-
if: github.repository_owner == 'flutter' && needs.release.result == 'success'
25-
runs-on: ubuntu-latest
24+
if: needs.release.result == 'success'
2625
permissions:
2726
# Need write permission to create PR.
2827
contents: write
2928
pull-requests: write
30-
steps:
31-
- name: Checkout repository
32-
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
33-
with:
34-
fetch-depth: 0 # Fetch history to allow branch comparison
35-
persist-credentials: false
36-
37-
- name: Create Pull Request
38-
env:
39-
GH_TOKEN: ${{ secrets.FLUTTERGITHUBBOT_TOKEN }}
40-
run: |
41-
gh auth setup-git
42-
# 1. Fetch main so the runner can see the difference
43-
git fetch origin main
44-
45-
# 2. Verify there are actually new commits to sync
46-
# This prevents the "GraphQL: No commits between main..." error
47-
COMMITS_COUNT=$(git rev-list --count origin/main..HEAD)
48-
49-
if [ "$COMMITS_COUNT" -eq "0" ]; then
50-
echo "No new commits found on ${GITHUB_REF_NAME} compared to main. Nothing to sync."
51-
exit 0
52-
fi
53-
54-
# 3. Extract package name for label
55-
BRANCH_NAME="${GITHUB_REF_NAME}"
56-
TEMP="${BRANCH_NAME#release-}"
57-
PACKAGE_NAME="${TEMP%-*}"
58-
59-
# 4. Check if PR already exists before creating
60-
EXISTING_PR=$(gh pr list --head "${GITHUB_REF_NAME}" --base "main" --json number --jq '.[0].number')
61-
if [ -n "$EXISTING_PR" ]; then
62-
echo "A pull request for ${GITHUB_REF_NAME} already exists (#${EXISTING_PR})."
63-
exit 0
64-
fi
65-
66-
# 5. Create the PR directly
67-
gh pr create \
68-
--base "main" \
69-
--head "${GITHUB_REF_NAME}" \
70-
--title "Sync ${GITHUB_REF_NAME} to main" \
71-
--body "This automated PR syncs the changes from the release branch ${GITHUB_REF_NAME} back to the main branch." \
72-
--label "override: batch-${PACKAGE_NAME}"
73-
29+
uses: ./.github/workflows/reusable_sync_to_main.yml
30+
with:
31+
branch-name: '${{ github.ref_name }}'
32+
secrets:
33+
BOT_TOKEN: ${{ secrets.FLUTTERGITHUBBOT_TOKEN }}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: Reusable Sync To Main
2+
on:
3+
workflow_call:
4+
inputs:
5+
branch-name:
6+
description: 'The release branch to sync back to main.'
7+
required: true
8+
type: string
9+
comment-if-exists:
10+
description: >-
11+
If non-empty, this is posted as a comment when the sync PR already
12+
exists. The comment is only posted once per PR.
13+
required: false
14+
default: ''
15+
type: string
16+
secrets:
17+
BOT_TOKEN:
18+
required: true
19+
jobs:
20+
sync_to_main:
21+
if: github.repository_owner == 'flutter'
22+
runs-on: ubuntu-latest
23+
permissions:
24+
# Need write permission to create PR.
25+
contents: write
26+
pull-requests: write
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0
30+
with:
31+
ref: ${{ inputs.branch-name }}
32+
fetch-depth: 0 # Fetch history to allow branch comparison
33+
persist-credentials: false
34+
35+
- name: Create Pull Request
36+
env:
37+
GH_TOKEN: ${{ secrets.BOT_TOKEN }}
38+
BRANCH_NAME: ${{ inputs.branch-name }}
39+
COMMENT: ${{ inputs.comment-if-exists }}
40+
# Used to avoid posting the same comment on every scheduled run.
41+
COMMENT_MARKER: '<!-- batch-release-sync-nudge -->'
42+
run: |
43+
gh auth setup-git
44+
# 1. Fetch main so the runner can see the difference
45+
git fetch origin main
46+
47+
# 2. Check if PR already exists before creating
48+
EXISTING_PR=$(gh pr list --head "${BRANCH_NAME}" --base "main" --json number --jq '.[0].number')
49+
if [ -n "$EXISTING_PR" ]; then
50+
echo "A pull request for ${BRANCH_NAME} already exists (#${EXISTING_PR})."
51+
if [ -n "$COMMENT" ]; then
52+
if gh pr view "$EXISTING_PR" --json comments --jq '.comments[].body' | grep -qF "$COMMENT_MARKER"; then
53+
echo "Already commented on #${EXISTING_PR}."
54+
else
55+
gh pr comment "$EXISTING_PR" --body "${COMMENT} ${COMMENT_MARKER}"
56+
fi
57+
fi
58+
exit 0
59+
fi
60+
61+
# 3. Verify there are actually new commits to sync.
62+
# This prevents the "GraphQL: No commits between main..." error, and is
63+
# the expected state when the release PR itself has not landed yet.
64+
COMMITS_COUNT=$(git rev-list --count origin/main..HEAD)
65+
66+
if [ "$COMMITS_COUNT" -eq "0" ]; then
67+
echo "No new commits found on ${BRANCH_NAME} compared to main. Nothing to sync."
68+
exit 0
69+
fi
70+
71+
# 4. Extract package name for label. Package names can't contain a
72+
# hyphen, so everything from the first one on is the version.
73+
TEMP="${BRANCH_NAME#release-}"
74+
PACKAGE_NAME="${TEMP%%-*}"
75+
76+
# 5. Create the PR directly
77+
gh pr create \
78+
--base "main" \
79+
--head "${BRANCH_NAME}" \
80+
--title "Sync ${BRANCH_NAME} to main" \
81+
--body "This automated PR syncs the changes from the release branch ${BRANCH_NAME} back to the main branch." \
82+
--label "override: batch-${PACKAGE_NAME}"

‎script/tool/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.14.4+2
2+
3+
* Adds an `in-flight-release-check` command, which reports whether an earlier
4+
batch release for a package hasn't been merged back yet.
5+
16
## 0.14.4+1
27

38
* Adds support for batch release of pre-1.0 packages.

‎script/tool/lib/src/branches_for_batch_release_command.dart‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class BranchesForBatchReleaseCommand extends PackageCommand {
7777
return;
7878
}
7979

80-
final pubspec = Pubspec.parse(package.pubspecFile.readAsStringSync());
80+
final Pubspec pubspec = package.parsePubspec();
8181
if (pubspec.version == null) {
8282
printError('The package has no version specified.');
8383
throw ToolExit(_kExitPackageMalformed);
@@ -96,7 +96,6 @@ class BranchesForBatchReleaseCommand extends PackageCommand {
9696

9797
await _createAndPushReleaseBranch(
9898
git: repository,
99-
package: package,
10099
releaseBranchName: releaseBranchName,
101100
remoteName: remoteName,
102101
);
@@ -171,7 +170,6 @@ class BranchesForBatchReleaseCommand extends PackageCommand {
171170
/// Throws a [ToolExit] if any of the steps fail.
172171
Future<void> _createAndPushReleaseBranch({
173172
required GitDir git,
174-
required RepositoryPackage package,
175173
required String releaseBranchName,
176174
required String remoteName,
177175
}) async {
@@ -189,11 +187,7 @@ class BranchesForBatchReleaseCommand extends PackageCommand {
189187

190188
await _pushBranch(git, remoteName, releaseBranchName);
191189

192-
final String? githubOutput = platform.environment['GITHUB_OUTPUT'];
193-
if (githubOutput != null && githubOutput.isNotEmpty) {
194-
final File file = package.directory.fileSystem.file(githubOutput);
195-
file.writeAsStringSync('release_branch=$releaseBranchName\n', mode: io.FileMode.append);
196-
}
190+
writeGitHubActionsOutput('release_branch', releaseBranchName);
197191
}
198192

199193
Future<void> _createHeadBranchAndCommit({

‎script/tool/lib/src/common/package_command.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,21 @@ abstract class PackageCommand extends Command<void> {
645645
return gitVersionFinder;
646646
}
647647

648+
/// Appends `name=value` to the file that GitHub Actions reads step outputs
649+
/// from, making it available to later steps as `steps.<id>.outputs.<name>`.
650+
///
651+
/// Does nothing when not running in GitHub Actions, where `GITHUB_OUTPUT` is
652+
/// unset.
653+
void writeGitHubActionsOutput(String name, String value) {
654+
final String? githubOutput = platform.environment['GITHUB_OUTPUT'];
655+
if (githubOutput == null || githubOutput.isEmpty) {
656+
return;
657+
}
658+
packagesDir.fileSystem
659+
.file(githubOutput)
660+
.writeAsStringSync('$name=$value\n', mode: io.FileMode.append);
661+
}
662+
648663
// Returns the names of packages that have been changed given a list of
649664
// changed files.
650665
//

0 commit comments

Comments
 (0)