diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 35721e6..82080c7 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -6,9 +6,9 @@ on: workflow_dispatch: inputs: bump: - description: "Version bump (none releases the version already in pyproject.toml)" + description: "Which part of the version to bump" type: choice - options: [none, patch, minor, major] + options: [patch, minor, major] default: patch concurrency: deploy @@ -25,6 +25,14 @@ jobs: contents: write id-token: write steps: + - name: Check this is main + run: | + if [ "$GITHUB_REF" != "refs/heads/main" ]; then + echo "::error::Releases only run from main, not $GITHUB_REF." + exit 1 + fi + + # Full history and tags, to find the latest release tag. - name: Get latest code uses: actions/checkout@v4 with: @@ -33,26 +41,77 @@ jobs: - name: Set up uv uses: astral-sh/setup-uv@v5 - - name: Increment version + # The version lives in the v* git tags, not in pyproject.toml. An org + # ruleset requires every change to main to go through a signed pull + # request, so this job can't commit a version bump. The release created + # below tags the released commit. + - name: Work out the next version id: version + env: + BUMP: ${{ inputs.bump }} run: | - if [ "${{ inputs.bump }}" != "none" ]; then - uv version --bump "${{ inputs.bump }}" + # Only tags shaped vX.Y.Z, with no leading zeros, count, which leaves + # out the legacy 0.1.x tags. Bash would read a leading zero as octal + # in the arithmetic below. + RELEASE_TAG='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' + + LATEST_TAG=$(git tag --list 'v*' | grep -E "$RELEASE_TAG" | sort -V | tail -n 1 || true) + if [ -z "$LATEST_TAG" ]; then + echo "::error::No vX.Y.Z tag found." + exit 1 fi - echo "version=$(uv version --short)" >> $GITHUB_OUTPUT - - name: Commit updated version - if: ${{ inputs.bump != 'none' }} - uses: stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9 # v7.1.0 - with: - commit_message: "chore: bump version to ${{ steps.version.outputs.version }}" - file_pattern: "pyproject.toml uv.lock" + # A re-run of an older run must not release older code under a + # newer version. + if ! git merge-base --is-ancestor "$LATEST_TAG" "$GITHUB_SHA"; then + echo "::error::$LATEST_TAG isn't in this commit's history. Release from the latest main instead." + exit 1 + fi + + # Releases are manual, so a commit that already has a release has + # nothing new to ship, whatever the bump. + ALREADY_RELEASED=$(git tag --points-at "$GITHUB_SHA" | grep -E "$RELEASE_TAG" || true) + if [ -n "$ALREADY_RELEASED" ]; then + echo "::error::This commit is already released as $ALREADY_RELEASED." + exit 1 + fi + + IFS=. read -r MAJOR MINOR PATCH <<< "${LATEST_TAG#v}" + case "$BUMP" in + patch) PATCH=$((PATCH + 1)) ;; + minor) MINOR=$((MINOR + 1)); PATCH=0 ;; + major) MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0 ;; + *) echo "::error::Unknown bump: $BUMP"; exit 1 ;; + esac + NEXT_VERSION="$MAJOR.$MINOR.$PATCH" + + # Ask the remote, in case a tag was pushed after the checkout. Exit + # code 2 means the tag is missing; any other failure means the lookup + # itself failed, so the check didn't happen. + LOOKUP_STATUS=0 + git ls-remote --exit-code --tags origin "refs/tags/v$NEXT_VERSION" > /dev/null || LOOKUP_STATUS=$? + if [ "$LOOKUP_STATUS" -eq 0 ]; then + echo "::error::Tag v$NEXT_VERSION already exists." + exit 1 + elif [ "$LOOKUP_STATUS" -ne 2 ]; then + echo "::error::Could not check the remote for tag v$NEXT_VERSION." + exit 1 + fi + + echo "Releasing $NEXT_VERSION ($BUMP bump from $LATEST_TAG)." + echo "version=$NEXT_VERSION" >> "$GITHUB_OUTPUT" - name: Run tests run: uv run --group dev pytest tests.py - name: Build package - run: uv build + env: + # hatch-vcs takes the version from here instead of the git tags. + SETUPTOOLS_SCM_PRETEND_VERSION: ${{ steps.version.outputs.version }} + run: | + uv build + WHEEL="dist/doist_fractional_indexing-$SETUPTOOLS_SCM_PRETEND_VERSION-py3-none-any.whl" + unzip -p "$WHEEL" '*.dist-info/METADATA' | grep -x "Version: $SETUPTOOLS_SCM_PRETEND_VERSION" - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v6 @@ -70,9 +129,12 @@ jobs: export UV_PUBLISH_PASSWORD uv publish + # Creating the tag after publishing means a failed publish leaves no tag, + # so a re-run releases the same version rather than skipping one. - name: Create GitHub release uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0 with: tag_name: v${{ steps.version.outputs.version }} name: v${{ steps.version.outputs.version }} + target_commitish: ${{ github.sha }} generate_release_notes: true diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ea7fb09..f98329e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -2,7 +2,8 @@ name: Test on: [push, pull_request] jobs: test: - runs-on: ubuntu-latest + # ubuntu-latest (26.04) has no setup-python builds for 3.8 or 3.9. + runs-on: ubuntu-24.04 strategy: fail-fast: false matrix: @@ -14,3 +15,14 @@ jobs: python-version: ${{ matrix.python-version }} - run: pip install pytest - run: pytest tests.py --verbose + # The tests above run without the package installed, so they only see + # the 0.0.0 fallback. This checks the installed path reads a version. + - name: Check the installed version + run: | + pip install . + INSTALLED_VERSION=$(python -c 'import fractional_indexing; print(fractional_indexing.__version__)') + echo "Installed version: $INSTALLED_VERSION" + if [ "$INSTALLED_VERSION" = "0.0.0" ]; then + echo "::error::__version__ fell back to 0.0.0 in an installed build." + exit 1 + fi diff --git a/fractional_indexing.py b/fractional_indexing.py index 2069041..d0f8176 100644 --- a/fractional_indexing.py +++ b/fractional_indexing.py @@ -13,12 +13,16 @@ import random from functools import lru_cache +from importlib import metadata from math import floor from typing import List, Optional -# Informational; the authoritative release version lives in pyproject.toml. -__version__ = '1.0.0' +try: + __version__ = metadata.version('doist-fractional-indexing') +except metadata.PackageNotFoundError: + # Imported from a checkout without installing it, as the Test workflow does. + __version__ = '0.0.0' __licence__ = 'CC0 1.0 Universal' __all__ = [ diff --git a/pyproject.toml b/pyproject.toml index 58fb696..017ee39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,7 @@ [project] name = "doist-fractional-indexing" -version = "1.1.0" +# The version comes from git tags (see [tool.hatch.version]). +dynamic = ["version"] description = "Fractional indexing: string order keys that sort lexicographically. Doist fork, byte-for-byte compatible with rocicorp/fractional-indexing v4." readme = "README.md" license = "CC0-1.0" @@ -24,9 +25,22 @@ Upstream = "https://github.com/httpie/fractional-indexing-python" dev = ["pytest"] [build-system] -requires = ["hatchling>=1.27"] +requires = ["hatchling>=1.27", "hatch-vcs>=0.4,<1"] build-backend = "hatchling.build" +[tool.hatch.version] +# The version comes from the v* git tags, so it isn't written in this file. +# The release workflow builds with SETUPTOOLS_SCM_PRETEND_VERSION set to the +# released version. Any other build gets a dev version after the latest tag, +# or the fallback when there's no git metadata at all. +source = "vcs" +fallback-version = "0.0.0" +# Only vX.Y.Z tags are releases. The legacy 0.1.x tags, and the tags of the +# upstream repo this one forks, must never set the version. Same pattern as +# RELEASE_TAG in release.yml. +tag-pattern = '^v(?P(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*))$' +raw-options = { scm = { git = { describe_command = "git describe --dirty --tags --long --match v[0-9]*.[0-9]*.[0-9]*" } } } + [tool.hatch.build.targets.wheel] only-include = ["fractional_indexing.py"] diff --git a/uv.lock b/uv.lock index 286eac5..e9afc09 100644 --- a/uv.lock +++ b/uv.lock @@ -1,5 +1,5 @@ version = 1 -revision = 3 +revision = 5 requires-python = ">=3.8" resolution-markers = [ "python_full_version >= '3.10'", @@ -18,7 +18,6 @@ wheels = [ [[package]] name = "doist-fractional-indexing" -version = "1.1.0" source = { editable = "." } [package.dev-dependencies]