# Copyright 2023 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.

name: Cherry-pick Labeled PR to Release Branch

on:
  pull_request_target:
    branches: master
    types: [labeled]

permissions:
  contents: read
  pull-requests: write

jobs:
  cherrypick_not_merged_remove_label:
    name: cherrypick_not_merged_remove_label
    runs-on: ubuntu-latest
    if: |
      (github.event.label.name == format('cp{0} beta', ':') || github.event.label.name == format('cp{0} stable', ':')) &&
      (github.event.pull_request.merged == false)
    steps:
      - name: Leave Comment and Remove Label
        run: |
          gh pr edit ${{ github.event.pull_request.number }} --remove-label "cp: beta"
          gh pr edit ${{ github.event.pull_request.number }} --remove-label "cp: stable"
          gh pr comment ${{ github.event.pull_request.number }} -R flutter/flutter -b "Only merged pull requests can be cherrypicked."
        env:
          GITHUB_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
  cherrypick_to_release:
    name: cherrypick_to_release
    runs-on: ubuntu-latest
    if: |
      (github.event.label.name == format('cp{0} beta', ':') || github.event.label.name == format('cp{0} stable', ':')) &&
      (github.event.pull_request.merged == true)
    steps:
      - name: Get Release Channel
        run: |
          echo "CHANNEL=$(echo ${{ github.event.label.name }} | cut -d ':' -f 2 | xargs)" >> $GITHUB_ENV
      - name: Get Release Candidate Branch
        run: |
          RELEASE_BRANCH=$(curl https://raw.githubusercontent.com/flutter/flutter/$CHANNEL/bin/internal/release-candidate-branch.version)
          echo "RELEASE_BRANCH=$(echo $RELEASE_BRANCH | tr -d '\n')" >> $GITHUB_ENV
      - name: Get Cherry Pick PR
        run: |
          echo "COMMIT_SHA=$(echo ${{ github.event.pull_request.merge_commit_sha }})" >> $GITHUB_ENV
      - name: Checkout Flutter Repo
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3
        with:
          repository: flutteractionsbot/flutter
          path: flutter
          ref: master
          persist-credentials: false
          # Checkout all history commits on master branch, so that the cp commit is a known object
          fetch-depth: 0
      # use same name when checking out branch, since the marketplace action does a hard reset.
      - name: Attempt CP
        id: attempt-cp
        working-directory: ./flutter
        run: |
          git config user.name "flutteractionsbot"
          git config user.email "<flutter-actions-bot@google.com>"
          git remote add upstream https://github.com/flutter/flutter.git
          git fetch upstream $RELEASE_BRANCH
          git fetch upstream master
          git checkout -b cp-${CHANNEL}-${COMMIT_SHA} --track upstream/$RELEASE_BRANCH
          git cherry-pick $COMMIT_SHA
      - name: Create PR on CP success
        if: ${{ steps.attempt-cp.conclusion == 'success' }}
        working-directory: ./flutter
        id: create-pr
        run: |
          git push https://${{ env.GITHUB_TOKEN }}@github.com/flutteractionsbot/flutter cp-${CHANNEL}-${COMMIT_SHA}
          {
            echo 'PR_URL<<EOF'
            gh pr create --title "[CP-${CHANNEL}]${PR_TITLE}" --body-file ".github/PR_TEMPLATE/PULL_REQUEST_CP_TEMPLATE.md" --base ${RELEASE_BRANCH} --label "cp: review" --repo flutter/flutter --head flutteractionsbot:cp-${CHANNEL}-${COMMIT_SHA}
            echo EOF
          } >> "$GITHUB_ENV"
        env:
          GITHUB_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
          PR_TITLE: ${{ github.event.pull_request.title }}
      - name: Leave Comment on CP success
        if: ${{ steps.create-pr.conclusion == 'success' }}
        run: |
          echo $PR_URL
          NEW_PR_NUMBER="${PR_URL##*/}"
          SUCCESS_MSG=" @${{ github.actor }} please fill out the PR description above, afterwards the release team will review this request."
          gh pr comment $NEW_PR_NUMBER -R flutter/flutter -b "${SUCCESS_MSG}"
        env:
          GITHUB_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
      - name: Leave Comment on CP failure
        if: ${{ failure() && steps.attempt-cp.conclusion == 'failure' }}
        run: |
          FAILURE_MSG="Failed to create CP due to merge conflicts.<br>"
          FAILURE_MSG+="You will need to create the PR manually. See [the cherrypick wiki](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md) for more info."
          gh pr comment ${{ github.event.pull_request.number }} -R flutter/flutter -b "${FAILURE_MSG}"
        env:
          GITHUB_TOKEN: ${{ secrets.FLUTTERACTIONSBOT_CP_TOKEN }}
