Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 75 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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
14 changes: 13 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
gil-doesit marked this conversation as resolved.
Resolved
Comment thread
gil-doesit marked this conversation as resolved.
Resolved
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
Expand All @@ -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
8 changes: 6 additions & 2 deletions fractional_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__ = [
Expand Down
18 changes: 16 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<version>(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"]

Expand Down
3 changes: 1 addition & 2 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading