name: Branch Created

on:
  workflow_dispatch:
    inputs:
      branch-name:
        description: Branch name (e.g. `29-x-y`)
        required: true
        type: string
  create:

permissions: {}

jobs:
  release-branch-created:
    name: Release Branch Created
    if: ${{ github.event_name == 'workflow_dispatch' || (github.event.ref_type == 'branch' && endsWith(github.event.ref, '-x-y') && !startsWith(github.event.ref, 'roller')) }}
    permissions:
      contents: read
      pull-requests: write
      repository-projects: write  # Required for labels
    runs-on: ubuntu-latest
    steps:
      - name: Determine Major Version
        id: check-major-version
        run: |
          if [[ ${{ github.event.inputs.branch-name || github.event.ref }} =~ ^([0-9]+)-x-y$ ]]; then
            echo "MAJOR=${BASH_REMATCH[1]}" >> "$GITHUB_OUTPUT"
          else
            echo "Not a release branch: ${{ github.event.inputs.branch-name || github.event.ref }}"
          fi
      - name: New Release Branch Tasks
        if: ${{ steps.check-major-version.outputs.MAJOR }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_REPO: electron/electron
          MAJOR: ${{ steps.check-major-version.outputs.MAJOR }}
          NUM_SUPPORTED_VERSIONS: 3
        run: |
          PREVIOUS_MAJOR=$((MAJOR - 1))
          UNSUPPORTED_MAJOR=$((MAJOR - NUM_SUPPORTED_VERSIONS - 1))

          # Create new labels
          gh label create $MAJOR-x-y --color 8d9ee8 || true
          gh label create target/$MAJOR-x-y --color ad244f --description "PR should also be added to the \"${MAJOR}-x-y\" branch." || true
          gh label create merged/$MAJOR-x-y --color 61a3c6 --description "PR was merged to the \"${MAJOR}-x-y\" branch." || true
          gh label create in-flight/$MAJOR-x-y --color db69a6 || true
          gh label create needs-manual-bp/$MAJOR-x-y --color 8b5dba || true

          # Change color of old labels
          gh label edit $UNSUPPORTED_MAJOR-x-y --color ededed || true
          gh label edit target/$UNSUPPORTED_MAJOR-x-y --color ededed || true
          gh label edit merged/$UNSUPPORTED_MAJOR-x-y --color ededed || true
          gh label edit in-flight/$UNSUPPORTED_MAJOR-x-y --color ededed || true
          gh label edit needs-manual-bp/$UNSUPPORTED_MAJOR-x-y --color ededed || true

          # Add the new target label to any PRs which:
          #   * target the previous major
          #   * are in-flight for the previous major
          #   * need manual backport for the previous major
          for PREVIOUS_MAJOR_LABEL in target/$PREVIOUS_MAJOR-x-y in-flight/$PREVIOUS_MAJOR-x-y needs-manual-bp/$PREVIOUS_MAJOR-x-y; do
            PULL_REQUESTS=$(gh pr list --label $PREVIOUS_MAJOR_LABEL --jq .[].number --json number --limit 500)
            if [[ $PULL_REQUESTS ]]; then
              echo $PULL_REQUESTS | xargs -n 1 gh pr edit --add-label target/$MAJOR-x-y || true
            fi
          done
      - name: Generate GitHub App token
        if: ${{ steps.check-major-version.outputs.MAJOR }}
        uses: electron/github-app-auth-action@384fd19694fe7b6dcc9a684746c6976ad78228ae # v1.1.1
        id: generate-token
        with:
          creds: ${{ secrets.RELEASE_BOARD_GH_APP_CREDS }}
          org: electron
      - name: Generate Release Project Board Metadata
        if: ${{ steps.check-major-version.outputs.MAJOR }}
        uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
        id: generate-project-metadata
        with:
          script: |
            const major = ${{ steps.check-major-version.outputs.MAJOR }}
            const nextMajor = major + 1
            const prevMajor = major - 1

            core.setOutput("major", major)
            core.setOutput("next-major", nextMajor)
            core.setOutput("prev-major", prevMajor)
            core.setOutput("prev-prev-major", prevMajor - 1)
            core.setOutput("template-view", JSON.stringify({
                major,
                "next-major": nextMajor,
                "prev-major": prevMajor,
            }))
      - name: Create Release Project Board
        if: ${{ steps.check-major-version.outputs.MAJOR }}
        uses: dsanders11/project-actions/copy-project@eb760c48894b5702398529cbb8f6e98378e315d0 # v1.3.0
        id: create-release-board
        with:
          drafts: true
          project-number: 64
          # TODO - Set to public once GitHub fixes their GraphQL bug
          # public: true
          # TODO - Enable once GitHub doesn't require overly broad, read
          #        and write permission for repo "Contents" to link
          # link-to-repository: electron/electron
          template-view: ${{ steps.generate-project-metadata.outputs.template-view }}
          title: ${{ steps.generate-project-metadata.outputs.major }}-x-y
          token: ${{ steps.generate-token.outputs.token }}
      - name: Dump Release Project Board Contents
        if: ${{ steps.check-major-version.outputs.MAJOR }}
        run: gh project item-list ${{ steps.create-release-board.outputs.number }} --owner electron --format json | jq
        env:
          GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
      - name: Find Previous Release Project Board
        if: ${{ steps.check-major-version.outputs.MAJOR }}
        uses: dsanders11/project-actions/find-project@eb760c48894b5702398529cbb8f6e98378e315d0 # v1.3.0
        id: find-prev-release-board
        with:
          title: ${{ steps.generate-project-metadata.outputs.prev-prev-major }}-x-y
          token: ${{ steps.generate-token.outputs.token }}
      - name: Close Previous Release Project Board
        if: ${{ steps.check-major-version.outputs.MAJOR }}
        uses: dsanders11/project-actions/close-project@eb760c48894b5702398529cbb8f6e98378e315d0 # v1.3.0
        with:
          project-number: ${{ steps.find-prev-release-board.outputs.number }}
          token: ${{ steps.generate-token.outputs.token }}
