| name: Reusable Sync To Main |
| on: |
| workflow_call: |
| inputs: |
| branch-name: |
| description: 'The release branch to sync back to main.' |
| required: true |
| type: string |
| comment-if-exists: |
| description: >- |
| If non-empty, this is posted as a comment when the sync PR already |
| exists. The comment is only posted once per PR. |
| required: false |
| default: '' |
| type: string |
| secrets: |
| BOT_TOKEN: |
| required: true |
| jobs: |
| sync_to_main: |
| if: github.repository_owner == 'flutter' |
| runs-on: ubuntu-latest |
| permissions: |
| # Need write permission to create PR. |
| contents: write |
| pull-requests: write |
| steps: |
| - name: Checkout repository |
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 |
| with: |
| ref: ${{ inputs.branch-name }} |
| fetch-depth: 0 # Fetch history to allow branch comparison |
| persist-credentials: false |
| |
| - name: Create Pull Request |
| env: |
| GH_TOKEN: ${{ secrets.BOT_TOKEN }} |
| BRANCH_NAME: ${{ inputs.branch-name }} |
| COMMENT: ${{ inputs.comment-if-exists }} |
| # Used to avoid posting the same comment on every scheduled run. |
| COMMENT_MARKER: '<!-- batch-release-sync-nudge -->' |
| run: | |
| gh auth setup-git |
| # 1. Fetch main so the runner can see the difference |
| git fetch origin main |
| |
| # 2. Check if PR already exists before creating |
| EXISTING_PR=$(gh pr list --head "${BRANCH_NAME}" --base "main" --json number --jq '.[0].number') |
| if [ -n "$EXISTING_PR" ]; then |
| echo "A pull request for ${BRANCH_NAME} already exists (#${EXISTING_PR})." |
| if [ -n "$COMMENT" ]; then |
| if gh pr view "$EXISTING_PR" --json comments --jq '.comments[].body' | grep -qF "$COMMENT_MARKER"; then |
| echo "Already commented on #${EXISTING_PR}." |
| else |
| gh pr comment "$EXISTING_PR" --body "${COMMENT} ${COMMENT_MARKER}" |
| fi |
| fi |
| exit 0 |
| fi |
| |
| # 3. Verify there are actually new commits to sync. |
| # This prevents the "GraphQL: No commits between main..." error, and is |
| # the expected state when the release PR itself has not landed yet. |
| COMMITS_COUNT=$(git rev-list --count origin/main..HEAD) |
| |
| if [ "$COMMITS_COUNT" -eq "0" ]; then |
| echo "No new commits found on ${BRANCH_NAME} compared to main. Nothing to sync." |
| exit 0 |
| fi |
| |
| # 4. Extract package name for label. Package names can't contain a |
| # hyphen, so everything from the first one on is the version. |
| TEMP="${BRANCH_NAME#release-}" |
| PACKAGE_NAME="${TEMP%%-*}" |
| |
| # 5. Create the PR directly |
| gh pr create \ |
| --base "main" \ |
| --head "${BRANCH_NAME}" \ |
| --title "Sync ${BRANCH_NAME} to main" \ |
| --body "This automated PR syncs the changes from the release branch ${BRANCH_NAME} back to the main branch." \ |
| --label "override: batch-${PACKAGE_NAME}" |