name: 🦋 Changesets PR

on:
  push:
    branches:
      - main
    paths:
      - "packages/**"
      - ".changeset/**"
      - ".server-changes/**"
      - "package.json"
      - "pnpm-lock.yaml"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  release-pr:
    name: Create Release PR
    runs-on: warp-ubuntu-latest-x64-2x
    permissions:
      contents: write
      pull-requests: write
      checks: write
    if: github.repository == 'triggerdotdev/trigger.dev'
    steps:
      - name: Checkout
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 # zizmor: ignore[artipacked] changesets/action pushes the release branch; no artifact upload here so no leak path
        with:
          fetch-depth: 0

      - name: Setup pnpm
        uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5.0.0

      - name: Setup node
        uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
        with:
          node-version: 22.23.1
          cache: "pnpm"

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Create release PR
        id: changesets
        uses: changesets/action@a45c4d594aa4e2c509dc14a9f2b3b67ba3780d0d # v1.9.0
        with:
          version: pnpm run changeset:version
          commit: "chore: release"
          title: "chore: release"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Update PR title and enhance body
        if: steps.changesets.outputs.published != 'true'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PR_NUMBER=$(gh pr list --head changeset-release/main --json number --jq '.[0].number')
          if [ -n "$PR_NUMBER" ]; then
            git fetch origin changeset-release/main
            # we arbitrarily reference the version of the cli package here; it is the same for all package releases
            VERSION=$(git show origin/changeset-release/main:packages/cli-v3/package.json | jq -r '.version')
            gh pr edit "$PR_NUMBER" --title "chore: release v$VERSION"

            # Enhance the PR body with a clean, deduplicated summary
            RAW_BODY=$(gh pr view "$PR_NUMBER" --json body --jq '.body')
            ENHANCED_BODY=$(CHANGESET_PR_BODY="$RAW_BODY" node scripts/enhance-release-pr.mjs "$VERSION")
            if [ -n "$ENHANCED_BODY" ]; then
              gh api repos/triggerdotdev/trigger.dev/pulls/"$PR_NUMBER" \
                -X PATCH \
                -f body="$ENHANCED_BODY"
            fi
          fi

      # The changesets bot authors release PRs with GITHUB_TOKEN, which by GitHub
      # design cannot trigger downstream workflows. That leaves the required
      # "All PR Checks" status permanently Expected and the PR unmergeable.
      # The release PR only bumps package.json + lockfile + CHANGELOGs from
      # changesets already on main, so we self-report the required check as
      # success. If a human ever pushes to changeset-release/main, the real
      # pr_checks.yml fires and its result overwrites this one (last write wins
      # for the same context on the same SHA).
      - name: Self-report "All PR Checks" success on release PR
        if: steps.changesets.outputs.published != 'true'
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PR_NUMBER=$(gh pr list --head changeset-release/main --json number --jq '.[0].number')
          if [ -z "$PR_NUMBER" ]; then exit 0; fi
          HEAD_SHA=$(gh pr view "$PR_NUMBER" --json headRefOid --jq '.headRefOid')
          gh api -X POST repos/${{ github.repository }}/check-runs \
            -f name="All PR Checks" \
            -f head_sha="$HEAD_SHA" \
            -f status=completed \
            -f conclusion=success \
            -f 'output[title]=Auto-pass for changeset release PR' \
            -f 'output[summary]=Required check auto-satisfied for changeset-release/main PRs. Full CI ran on the underlying commits before they landed on main.'
