name: Create Release Pull Request or Publish to npm

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

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

jobs:
  publish-branch-package:
    name: Publish Branch Package (Pull Requests)
    if: github.event_name == 'pull_request'
    runs-on: ubuntu-24.04
    timeout-minutes: 5
    permissions:
      contents: read
      pull-requests: write
      id-token: write # Required for trusted publishing
    steps:
      - name: Check out Git repository
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          ref: ${{ github.event.inputs.commit_hash || github.event.pull_request.head.sha || github.sha }}

      - name: Install Node and npm packages
        uses: ./.github/actions/setup-node-env

      - name: Publish branch package to npm
        uses: wistia/publish-branch-to-npm@525dea1ce88b510393e94769852b9b2f4a0bcf94 # v2.3.5
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          npm_token: ${{ secrets.NPM_TOKEN }}
          commit_hash: ${{ github.event.inputs.commit_hash || github.event.pull_request.head.sha || github.sha }}

  release:
    name: Release (Main Branch)
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    runs-on: ubuntu-22.04
    timeout-minutes: 5
    permissions:
      contents: write
      id-token: write
      issues: read
      packages: write
      pull-requests: write
    steps:
      - name: Check out Git repository
        uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
        with:
          # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
          fetch-depth: 0

      - name: Install Node and npm packages
        uses: ./.github/actions/setup-node-env

      # this is a hack but at this time it's the only way to get the changeset version for the
      # commit message in the next step. using `changeset status` it generates a `release.json` file,
      # then reads the new version from it and then removes the file
      - name: Get new package version
        run: |
          echo "NEW_PACKAGE_VERSION=$(yarn changeset status --output=release.json && jq -r '.releases[0].newVersion' release.json && rm release.json)" >> "$GITHUB_ENV"

      - name: Create Release Pull Request or Publish to npm
        uses: changesets/action@e0145edc7d9d8679003495b11f87bd8ef63c0cba # v1.5.3
        id: changesets
        with:
          createGithubReleases: true
          publish: yarn release
          title: "chore: release v${{env.NEW_PACKAGE_VERSION}}"
          commit: "chore: release v${{env.NEW_PACKAGE_VERSION}}"
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
