name: Release

on:
  push:

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

permissions:
  id-token: write # Required for OIDC
  contents: read
  checks: write
  statuses: write

jobs:
  release-main:
    if: ${{ github.ref_name == 'main' }}
    name: Main
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0
          persist-credentials: false

      - uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1
        id: app-token
        with:
          app-id: ${{ vars.PRIMER_APP_ID_SHARED }}
          private-key: ${{ secrets.PRIMER_APP_PRIVATE_KEY_SHARED }}

      - name: Set up Node.js
        uses: actions/setup-node@v7
        with:
          node-version: 24

      - name: Install dependencies
        run: npm ci

      - name: Create release pull request or publish to npm
        id: changesets
        uses: changesets/action@v2
        with:
          github-token: ${{ steps.app-token.outputs.token }}
          pr-title: Release Tracking
          # This expects you to have a script called release which does a build for your packages and calls changeset publish
          publish-script: npm run release
        env:
          GITHUB_TOKEN: ${{ steps.app-token.outputs.token }}

  release-canary:
    name: Canary
    if: ${{ github.repository == 'primer/eslint-plugin-primer-react' && github.ref_name != 'main' && github.ref_name != 'changeset-release/main'  }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0

      - name: Set up Node.js
        uses: actions/setup-node@v7
        with:
          node-version: 24

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build --if-present

      - name: Publish canary version
        run: |
          echo "$( jq '.version = "0.0.0"' package.json )" > package.json
          echo -e "---\n'eslint-plugin-primer-react': patch\n---\n\nFake entry to force publishing" > .changeset/force-snapshot-release.md
          npx changeset version --snapshot
          npx changeset publish --tag canary
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Output canary version number
        uses: actions/github-script@v9.0.0
        with:
          script: |
            const package = require(`${process.env.GITHUB_WORKSPACE}/package.json`)
            github.rest.repos.createCommitStatus({
              owner: context.repo.owner,
              repo: context.repo.repo,
              sha: context.sha,
              state: 'success',
              context: `Published ${package.name}`,
              description: package.version,
              target_url: `https://unpkg.com/${package.name}@${package.version}/`
            })

      - name: Upload versions json file
        uses: primer/.github/.github/actions/upload-versions@main

  release-candidate:
    name: Candidate
    if: ${{ github.repository == 'primer/eslint-plugin-primer-react' && github.ref_name == 'changeset-release/main' }}

    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v7
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0

      - name: Set up Node.js
        uses: actions/setup-node@v7
        with:
          node-version: 24

      - name: Install dependencies
        run: npm ci

      - name: Build
        run: npm run build --if-present

      - name: Publish release candidate
        run: |
          version=$(jq -r .version package.json)
          echo "$( jq ".version = \"$(echo $version)-rc.$(git rev-parse --short HEAD)\"" package.json )" > package.json
          npm publish --tag next
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Output release candidate version number
        uses: actions/github-script@v9.0.0
        with:
          script: |
            const package = require(`${process.env.GITHUB_WORKSPACE}/package.json`)
            github.rest.repos.createCommitStatus({
              owner: context.repo.owner,
              repo: context.repo.repo,
              sha: context.sha,
              state: 'success',
              context: `Published ${package.name}`,
              description: package.version,
              target_url: `https://unpkg.com/${package.name}@${package.version}/`
            })

      - name: Upload versions json file
        uses: primer/.github/.github/actions/upload-versions@main
