name: Release

on:
  push:
    tags: ['v*']
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version, without v; dispatch from the matching existing vX.Y.Z tag ref'
        required: true
        type: string

concurrency:
  group: release-${{ github.ref_name }}
  cancel-in-progress: false

permissions:
  contents: read

env:
  CARGO_TERM_COLOR: always
  CARGO_INCREMENTAL: 0
  RUSTFLAGS: -Dwarnings

jobs:
  resolve:
    timeout-minutes: 10
    runs-on: ubuntu-latest
    permissions:
      contents: read
      # Read release-candidate.yml runs for the RC receipt check below.
      actions: read
    outputs:
      tag: ${{ steps.release.outputs.tag }}
      sha: ${{ steps.release.outputs.sha }}
      version: ${{ steps.release.outputs.version }}
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          fetch-depth: 0
      - name: Resolve release source
        id: release
        shell: bash
        env:
          INPUT_VERSION: ${{ inputs.version }}
        run: |
          set -euo pipefail

          if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
            if ! [[ "${INPUT_VERSION}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
              echo "::error::Release version '${INPUT_VERSION}' must use X.Y.Z." >&2
              exit 1
            fi
            tag="v${INPUT_VERSION}"
            if [[ "${GITHUB_REF}" != "refs/tags/${tag}" ]]; then
              echo "::error::Dispatch release.yml from --ref ${tag}, not ${GITHUB_REF}." >&2
              exit 1
            fi
          else
            tag="${GITHUB_REF_NAME}"
          fi

          if ! [[ "${tag}" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
            echo "::error::Release tag '${tag}' must use vX.Y.Z." >&2
            exit 1
          fi
          if ! git rev-parse --verify "refs/tags/${tag}^{commit}" >/dev/null 2>&1; then
            echo "::error::Release tag ${tag} does not exist. Create it from the frozen main commit before dispatching." >&2
            exit 1
          fi

          sha="$(git rev-parse "refs/tags/${tag}^{commit}")"
          event_sha="$(git rev-parse "${GITHUB_SHA}^{commit}")"
          if [[ "${event_sha}" != "${sha}" ]]; then
            echo "::error::Trigger SHA ${event_sha} does not match ${tag} at ${sha}; the tag moved after this run was created." >&2
            exit 1
          fi

          {
            echo "tag=${tag}"
            echo "sha=${sha}"
            echo "version=${tag#v}"
          } >> "${GITHUB_OUTPUT}"
      - name: Validate tagged release metadata
        shell: bash
        env:
          SHA: ${{ steps.release.outputs.sha }}
          TAG: ${{ steps.release.outputs.tag }}
        run: |
          set -euo pipefail
          git checkout --detach "${SHA}"
          expected="${TAG#v}"
          workspace_version="$(grep -E '^version = "' Cargo.toml | head -n1 | sed -E 's/^version = "([^"]+)".*/\1/')"
          npm_version="$(node -p "require('./npm/codewhale/package.json').version")"
          binary_version="$(node -p "require('./npm/codewhale/package.json').codewhaleBinaryVersion")"
          sdk_version="$(node -p "require('./npm/runtime-sdk/package.json').version")"
          vscode_version="$(node -p "require('./extensions/vscode/package.json').version")"
          for pair in \
            "workspace:${workspace_version}" \
            "npm:${npm_version}" \
            "npm binary:${binary_version}" \
            "runtime-sdk:${sdk_version}" \
            "vscode:${vscode_version}"; do
            label="${pair%%:*}"
            actual="${pair#*:}"
            if [[ "${actual}" != "${expected}" ]]; then
              echo "::error::${label} version ${actual} does not match tag ${TAG}." >&2
              exit 1
            fi
          done
          ./scripts/release/check-versions.sh --require-dated-release
      - name: Require release source on main
        run: ./scripts/release/ensure-release-on-main.sh "${{ steps.release.outputs.sha }}"
      - name: Require a green release-candidate receipt for this exact SHA
        # Fail fast, before the long parity and artifact builds: the tag must
        # point at a commit a release-candidate run already validated,
        # Parity included. A missing receipt means run the RC on this SHA,
        # never move the tag (v0.10.0 was re-pointed after the RC skipped
        # parity).
        env:
          GH_TOKEN: ${{ github.token }}
          SHA: ${{ steps.release.outputs.sha }}
        run: ./scripts/release/require-rc-receipt.sh "${GITHUB_REPOSITORY}" "${SHA}"
      - name: Refuse an existing public asset set
        env:
          GH_TOKEN: ${{ github.token }}
          TAG: ${{ steps.release.outputs.tag }}
        run: node scripts/release/ensure-release-assets-absent.js "${GITHUB_REPOSITORY}" "${TAG}"

  parity:
    name: Parity
    needs: resolve
    uses: ./.github/workflows/release-parity.yml

  artifacts:
    needs: [parity, resolve]
    if: ${{ !cancelled() && needs.resolve.result == 'success' && needs.parity.result == 'success' }}
    uses: ./.github/workflows/release-artifacts.yml
    with:
      source_sha: ${{ needs.resolve.outputs.sha }}
      version: ${{ needs.resolve.outputs.version }}
      retention_days: 14

  docker-build:
    needs: [artifacts, resolve]
    if: ${{ !cancelled() && needs.artifacts.result == 'success' }}
    name: Docker ${{ matrix.platform }}
    timeout-minutes: 60
    strategy:
      fail-fast: false
      matrix:
        include:
          - runner: ubuntu-latest
            platform: linux/amd64
            architecture: amd64
            cli_artifact: codewhale-linux-x64
            shim_artifact: codew-linux-x64
          - runner: ubuntu-24.04-arm
            platform: linux/arm64
            architecture: arm64
            cli_artifact: codewhale-linux-arm64
            shim_artifact: codew-linux-arm64
    runs-on: ${{ matrix.runner }}
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout release infrastructure
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          ref: ${{ needs.resolve.outputs.sha }}
          path: infra
      - name: Download Codewhale release binary
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
        with:
          name: ${{ matrix.cli_artifact }}
          path: docker-context/bin
      - name: Download codew release alias
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
        with:
          name: ${{ matrix.shim_artifact }}
          path: docker-context/bin
      - name: Verify native release bytes
        shell: bash
        env:
          CLI_ARTIFACT: ${{ matrix.cli_artifact }}
          SHIM_ARTIFACT: ${{ matrix.shim_artifact }}
        run: |
          set -euo pipefail
          mv -- "docker-context/bin/${CLI_ARTIFACT}" docker-context/bin/codewhale
          mv -- "docker-context/bin/${SHIM_ARTIFACT}" docker-context/bin/codew
          chmod 0755 docker-context/bin/codewhale docker-context/bin/codew
          cmp docker-context/bin/codewhale docker-context/bin/codew
          docker-context/bin/codewhale --version
          docker-context/bin/codew --version
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@594f3bf4285d9ea8dc53c9a0c9c4092420091003 # v4
      - name: Log in to GitHub Container Registry
        uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Normalize image name
        id: image
        shell: bash
        run: echo "name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
      - name: Extract image labels
        id: meta
        uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6
        with:
          images: |
            ${{ steps.image.outputs.name }}
          tags: |
            type=raw,value=${{ needs.resolve.outputs.version }}
      - name: Revalidate release tag before container upload
        env:
          EXPECTED_SHA: ${{ needs.resolve.outputs.sha }}
          TAG: ${{ needs.resolve.outputs.tag }}
        run: |
          ./infra/scripts/release/verify-remote-tag.sh \
            "https://github.com/${GITHUB_REPOSITORY}.git" \
            "${TAG}" \
            "${EXPECTED_SHA}"
      - name: Assemble and push native image by digest
        id: build
        uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7
        env:
          DOCKER_BUILD_RECORD_UPLOAD: false
          DOCKER_BUILD_SUMMARY: false
        with:
          context: docker-context
          file: infra/packaging/docker/Dockerfile.release
          platforms: ${{ matrix.platform }}
          provenance: mode=max
          sbom: true
          labels: ${{ steps.meta.outputs.labels }}
          outputs: type=image,name=${{ steps.image.outputs.name }},push-by-digest=true,name-canonical=true,push=true
      - name: Smoke native image digest
        shell: bash
        env:
          IMAGE: ${{ steps.image.outputs.name }}@${{ steps.build.outputs.digest }}
        run: |
          set -euo pipefail
          docker pull "${IMAGE}"
          docker run --rm --entrypoint codewhale "${IMAGE}" --version
          docker run --rm --entrypoint codew "${IMAGE}" --version
      - name: Export image digest
        shell: bash
        env:
          DIGEST: ${{ steps.build.outputs.digest }}
        run: |
          set -euo pipefail
          if ! [[ "${DIGEST}" =~ ^sha256:[0-9a-f]{64}$ ]]; then
            echo "Unexpected image digest: ${DIGEST}" >&2
            exit 1
          fi
          mkdir -p digests
          touch "digests/${DIGEST#sha256:}"
      - name: Upload image digest
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
        with:
          name: docker-digest-${{ matrix.architecture }}
          path: digests/*
          if-no-files-found: error
          retention-days: 1
          overwrite: true

  docker:
    timeout-minutes: 30
    needs: [docker-build, resolve]
    if: ${{ !cancelled() && needs.docker-build.result == 'success' }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    steps:
      - name: Checkout release infrastructure
        uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          ref: ${{ needs.resolve.outputs.sha }}
          path: infra
      - name: Download native image digests
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
        with:
          path: digests
          pattern: docker-digest-*
          merge-multiple: true
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@594f3bf4285d9ea8dc53c9a0c9c4092420091003 # v4
      - name: Log in to GitHub Container Registry
        uses: docker/login-action@dbcb813823bdd20940b903addbd779551569679f # v4
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.GITHUB_TOKEN }}
      - name: Normalize image name
        id: image
        shell: bash
        run: echo "name=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_OUTPUT"
      - name: Extract metadata
        id: meta
        uses: docker/metadata-action@dc802804100637a589fabce1cb79ff13a1411302 # v6
        with:
          images: |
            ${{ steps.image.outputs.name }}
          tags: |
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern=v{{major}}
            type=ref,event=tag
            type=semver,pattern={{version}},value=${{ needs.resolve.outputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }}
            type=semver,pattern={{major}}.{{minor}},value=${{ needs.resolve.outputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }}
            type=semver,pattern=v{{major}},value=${{ needs.resolve.outputs.tag }},enable=${{ github.event_name == 'workflow_dispatch' }}
            type=raw,value=${{ inputs.version }},enable=${{ github.event_name == 'workflow_dispatch' }}
            type=raw,value=v${{ inputs.version }},enable=${{ github.event_name == 'workflow_dispatch' }}
            type=raw,value=latest
      - name: Revalidate release tag before container publish
        env:
          EXPECTED_SHA: ${{ needs.resolve.outputs.sha }}
          TAG: ${{ needs.resolve.outputs.tag }}
        run: |
          ./infra/scripts/release/verify-remote-tag.sh \
            "https://github.com/${GITHUB_REPOSITORY}.git" \
            "${TAG}" \
            "${EXPECTED_SHA}"
      - name: Publish multi-architecture manifest
        shell: bash
        env:
          IMAGE: ${{ steps.image.outputs.name }}
          TAGS: ${{ steps.meta.outputs.tags }}
        run: |
          set -euo pipefail
          mapfile -t digest_files < <(find digests -maxdepth 1 -type f -printf '%f\n' | sort)
          if [[ "${#digest_files[@]}" -ne 2 ]]; then
            echo "Expected exactly two native image digests; found ${#digest_files[@]}." >&2
            exit 1
          fi

          sources=()
          for digest in "${digest_files[@]}"; do
            if ! [[ "${digest}" =~ ^[0-9a-f]{64}$ ]]; then
              echo "Unexpected image digest file: ${digest}" >&2
              exit 1
            fi
            sources+=("${IMAGE}@sha256:${digest}")
          done

          tag_args=()
          while IFS= read -r tag; do
            [[ -n "${tag}" ]] && tag_args+=(--tag "${tag}")
          done <<< "${TAGS}"
          if [[ "${#tag_args[@]}" -eq 0 ]]; then
            echo "No container tags were generated." >&2
            exit 1
          fi

          docker buildx imagetools create "${tag_args[@]}" "${sources[@]}"
      - name: Verify and smoke published container
        shell: bash
        env:
          IMAGE: ${{ steps.image.outputs.name }}:${{ needs.resolve.outputs.tag }}
        run: |
          set -euo pipefail
          docker buildx imagetools inspect "${IMAGE}"
          raw_manifest="$(docker buildx imagetools inspect --raw "${IMAGE}")"
          jq -e \
            '[.manifests[] | select(.platform.os == "linux") | "linux/\(.platform.architecture)"] | unique | sort == ["linux/amd64", "linux/arm64"]' \
            <<< "${raw_manifest}"
          docker pull "${IMAGE}"
          docker run --rm --entrypoint codewhale "${IMAGE}" --version
          docker run --rm --entrypoint codew "${IMAGE}" --version

  release:
    timeout-minutes: 30
    needs: [artifacts, docker, resolve]
    if: ${{ !cancelled() && needs.artifacts.result == 'success' && needs.docker.result == 'success' }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          ref: ${{ needs.resolve.outputs.sha }}
          path: repo
      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 22
          package-manager-cache: false
      - uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
        with:
          name: codewhale-release-assets
          path: artifacts
      - name: Revalidate exact authoritative asset set
        run: node repo/scripts/release/assemble-release-assets.js --verify artifacts
      - name: Generate release body from CHANGELOG
        shell: bash
        run: |
          ./repo/scripts/release/generate-release-body.sh \
            "${{ needs.resolve.outputs.tag }}" repo/CHANGELOG.md > release-body.md
      - name: Revalidate release tag before GitHub Release write
        env:
          EXPECTED_SHA: ${{ needs.resolve.outputs.sha }}
          TAG: ${{ needs.resolve.outputs.tag }}
        run: |
          ./repo/scripts/release/verify-remote-tag.sh \
            "https://github.com/${GITHUB_REPOSITORY}.git" \
            "${TAG}" \
            "${EXPECTED_SHA}"
      - name: Reconfirm public asset set is still empty
        env:
          GH_TOKEN: ${{ github.token }}
          TAG: ${{ needs.resolve.outputs.tag }}
        run: node repo/scripts/release/ensure-release-assets-absent.js "${GITHUB_REPOSITORY}" "${TAG}"
      - uses: softprops/action-gh-release@efb35369e0ad2afab669f228072c1b0d510eae64 # v3
        with:
          tag_name: ${{ needs.resolve.outputs.tag }}
          files: artifacts/*
          prerelease: false
          body_path: release-body.md
          overwrite_files: false
          fail_on_unmatched_files: true

  npm:
    timeout-minutes: 20
    needs: [release, resolve]
    if: ${{ !cancelled() && needs.release.result == 'success' }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
      id-token: write
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          ref: ${{ needs.resolve.outputs.sha }}
          fetch-depth: 0
      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 24
          registry-url: https://registry.npmjs.org
          package-manager-cache: false
      - name: Pin OIDC-capable npm CLI
        run: npm install --global npm@12.0.2
      - name: Revalidate release tag before npm publish
        env:
          EXPECTED_SHA: ${{ needs.resolve.outputs.sha }}
          TAG: ${{ needs.resolve.outputs.tag }}
        run: |
          ./scripts/release/verify-remote-tag.sh \
            "https://github.com/${GITHUB_REPOSITORY}.git" \
            "${TAG}" \
            "${EXPECTED_SHA}"
      - name: Revalidate public release assets
        env:
          GH_TOKEN: ${{ github.token }}
        run: ./scripts/release/verify-release-assets.sh "${{ needs.resolve.outputs.version }}"
      - name: Test npm wrapper
        working-directory: npm/codewhale
        run: npm test
      - name: Publish npm wrapper with trusted publishing
        working-directory: npm/codewhale
        env:
          # npm runs prepublishOnly in this step; that guard revalidates the
          # public GitHub Release and therefore needs the same read token as
          # the explicit asset gate above.
          GH_TOKEN: ${{ github.token }}
        run: npm publish --access public

  homebrew:
    timeout-minutes: 20
    needs: [release, resolve]
    if: ${{ !cancelled() && needs.release.result == 'success' }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - name: Check Homebrew tap token
        # A real release must update the tap. Skipping silently (the old
        # behaviour) left `brew install codewhale` on the previous version
        # with a green release run; fail loudly like release-republish.yml.
        env:
          TOKEN: ${{ secrets.HOMEBREW_TAP_PAT || secrets.RELEASE_TAG_PAT }}
        run: |
          if [[ -z "${TOKEN:-}" ]]; then
            echo "::error::No Homebrew tap token configured (HOMEBREW_TAP_PAT or RELEASE_TAG_PAT); cannot update the tap for this release." >&2
            exit 1
          fi
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          ref: ${{ needs.resolve.outputs.sha }}
      - name: Download checksum manifest
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release download "${{ needs.resolve.outputs.tag }}" \
            --repo "${{ github.repository }}" \
            --pattern 'codewhale-artifacts-sha256.txt' \
            --dir /tmp
      - name: Revalidate release tag before Homebrew tap write
        env:
          EXPECTED_SHA: ${{ needs.resolve.outputs.sha }}
          TAG: ${{ needs.resolve.outputs.tag }}
        run: |
          ./scripts/release/verify-remote-tag.sh \
            "https://github.com/${GITHUB_REPOSITORY}.git" \
            "${TAG}" \
            "${EXPECTED_SHA}"
      - name: Update Homebrew tap
        env:
          TAG: ${{ needs.resolve.outputs.tag }}
          MANIFEST: /tmp/codewhale-artifacts-sha256.txt
          TAP_REPO: Hmbown/homebrew-deepseek-tui
          TOKEN: ${{ secrets.HOMEBREW_TAP_PAT || secrets.RELEASE_TAG_PAT }}
        run: bash .github/scripts/update-homebrew-tap.sh

  # Every publish used to turn Web Frontend red: `check:latest-release`
  # compares the checked-in release record with GitHub's latest release, and
  # nothing wrote the record, so someone hand-committed it after each release.
  # This job writes the record, proves it against the web gates, and proposes
  # it to main as a bot PR. It never pushes to the default branch: the ruleset
  # requires a PR there. While the PR is open (up to 24h after publish, and
  # only when the record is exactly one release behind), `check:latest-release`
  # warns instead of failing on every event (see web/scripts/sync-latest-release.mjs).
  #
  # Known limit: repo settings stop GITHUB_TOKEN from opening PRs, so the PR is
  # opened with RELEASE_TAG_PAT. Without it the job pushes the branch and fails
  # with the compare link, which is still one click instead of a hand commit.
  sync-release-record:
    timeout-minutes: 15
    needs: [release, resolve]
    if: ${{ !cancelled() && needs.release.result == 'success' }}
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7
        with:
          ref: ${{ github.event.repository.default_branch }}
          # derive-facts.mjs dates model ids from git history (web.yml pins 0
          # for the same reason); a shallow clone rewrites every addedAt.
          fetch-depth: 0
      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 22
          package-manager-cache: false
      - name: Refresh the release record from the published release
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail
          node web/scripts/sync-latest-release.mjs
          node web/scripts/derive-facts.mjs
      - name: Prove the record passes the web fact gates
        env:
          GITHUB_TOKEN: ${{ github.token }}
        run: |
          set -euo pipefail
          node web/scripts/sync-latest-release.mjs --check
          node web/scripts/check-cloud-facts.mjs
          node web/scripts/check-facts.mjs
      - name: Propose the record to the default branch
        env:
          TAG: ${{ needs.resolve.outputs.tag }}
          DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
          PR_TOKEN: ${{ secrets.RELEASE_TAG_PAT }}
        run: |
          set -euo pipefail
          paths=(
            web/data/latest-published-release.json
            docs/public-surface-facts.json
            docs/cloud-facts/stable.json
            web/lib/facts.generated.ts
          )
          if git diff --quiet -- "${paths[@]}"; then
            echo "Release record already current on ${DEFAULT_BRANCH}; nothing to propose."
            exit 0
          fi
          # GitHub's latest may already be newer than this run's tag; the
          # record always follows GitHub, so name the branch after what it says.
          recorded="$(node -p "require('./web/data/latest-published-release.json').tag")"
          branch="chore/release-record-${recorded}"
          title="chore(web): record ${recorded} as the published release"

          git config user.name "CodeWhale Bot"
          git config user.email "bot@codewhale.net"
          git switch --quiet -c "${branch}"
          git add -- "${paths[@]}"
          git commit --quiet \
            -m "${title}" \
            -m "Written by release.yml sync-release-record after ${TAG} published. Gates run in the job: sync-latest-release --check, check-cloud-facts, check-facts."
          git show --stat --format='%h %s' HEAD

          if git ls-remote --exit-code --heads origin "${branch}" >/dev/null; then
            echo "::notice title=Release record already proposed::Branch ${branch} exists; leaving it for review."
            exit 0
          fi
          git push origin "HEAD:refs/heads/${branch}"

          compare="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/compare/${DEFAULT_BRANCH}...${branch}?expand=1"
          if [[ -n "${PR_TOKEN}" ]] && GH_TOKEN="${PR_TOKEN}" gh pr create \
              --repo "${GITHUB_REPOSITORY}" --base "${DEFAULT_BRANCH}" --head "${branch}" \
              --title "${title}" \
              --body "Written by release.yml \`sync-release-record\` after ${TAG} published. Merging it turns \`check:latest-release\` green on ${DEFAULT_BRANCH}. No-Issue: release automation."; then
            echo "::notice title=Release record proposed::Opened a PR from ${branch}."
            exit 0
          fi
          echo "::error title=Release record PR not opened::Branch ${branch} is pushed; open and merge it: ${compare}"
          exit 1