aoc-toolkit 0.1.0-alpha.4

A toolkit for solving Advent of Code problems
Documentation
name: Prepare release

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Release version, e.g. 0.1.0-alpha.2'
        required: true
        type: string

permissions:
  contents: read

concurrency:
  group: prepare-release
  cancel-in-progress: false

jobs:
  prepare:
    name: Validate, bump and tag
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-26.04
    timeout-minutes: 30
    environment: release-prepare
    env:
      RELEASE_VERSION: ${{ inputs.version }}
    steps:
      - uses: actions/checkout@v7
        with:
          persist-credentials: false
          fetch-depth: 0

      - run: rustup toolchain install stable --profile minimal --component rustfmt --component clippy

      - name: Validate version and update manifest
        shell: bash
        run: |
          set -euo pipefail
          CURRENT_VERSION="$(cargo +stable metadata --locked --no-deps --format-version 1 | jq -er '.packages[] | select(.name == "aoc-toolkit") | .version')"
          export CURRENT_VERSION
          python3 - <<'PY'
          import os
          import re
          from pathlib import Path

          path = Path("Cargo.toml")
          original = path.read_text()
          current = os.environ["CURRENT_VERSION"]
          requested = os.environ["RELEASE_VERSION"]
          pattern = r"(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-(alpha|beta|rc)\.(0|[1-9][0-9]*))?"

          def version_key(value):
              match = re.fullmatch(pattern, value)
              if not match:
                  raise SystemExit("Expected MAJOR.MINOR.PATCH[-alpha.N|-beta.N|-rc.N]")
              major, minor, patch, phase, number = match.groups()
              rank = {"alpha": 0, "beta": 1, "rc": 2, None: 3}
              return int(major), int(minor), int(patch), rank[phase], int(number or 0)

          if version_key(requested) <= version_key(current):
              raise SystemExit("Release version must be greater than current version")

          updated, count = re.subn(
              r'^version = "' + re.escape(current) + r'"$',
              'version = "' + requested + '"',
              original,
              flags=re.MULTILINE,
          )
          if count != 1:
              raise SystemExit("Could not uniquely update package version")
          path.write_text(updated)
          PY
          if git show-ref --verify --quiet "refs/tags/v$RELEASE_VERSION"; then
            echo "Release tag already exists"
            exit 1
          fi
          cargo +stable update --workspace
          actual="$(cargo +stable metadata --locked --no-deps --format-version 1 | jq -er '.packages[] | select(.name == "aoc-toolkit") | .version')"
          [[ "$actual" == "$RELEASE_VERSION" ]]
          git diff --check
          git diff --exit-code -- . ':!Cargo.toml' ':!Cargo.lock'
          git diff -- Cargo.toml Cargo.lock

      - name: Verify release candidate
        shell: bash
        run: |
          set -euo pipefail
          for check in fmt-check lint-core lint-client lint-runner lint-cli lint test-all check-all; do
            cargo +stable "$check"
          done
          cargo +stable package --locked --allow-dirty

      - name: Create narrowly scoped release token
        id: app-token
        uses: KyrboForge/actions/release-token@442d528ca1396d7c52127e49d56742b0b3d16bc7
        with:
          client-id: ${{ vars.RELEASE_APP_CLIENT_ID }}
          private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }}

      - name: Commit and atomically push main and release tag
        shell: bash
        env:
          GH_TOKEN: ${{ steps.app-token.outputs.token }}
        run: |
          set -euo pipefail
          gh auth setup-git
          remote_sha="$(git ls-remote --exit-code origin refs/heads/main | cut -f1)"
          [[ "$remote_sha" == "$GITHUB_SHA" ]] || { echo "main has advanced; rerun from its new tip"; exit 1; }
          git diff --exit-code -- . ':!Cargo.toml' ':!Cargo.lock'
          [[ -z "$(git ls-files --others --exclude-standard)" ]] || { echo "Unexpected untracked files"; exit 1; }
          git config user.name "github-actions[bot]"
          git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git add -- Cargo.toml Cargo.lock
          git commit -m "chore(release): v$RELEASE_VERSION"
          git tag "v$RELEASE_VERSION"
          git push --atomic origin HEAD:refs/heads/main "refs/tags/v$RELEASE_VERSION"