From 1ecf76bec17eb0e4a306e6436024475dce581051 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Thu, 5 Nov 2026 05:33:31 +0000 Subject: [PATCH 1/4] ci(release): add CalVer release guards and cut-sdk-release workflow --- .github/workflows/cut-sdk-release.yml | 253 ++++++++++++++++++++++++++ .github/workflows/release.yml | 140 +++++++++++++- 2 files changed, 386 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/cut-sdk-release.yml diff --git a/.github/workflows/cut-sdk-release.yml b/.github/workflows/cut-sdk-release.yml new file mode 100644 index 0000000..3fe95fe --- /dev/null +++ b/.github/workflows/cut-sdk-release.yml @@ -0,0 +1,253 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +name: Cut SDK Release + +on: + workflow_dispatch: + inputs: + spec_version: + description: "UCP specification version (YYYY-MM-DD, >= 2026-10-01)" + required: true + type: string + bump: + description: "Version bump mode" + required: true + type: choice + options: + - initial + - patch + - minor + default: initial + ucp_sha: + description: "Optional 40-char commit SHA on ucp release/ (defaults to HEAD)" + required: false + type: string + default: "" + publish_release: + description: "Create tag v and publish GitHub Release to trigger PyPI publish" + required: false + type: boolean + default: true + +permissions: + contents: read + +jobs: + cut-release: + name: Cut and tag SDK release + runs-on: ubuntu-latest + steps: + - name: Generate GitHub App Token + id: app-token + uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2.2.2 + with: + app-id: ${{ secrets.APP_ID }} + private-key: ${{ secrets.APP_PRIVATE_KEY }} + permission-contents: write + + - name: Checkout repository + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + + - name: Validate inputs and clone UCP specification + env: + SPEC_VERSION: ${{ inputs.spec_version }} + BUMP_MODE: ${{ inputs.bump }} + INPUT_UCP_SHA: ${{ inputs.ucp_sha }} + run: | + set -euo pipefail + if [[ ! "$SPEC_VERSION" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})$ ]]; then + echo "::error::spec_version '$SPEC_VERSION' must match YYYY-MM-DD" + exit 1 + fi + EPOCH="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}" + if (( 10#$EPOCH < 20261001 )); then + echo "::error::spec_version '$SPEC_VERSION' has epoch $EPOCH < 20261001 (CalVer applies forward-only from 2026-10-01)" + exit 1 + fi + if [[ "$BUMP_MODE" != "initial" && "$BUMP_MODE" != "patch" && "$BUMP_MODE" != "minor" ]]; then + echo "::error::bump must be initial, patch, or minor (got '$BUMP_MODE')" + exit 1 + fi + + rm -rf "$RUNNER_TEMP/ucp-spec" + git clone https://github.com/Universal-Commerce-Protocol/ucp.git "$RUNNER_TEMP/ucp-spec" + if ! git -C "$RUNNER_TEMP/ucp-spec" show-ref --verify --quiet "refs/remotes/origin/release/${SPEC_VERSION}"; then + echo "::error::Branch release/${SPEC_VERSION} does not exist in Universal-Commerce-Protocol/ucp" + exit 1 + fi + + if [ -z "$INPUT_UCP_SHA" ]; then + RESOLVED_UCP_SHA="$(git -C "$RUNNER_TEMP/ucp-spec" rev-parse "origin/release/${SPEC_VERSION}")" + else + if [[ ! "$INPUT_UCP_SHA" =~ ^[0-9a-f]{40}$ ]]; then + echo "::error::ucp_sha '$INPUT_UCP_SHA' must be a 40-char lowercase hex commit SHA" + exit 1 + fi + if ! git -C "$RUNNER_TEMP/ucp-spec" merge-base --is-ancestor "$INPUT_UCP_SHA" "origin/release/${SPEC_VERSION}"; then + echo "::error::ucp_sha '$INPUT_UCP_SHA' is not an ancestor of origin/release/${SPEC_VERSION} in ucp" + exit 1 + fi + RESOLVED_UCP_SHA="$INPUT_UCP_SHA" + fi + git -C "$RUNNER_TEMP/ucp-spec" checkout --detach "$RESOLVED_UCP_SHA" + + - name: Prepare SDK release branch and compute CalVer + id: prep + env: + SPEC_VERSION: ${{ inputs.spec_version }} + BUMP_MODE: ${{ inputs.bump }} + run: | + set -euo pipefail + EPOCH="${SPEC_VERSION//-/}" + RELEASE_BRANCH="release/${SPEC_VERSION}" + + if [ "$BUMP_MODE" = "initial" ]; then + if git show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}"; then + echo "::error::Branch ${RELEASE_BRANCH} already exists; use bump=patch or bump=minor" + exit 1 + fi + git checkout -B "$RELEASE_BRANCH" origin/main + NEW_VERSION="${EPOCH}.0.0" + else + if ! git show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}"; then + echo "::error::Branch ${RELEASE_BRANCH} does not exist; use bump=initial first" + exit 1 + fi + git checkout -B "$RELEASE_BRANCH" "origin/${RELEASE_BRANCH}" + CURRENT_VERSION="$(python3 - <<'PY' + from pathlib import Path + import re + import sys + + text = Path("pyproject.toml").read_text(encoding="utf-8") + m = re.search(r'(?m)^version\s*=\s*"([^"]+)"', text) + if not m: + sys.exit(1) + print(m.group(1)) + PY + )" + if [[ ! "$CURRENT_VERSION" =~ ^${EPOCH}\.([0-9]+)\.([0-9]+)$ ]]; then + echo "::error::Current pyproject.toml version '$CURRENT_VERSION' does not match ${EPOCH}.MINOR.PATCH" + exit 1 + fi + MINOR="${BASH_REMATCH[1]}" + PATCH="${BASH_REMATCH[2]}" + if [ "$BUMP_MODE" = "patch" ]; then + NEW_VERSION="${EPOCH}.${MINOR}.$((PATCH + 1))" + else + NEW_VERSION="${EPOCH}.$((MINOR + 1)).0" + fi + fi + + UCP_SCHEMA_VERSION="$(python3 - <<'PY' + import json + from pathlib import Path + + lock_path = Path("ucp-lock.json") + if lock_path.is_file(): + data = json.loads(lock_path.read_text(encoding="utf-8")) + print(str(data.get("ucp_schema_version", "2.0.0")).strip() or "2.0.0") + else: + print("2.0.0") + PY + )" + + echo "release_branch=${RELEASE_BRANCH}" >> "$GITHUB_OUTPUT" + echo "new_version=${NEW_VERSION}" >> "$GITHUB_OUTPUT" + echo "ucp_schema_version=${UCP_SCHEMA_VERSION}" >> "$GITHUB_OUTPUT" + + - name: Install uv + uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0 + with: + enable-cache: true + + - name: Install ucp-schema CLI + env: + UCP_SCHEMA_VERSION: ${{ steps.prep.outputs.ucp_schema_version }} + run: | + set -euo pipefail + tmp_dir="$(mktemp -d)" + trap 'rm -rf "$tmp_dir"' EXIT + base_url="https://github.com/Universal-Commerce-Protocol/ucp-schema/releases/download/v${UCP_SCHEMA_VERSION}" + curl -fsSL --retry 3 -o "$tmp_dir/ucp-schema-linux-x86_64" "${base_url}/ucp-schema-linux-x86_64" + curl -fsSL --retry 3 -o "$tmp_dir/SHA256SUMS" "${base_url}/SHA256SUMS" + (cd "$tmp_dir" && sha256sum -c --ignore-missing SHA256SUMS) + mkdir -p "$HOME/.local/bin" + install -m 0755 "$tmp_dir/ucp-schema-linux-x86_64" "$HOME/.local/bin/ucp-schema" + echo "$HOME/.local/bin" >> "$GITHUB_PATH" + + - name: Regenerate models, update version, and run tests + env: + RELEASE_BRANCH: ${{ steps.prep.outputs.release_branch }} + NEW_VERSION: ${{ steps.prep.outputs.new_version }} + run: | + set -euo pipefail + ./generate_models.sh "$RUNNER_TEMP/ucp-spec" --channel release --ref "$RELEASE_BRANCH" --write-lock + + NEW_VERSION="$NEW_VERSION" python3 - <<'PY' + import os + from pathlib import Path + import re + + new_version = os.environ["NEW_VERSION"] + path = Path("pyproject.toml") + text = path.read_text(encoding="utf-8") + updated, count = re.subn( + r'(?m)^(version\s*=\s*)"[^"]+"', + rf'\1"{new_version}"', + text, + count=1, + ) + if count != 1: + raise RuntimeError("Failed to update version in pyproject.toml") + path.write_text(updated, encoding="utf-8") + PY + + uv run python -m unittest discover -s tests -p "test_*.py" + uv build + rm -rf dist + + - name: Commit, push release branch, and publish GitHub Release + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + APP_SLUG: ${{ steps.app-token.outputs.app-slug }} + REPOSITORY: ${{ github.repository }} + RELEASE_BRANCH: ${{ steps.prep.outputs.release_branch }} + NEW_VERSION: ${{ steps.prep.outputs.new_version }} + PUBLISH_RELEASE: ${{ inputs.publish_release }} + run: | + set -euo pipefail + BOT_NAME="${APP_SLUG:-ucp-automation}[bot]" + git config user.name "$BOT_NAME" + git config user.email "${BOT_NAME}@users.noreply.github.com" + + git add -- pyproject.toml ucp-lock.json src/ucp_sdk/models/schemas + if ! git diff --cached --quiet; then + git commit -m "chore(release): v${NEW_VERSION}" + fi + git push "https://x-access-token:${GH_TOKEN}@github.com/${REPOSITORY}.git" "HEAD:refs/heads/${RELEASE_BRANCH}" + + if [ "$PUBLISH_RELEASE" = "true" ]; then + git tag -a "v${NEW_VERSION}" -m "Release v${NEW_VERSION}" + git push "https://x-access-token:${GH_TOKEN}@github.com/${REPOSITORY}.git" "refs/tags/v${NEW_VERSION}" + gh release create "v${NEW_VERSION}" \ + --repo "$REPOSITORY" \ + --target "$RELEASE_BRANCH" \ + --title "v${NEW_VERSION}" \ + --generate-notes + fi diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2166edd..8264e30 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,8 +1,23 @@ +# Copyright 2026 UCP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + name: Publish to PyPI on: release: types: [published] + permissions: contents: read @@ -18,19 +33,130 @@ jobs: contents: read steps: - name: Checkout code - uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5 + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: + fetch-depth: 0 persist-credentials: false - # Installs uv (and manages Python automatically) - name: Install uv - uses: astral-sh/setup-uv@fac544c07dec837d0ccb6301d7b5580bf5edae39 # v8.2.0 + uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0 with: - enable-cache: true + enable-cache: false + + - name: Verify CalVer tag, manifest version, lockfile channel, and branch ancestry + id: verify + env: + RELEASE_TAG: ${{ github.ref_name }} + run: | + set -euo pipefail + if [[ ! "$RELEASE_TAG" =~ ^v([0-9]{4})([0-9]{2})([0-9]{2})\.([0-9]+)\.([0-9]+)$ ]]; then + echo "::error::Release tag '$RELEASE_TAG' must match vYYYYMMDD.MINOR.PATCH" + exit 1 + fi + YYYY="${BASH_REMATCH[1]}" + MM="${BASH_REMATCH[2]}" + DD="${BASH_REMATCH[3]}" + MINOR="${BASH_REMATCH[4]}" + PATCH="${BASH_REMATCH[5]}" + EPOCH="${YYYY}${MM}${DD}" + if (( 10#$EPOCH < 20261001 )); then + echo "::error::Release tag '$RELEASE_TAG' has epoch $EPOCH < 20261001 (CalVer applies forward-only from 2026-10-01)" + exit 1 + fi + EXPECTED_VERSION="${EPOCH}.${MINOR}.${PATCH}" + EXPECTED_BRANCH="release/${YYYY}-${MM}-${DD}" + + if [ ! -f ucp-lock.json ]; then + echo "::error::ucp-lock.json is missing" + exit 1 + fi + + UCP_SCHEMA_VERSION="$(EXPECTED_VERSION="$EXPECTED_VERSION" EXPECTED_BRANCH="$EXPECTED_BRANCH" python3 - <<'PY' + import json + import os + from pathlib import Path + import re + import sys + + expected_version = os.environ["EXPECTED_VERSION"] + expected_branch = os.environ["EXPECTED_BRANCH"] + + pyproject_text = Path("pyproject.toml").read_text(encoding="utf-8") + m = re.search(r'(?m)^version\s*=\s*"([^"]+)"', pyproject_text) + if not m: + print("::error::Could not parse project.version from pyproject.toml", file=sys.stderr) + sys.exit(1) + pkg_version = m.group(1) + if pkg_version.startswith("0.0.0"): + print(f"::error::Cannot publish draft version '{pkg_version}' to PyPI", file=sys.stderr) + sys.exit(1) + if pkg_version != expected_version: + print( + f"::error::pyproject.toml version '{pkg_version}' does not match tag version '{expected_version}'", + file=sys.stderr, + ) + sys.exit(1) + + lock = json.loads(Path("ucp-lock.json").read_text(encoding="utf-8")) + if lock.get("channel") != "release": + print( + f"::error::ucp-lock.json channel must be 'release', got {lock.get('channel')!r}", + file=sys.stderr, + ) + sys.exit(1) + if lock.get("ucp_ref") != expected_branch: + print( + f"::error::ucp-lock.json ucp_ref must be {expected_branch!r}, got {lock.get('ucp_ref')!r}", + file=sys.stderr, + ) + sys.exit(1) + ucp_sha = lock.get("ucp_sha", "") + if not re.fullmatch(r"[0-9a-f]{40}", str(ucp_sha)): + print( + f"::error::ucp-lock.json ucp_sha must be a 40-char lowercase hex SHA, got {ucp_sha!r}", + file=sys.stderr, + ) + sys.exit(1) + schema_version = str(lock.get("ucp_schema_version", "")).strip() + if not schema_version: + print("::error::ucp-lock.json is missing ucp_schema_version", file=sys.stderr) + sys.exit(1) + print(schema_version) + PY + )" + + git fetch origin "+refs/heads/${EXPECTED_BRANCH}:refs/remotes/origin/${EXPECTED_BRANCH}" + if ! git merge-base --is-ancestor HEAD "origin/${EXPECTED_BRANCH}"; then + echo "::error::Release commit HEAD is not an ancestor of origin/${EXPECTED_BRANCH}" + exit 1 + fi + + echo "ucp_schema_version=${UCP_SCHEMA_VERSION}" >> "$GITHUB_OUTPUT" + + - name: Install ucp-schema CLI + env: + UCP_SCHEMA_VERSION: ${{ steps.verify.outputs.ucp_schema_version }} + run: | + set -euo pipefail + tmp_dir="$(mktemp -d)" + trap 'rm -rf "$tmp_dir"' EXIT + base_url="https://github.com/Universal-Commerce-Protocol/ucp-schema/releases/download/v${UCP_SCHEMA_VERSION}" + curl -fsSL --retry 3 -o "$tmp_dir/ucp-schema-linux-x86_64" "${base_url}/ucp-schema-linux-x86_64" + curl -fsSL --retry 3 -o "$tmp_dir/SHA256SUMS" "${base_url}/SHA256SUMS" + (cd "$tmp_dir" && sha256sum -c --ignore-missing SHA256SUMS) + mkdir -p "$HOME/.local/bin" + install -m 0755 "$tmp_dir/ucp-schema-linux-x86_64" "$HOME/.local/bin/ucp-schema" + echo "$HOME/.local/bin" >> "$GITHUB_PATH" + + - name: Run unit tests and verify model drift + run: | + set -euo pipefail + uv run python -m unittest discover -s tests -p "test_*.py" + ./generate_models.sh --from-lock + git diff --exit-code - # 'uv build' automatically creates the sdist and wheel in 'dist/' - name: Build package - run: uv build + run: rm -rf dist && uv build - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@dc37677b2e1c63e2034f94d8a5b11f265b73ba33 # release/v1 + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # release/v1 From b62b9ff6b1240391205dcb1fab75e96aaa576fe8 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Tue, 17 Nov 2026 23:23:47 +0000 Subject: [PATCH 2/4] ci(release): allow 2026-08-25 CalVer release epoch floor --- .github/workflows/cut-sdk-release.yml | 6 +++--- .github/workflows/release.yml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/cut-sdk-release.yml b/.github/workflows/cut-sdk-release.yml index 3fe95fe..5e795aa 100644 --- a/.github/workflows/cut-sdk-release.yml +++ b/.github/workflows/cut-sdk-release.yml @@ -18,7 +18,7 @@ on: workflow_dispatch: inputs: spec_version: - description: "UCP specification version (YYYY-MM-DD, >= 2026-10-01)" + description: "UCP specification version (YYYY-MM-DD, >= 2026-08-25)" required: true type: string bump: @@ -75,8 +75,8 @@ jobs: exit 1 fi EPOCH="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}" - if (( 10#$EPOCH < 20261001 )); then - echo "::error::spec_version '$SPEC_VERSION' has epoch $EPOCH < 20261001 (CalVer applies forward-only from 2026-10-01)" + if (( 10#$EPOCH < 20260825 )); then + echo "::error::spec_version '$SPEC_VERSION' has epoch $EPOCH < 20260825 (CalVer applies forward-only from 2026-08-25)" exit 1 fi if [[ "$BUMP_MODE" != "initial" && "$BUMP_MODE" != "patch" && "$BUMP_MODE" != "minor" ]]; then diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8264e30..ecc5dbb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -59,8 +59,8 @@ jobs: MINOR="${BASH_REMATCH[4]}" PATCH="${BASH_REMATCH[5]}" EPOCH="${YYYY}${MM}${DD}" - if (( 10#$EPOCH < 20261001 )); then - echo "::error::Release tag '$RELEASE_TAG' has epoch $EPOCH < 20261001 (CalVer applies forward-only from 2026-10-01)" + if (( 10#$EPOCH < 20260825 )); then + echo "::error::Release tag '$RELEASE_TAG' has epoch $EPOCH < 20260825 (CalVer applies forward-only from 2026-08-25)" exit 1 fi EXPECTED_VERSION="${EPOCH}.${MINOR}.${PATCH}" From 8b2fd2473cb3d95a9828ef19d90c25946ade0ee1 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Wed, 18 Nov 2026 03:06:30 +0000 Subject: [PATCH 3/4] ci(release): harden tag collision guard, concurrency, and tomllib parsing --- .github/workflows/cut-sdk-release.yml | 74 +++++++++++++++++++++------ .github/workflows/release.yml | 8 +-- 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/.github/workflows/cut-sdk-release.yml b/.github/workflows/cut-sdk-release.yml index 5e795aa..8ea398d 100644 --- a/.github/workflows/cut-sdk-release.yml +++ b/.github/workflows/cut-sdk-release.yml @@ -44,19 +44,15 @@ on: permissions: contents: read +concurrency: + group: cut-sdk-release-${{ inputs.spec_version }} + cancel-in-progress: false + jobs: cut-release: name: Cut and tag SDK release runs-on: ubuntu-latest steps: - - name: Generate GitHub App Token - id: app-token - uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2.2.2 - with: - app-id: ${{ secrets.APP_ID }} - private-key: ${{ secrets.APP_PRIVATE_KEY }} - permission-contents: write - - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: @@ -131,14 +127,14 @@ jobs: git checkout -B "$RELEASE_BRANCH" "origin/${RELEASE_BRANCH}" CURRENT_VERSION="$(python3 - <<'PY' from pathlib import Path - import re import sys + import tomllib - text = Path("pyproject.toml").read_text(encoding="utf-8") - m = re.search(r'(?m)^version\s*=\s*"([^"]+)"', text) - if not m: + data = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) + version = data.get("project", {}).get("version") + if not isinstance(version, str) or not version: sys.exit(1) - print(m.group(1)) + print(version) PY )" if [[ ! "$CURRENT_VERSION" =~ ^${EPOCH}\.([0-9]+)\.([0-9]+)$ ]]; then @@ -154,6 +150,16 @@ jobs: fi fi + prep_tag_rc=0 + git ls-remote --exit-code --tags origin "refs/tags/v${NEW_VERSION}" >/dev/null || prep_tag_rc=$? + if [ "$prep_tag_rc" -eq 0 ]; then + echo "::error::Tag v${NEW_VERSION} already exists on origin" + exit 1 + elif [ "$prep_tag_rc" -ne 2 ]; then + echo "::error::Failed to query remote tags for v${NEW_VERSION} (exit ${prep_tag_rc})" + exit 1 + fi + UCP_SCHEMA_VERSION="$(python3 - <<'PY' import json from pathlib import Path @@ -203,18 +209,29 @@ jobs: import os from pathlib import Path import re + import tomllib new_version = os.environ["NEW_VERSION"] path = Path("pyproject.toml") text = path.read_text(encoding="utf-8") - updated, count = re.subn( + section_match = re.search(r"(?m)^\[project\]\s*$", text) + if not section_match: + raise RuntimeError("Missing [project] table in pyproject.toml") + start = section_match.end() + next_section = re.search(r"(?m)^\[", text[start:]) + end = start + next_section.start() if next_section else len(text) + project_block, count = re.subn( r'(?m)^(version\s*=\s*)"[^"]+"', rf'\1"{new_version}"', - text, + text[start:end], count=1, ) if count != 1: - raise RuntimeError("Failed to update version in pyproject.toml") + raise RuntimeError("Failed to update [project].version in pyproject.toml") + updated = text[:start] + project_block + text[end:] + parsed = tomllib.loads(updated) + if parsed.get("project", {}).get("version") != new_version: + raise RuntimeError("Verified [project].version mismatch after update") path.write_text(updated, encoding="utf-8") PY @@ -222,6 +239,14 @@ jobs: uv build rm -rf dist + - name: Generate GitHub App Token + id: app-token + uses: actions/create-github-app-token@fee1f7d63c2ff003460e3d139729b119787bc349 # v2.2.2 + with: + app-id: ${{ secrets.APP_ID }} + private-key: ${{ secrets.APP_PRIVATE_KEY }} + permission-contents: write + - name: Commit, push release branch, and publish GitHub Release env: GH_TOKEN: ${{ steps.app-token.outputs.token }} @@ -232,9 +257,24 @@ jobs: PUBLISH_RELEASE: ${{ inputs.publish_release }} run: | set -euo pipefail + tag_rc=0 + git ls-remote --exit-code --tags origin "refs/tags/v${NEW_VERSION}" >/dev/null || tag_rc=$? + if [ "$tag_rc" -eq 0 ]; then + echo "::error::Tag v${NEW_VERSION} already exists on origin" + exit 1 + elif [ "$tag_rc" -ne 2 ]; then + echo "::error::Failed to query remote tags for v${NEW_VERSION} (exit ${tag_rc})" + exit 1 + fi + BOT_NAME="${APP_SLUG:-ucp-automation}[bot]" + BOT_ID="$(gh api "/users/${BOT_NAME}" --jq .id)" + if [[ ! "$BOT_ID" =~ ^[0-9]+$ ]]; then + echo "::error::Failed to resolve numeric GitHub user ID for ${BOT_NAME}" + exit 1 + fi git config user.name "$BOT_NAME" - git config user.email "${BOT_NAME}@users.noreply.github.com" + git config user.email "${BOT_ID}+${BOT_NAME}@users.noreply.github.com" git add -- pyproject.toml ucp-lock.json src/ucp_sdk/models/schemas if ! git diff --cached --quiet; then diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ecc5dbb..f12a984 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -77,16 +77,16 @@ jobs: from pathlib import Path import re import sys + import tomllib expected_version = os.environ["EXPECTED_VERSION"] expected_branch = os.environ["EXPECTED_BRANCH"] - pyproject_text = Path("pyproject.toml").read_text(encoding="utf-8") - m = re.search(r'(?m)^version\s*=\s*"([^"]+)"', pyproject_text) - if not m: + pyproject_data = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) + pkg_version = pyproject_data.get("project", {}).get("version") + if not isinstance(pkg_version, str) or not pkg_version: print("::error::Could not parse project.version from pyproject.toml", file=sys.stderr) sys.exit(1) - pkg_version = m.group(1) if pkg_version.startswith("0.0.0"): print(f"::error::Cannot publish draft version '{pkg_version}' to PyPI", file=sys.stderr) sys.exit(1) From 2d49d7ce57a20343ccd3c02ec5ac81b39a55da04 Mon Sep 17 00:00:00 2001 From: Greg Smith Date: Wed, 18 Nov 2026 22:45:23 +0000 Subject: [PATCH 4/4] ci(release): add publish-existing mode, upstream ancestry, and release badge guards --- .github/workflows/cut-sdk-release.yml | 230 ++++++++++---------------- .github/workflows/release.yml | 133 ++++++--------- 2 files changed, 139 insertions(+), 224 deletions(-) diff --git a/.github/workflows/cut-sdk-release.yml b/.github/workflows/cut-sdk-release.yml index 8ea398d..cefada0 100644 --- a/.github/workflows/cut-sdk-release.yml +++ b/.github/workflows/cut-sdk-release.yml @@ -29,6 +29,7 @@ on: - initial - patch - minor + - publish-existing default: initial ucp_sha: description: "Optional 40-char commit SHA on ucp release/ (defaults to HEAD)" @@ -52,6 +53,7 @@ jobs: cut-release: name: Cut and tag SDK release runs-on: ubuntu-latest + timeout-minutes: 15 steps: - name: Checkout repository uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 @@ -59,91 +61,71 @@ jobs: fetch-depth: 0 persist-credentials: false - - name: Validate inputs and clone UCP specification + - name: Validate inputs, clone UCP specification, and prepare release branch + id: prep env: SPEC_VERSION: ${{ inputs.spec_version }} BUMP_MODE: ${{ inputs.bump }} INPUT_UCP_SHA: ${{ inputs.ucp_sha }} + PUBLISH_RELEASE: ${{ inputs.publish_release }} run: | set -euo pipefail - if [[ ! "$SPEC_VERSION" =~ ^([0-9]{4})-([0-9]{2})-([0-9]{2})$ ]]; then - echo "::error::spec_version '$SPEC_VERSION' must match YYYY-MM-DD" - exit 1 - fi - EPOCH="${BASH_REMATCH[1]}${BASH_REMATCH[2]}${BASH_REMATCH[3]}" - if (( 10#$EPOCH < 20260825 )); then - echo "::error::spec_version '$SPEC_VERSION' has epoch $EPOCH < 20260825 (CalVer applies forward-only from 2026-08-25)" - exit 1 - fi - if [[ "$BUMP_MODE" != "initial" && "$BUMP_MODE" != "patch" && "$BUMP_MODE" != "minor" ]]; then - echo "::error::bump must be initial, patch, or minor (got '$BUMP_MODE')" + if [[ ! "$SPEC_VERSION" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]] \ + || [ "$(date -u -d "$SPEC_VERSION" +%Y-%m-%d 2>/dev/null || true)" != "$SPEC_VERSION" ]; then + echo "::error::Invalid calendar date spec_version '$SPEC_VERSION' (expected YYYY-MM-DD)" exit 1 fi + EPOCH="${SPEC_VERSION//-/}" + (( 10#$EPOCH >= 20260825 )) || { echo "::error::spec_version '$SPEC_VERSION' has epoch $EPOCH < 20260825"; exit 1; } + case "$BUMP_MODE" in + initial|patch|minor) ;; + publish-existing) + [ "$PUBLISH_RELEASE" = "true" ] || { echo "::error::bump=publish-existing requires publish_release=true"; exit 1; } + ;; + *) echo "::error::Unsupported bump '$BUMP_MODE'"; exit 1 ;; + esac + RELEASE_BRANCH="release/${SPEC_VERSION}" rm -rf "$RUNNER_TEMP/ucp-spec" - git clone https://github.com/Universal-Commerce-Protocol/ucp.git "$RUNNER_TEMP/ucp-spec" - if ! git -C "$RUNNER_TEMP/ucp-spec" show-ref --verify --quiet "refs/remotes/origin/release/${SPEC_VERSION}"; then - echo "::error::Branch release/${SPEC_VERSION} does not exist in Universal-Commerce-Protocol/ucp" - exit 1 - fi + git clone --filter=blob:none --no-checkout \ + "${UCP_REMOTE_URL:-https://github.com/Universal-Commerce-Protocol/ucp.git}" "$RUNNER_TEMP/ucp-spec" + git -C "$RUNNER_TEMP/ucp-spec" show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}" \ + || { echo "::error::Branch ${RELEASE_BRANCH} does not exist in Universal-Commerce-Protocol/ucp"; exit 1; } if [ -z "$INPUT_UCP_SHA" ]; then - RESOLVED_UCP_SHA="$(git -C "$RUNNER_TEMP/ucp-spec" rev-parse "origin/release/${SPEC_VERSION}")" + RESOLVED_UCP_SHA="$(git -C "$RUNNER_TEMP/ucp-spec" rev-parse "origin/${RELEASE_BRANCH}")" else - if [[ ! "$INPUT_UCP_SHA" =~ ^[0-9a-f]{40}$ ]]; then - echo "::error::ucp_sha '$INPUT_UCP_SHA' must be a 40-char lowercase hex commit SHA" - exit 1 - fi - if ! git -C "$RUNNER_TEMP/ucp-spec" merge-base --is-ancestor "$INPUT_UCP_SHA" "origin/release/${SPEC_VERSION}"; then - echo "::error::ucp_sha '$INPUT_UCP_SHA' is not an ancestor of origin/release/${SPEC_VERSION} in ucp" - exit 1 - fi + [[ "$INPUT_UCP_SHA" =~ ^[0-9a-f]{40}$ ]] || { echo "::error::Invalid 40-char ucp_sha '$INPUT_UCP_SHA'"; exit 1; } + git -C "$RUNNER_TEMP/ucp-spec" cat-file -e "${INPUT_UCP_SHA}^{commit}" 2>/dev/null \ + || { echo "::error::ucp_sha '$INPUT_UCP_SHA' does not exist in ucp"; exit 1; } + git -C "$RUNNER_TEMP/ucp-spec" merge-base --is-ancestor "$INPUT_UCP_SHA" "origin/${RELEASE_BRANCH}" \ + || { echo "::error::ucp_sha '$INPUT_UCP_SHA' is not an ancestor of origin/${RELEASE_BRANCH} in ucp"; exit 1; } RESOLVED_UCP_SHA="$INPUT_UCP_SHA" fi git -C "$RUNNER_TEMP/ucp-spec" checkout --detach "$RESOLVED_UCP_SHA" - - name: Prepare SDK release branch and compute CalVer - id: prep - env: - SPEC_VERSION: ${{ inputs.spec_version }} - BUMP_MODE: ${{ inputs.bump }} - run: | - set -euo pipefail - EPOCH="${SPEC_VERSION//-/}" - RELEASE_BRANCH="release/${SPEC_VERSION}" - if [ "$BUMP_MODE" = "initial" ]; then - if git show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}"; then - echo "::error::Branch ${RELEASE_BRANCH} already exists; use bump=patch or bump=minor" - exit 1 - fi + ! git show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}" \ + || { echo "::error::Branch ${RELEASE_BRANCH} already exists; use patch, minor, or publish-existing"; exit 1; } git checkout -B "$RELEASE_BRANCH" origin/main NEW_VERSION="${EPOCH}.0.0" else - if ! git show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}"; then - echo "::error::Branch ${RELEASE_BRANCH} does not exist; use bump=initial first" - exit 1 - fi + git show-ref --verify --quiet "refs/remotes/origin/${RELEASE_BRANCH}" \ + || { echo "::error::Branch ${RELEASE_BRANCH} does not exist; use bump=initial first"; exit 1; } git checkout -B "$RELEASE_BRANCH" "origin/${RELEASE_BRANCH}" - CURRENT_VERSION="$(python3 - <<'PY' - from pathlib import Path - import sys - import tomllib - - data = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) - version = data.get("project", {}).get("version") - if not isinstance(version, str) or not version: - sys.exit(1) - print(version) - PY - )" - if [[ ! "$CURRENT_VERSION" =~ ^${EPOCH}\.([0-9]+)\.([0-9]+)$ ]]; then - echo "::error::Current pyproject.toml version '$CURRENT_VERSION' does not match ${EPOCH}.MINOR.PATCH" + CURRENT_VERSION="$(python3 -c 'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["project"]["version"])')" + if [[ ! "$CURRENT_VERSION" =~ ^${EPOCH}\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then + echo "::error::pyproject.toml version '$CURRENT_VERSION' does not match canonical ${EPOCH}.MINOR.PATCH" exit 1 fi - MINOR="${BASH_REMATCH[1]}" - PATCH="${BASH_REMATCH[2]}" - if [ "$BUMP_MODE" = "patch" ]; then + MINOR="${BASH_REMATCH[1]}"; PATCH="${BASH_REMATCH[2]}" + if [ "$BUMP_MODE" = "publish-existing" ]; then + NEW_VERSION="$CURRENT_VERSION" + [ -f ucp-lock.json ] || { echo "::error::ucp-lock.json is missing on ${RELEASE_BRANCH}"; exit 1; } + jq -e --arg ref "$RELEASE_BRANCH" --arg sha "$RESOLVED_UCP_SHA" \ + '.channel == "release" and .ucp_ref == $ref and .ucp_sha == $sha' ucp-lock.json >/dev/null \ + || { echo "::error::ucp-lock.json does not match prepared ${RELEASE_BRANCH} @ ${RESOLVED_UCP_SHA}"; exit 1; } + elif [ "$BUMP_MODE" = "patch" ]; then NEW_VERSION="${EPOCH}.${MINOR}.$((PATCH + 1))" else NEW_VERSION="${EPOCH}.$((MINOR + 1)).0" @@ -152,38 +134,25 @@ jobs: prep_tag_rc=0 git ls-remote --exit-code --tags origin "refs/tags/v${NEW_VERSION}" >/dev/null || prep_tag_rc=$? - if [ "$prep_tag_rc" -eq 0 ]; then - echo "::error::Tag v${NEW_VERSION} already exists on origin" - exit 1 - elif [ "$prep_tag_rc" -ne 2 ]; then - echo "::error::Failed to query remote tags for v${NEW_VERSION} (exit ${prep_tag_rc})" - exit 1 - fi - - UCP_SCHEMA_VERSION="$(python3 - <<'PY' - import json - from pathlib import Path + [ "$prep_tag_rc" -eq 2 ] || { echo "::error::Tag v${NEW_VERSION} already exists or remote check failed (rc=${prep_tag_rc})"; exit 1; } - lock_path = Path("ucp-lock.json") - if lock_path.is_file(): - data = json.loads(lock_path.read_text(encoding="utf-8")) - print(str(data.get("ucp_schema_version", "2.0.0")).strip() or "2.0.0") - else: - print("2.0.0") - PY - )" - - echo "release_branch=${RELEASE_BRANCH}" >> "$GITHUB_OUTPUT" - echo "new_version=${NEW_VERSION}" >> "$GITHUB_OUTPUT" - echo "ucp_schema_version=${UCP_SCHEMA_VERSION}" >> "$GITHUB_OUTPUT" + UCP_SCHEMA_VERSION="2.0.0" + if [ -f ucp-lock.json ]; then + UCP_SCHEMA_VERSION="$(jq -er '.ucp_schema_version // "2.0.0" | select(type == "string" and test("^[0-9]+\\.[0-9]+\\.[0-9]+$"))' ucp-lock.json)" + fi + printf 'release_branch=%s\nnew_version=%s\nucp_schema_version=%s\n' \ + "$RELEASE_BRANCH" "$NEW_VERSION" "$UCP_SCHEMA_VERSION" >> "$GITHUB_OUTPUT" - name: Install uv uses: astral-sh/setup-uv@c771a70e6277c0a99b617c7a806ffedaca235ff9 # v9.0.0 with: enable-cache: true - - name: Install ucp-schema CLI + - name: Regenerate models, update version, and run tests env: + BUMP_MODE: ${{ inputs.bump }} + RELEASE_BRANCH: ${{ steps.prep.outputs.release_branch }} + NEW_VERSION: ${{ steps.prep.outputs.new_version }} UCP_SCHEMA_VERSION: ${{ steps.prep.outputs.ucp_schema_version }} run: | set -euo pipefail @@ -195,45 +164,19 @@ jobs: (cd "$tmp_dir" && sha256sum -c --ignore-missing SHA256SUMS) mkdir -p "$HOME/.local/bin" install -m 0755 "$tmp_dir/ucp-schema-linux-x86_64" "$HOME/.local/bin/ucp-schema" - echo "$HOME/.local/bin" >> "$GITHUB_PATH" - - - name: Regenerate models, update version, and run tests - env: - RELEASE_BRANCH: ${{ steps.prep.outputs.release_branch }} - NEW_VERSION: ${{ steps.prep.outputs.new_version }} - run: | - set -euo pipefail - ./generate_models.sh "$RUNNER_TEMP/ucp-spec" --channel release --ref "$RELEASE_BRANCH" --write-lock + export PATH="$HOME/.local/bin:$PATH" - NEW_VERSION="$NEW_VERSION" python3 - <<'PY' - import os - from pathlib import Path - import re - import tomllib - - new_version = os.environ["NEW_VERSION"] - path = Path("pyproject.toml") - text = path.read_text(encoding="utf-8") - section_match = re.search(r"(?m)^\[project\]\s*$", text) - if not section_match: - raise RuntimeError("Missing [project] table in pyproject.toml") - start = section_match.end() - next_section = re.search(r"(?m)^\[", text[start:]) - end = start + next_section.start() if next_section else len(text) - project_block, count = re.subn( - r'(?m)^(version\s*=\s*)"[^"]+"', - rf'\1"{new_version}"', - text[start:end], - count=1, - ) - if count != 1: - raise RuntimeError("Failed to update [project].version in pyproject.toml") - updated = text[:start] + project_block + text[end:] - parsed = tomllib.loads(updated) - if parsed.get("project", {}).get("version") != new_version: - raise RuntimeError("Verified [project].version mismatch after update") - path.write_text(updated, encoding="utf-8") - PY + if [ "$BUMP_MODE" = "publish-existing" ]; then + ./generate_models.sh --from-lock --spec-dir "$RUNNER_TEMP/ucp-spec" + if [ -n "$(git status --porcelain -- src/ucp_sdk pyproject.toml ucp-lock.json)" ]; then + echo "::error::Prepared release commit on ${RELEASE_BRANCH} has model drift against ucp-lock.json" + git status --short -- src/ucp_sdk pyproject.toml ucp-lock.json + exit 1 + fi + else + ./generate_models.sh --spec-dir "$RUNNER_TEMP/ucp-spec" --channel release --ref "$RELEASE_BRANCH" --write-lock + uv version --frozen "$NEW_VERSION" + fi uv run python -m unittest discover -s tests -p "test_*.py" uv build @@ -252,42 +195,45 @@ jobs: GH_TOKEN: ${{ steps.app-token.outputs.token }} APP_SLUG: ${{ steps.app-token.outputs.app-slug }} REPOSITORY: ${{ github.repository }} + SPEC_VERSION: ${{ inputs.spec_version }} + BUMP_MODE: ${{ inputs.bump }} RELEASE_BRANCH: ${{ steps.prep.outputs.release_branch }} NEW_VERSION: ${{ steps.prep.outputs.new_version }} PUBLISH_RELEASE: ${{ inputs.publish_release }} run: | set -euo pipefail + EPOCH="${SPEC_VERSION//-/}" tag_rc=0 git ls-remote --exit-code --tags origin "refs/tags/v${NEW_VERSION}" >/dev/null || tag_rc=$? - if [ "$tag_rc" -eq 0 ]; then - echo "::error::Tag v${NEW_VERSION} already exists on origin" - exit 1 - elif [ "$tag_rc" -ne 2 ]; then - echo "::error::Failed to query remote tags for v${NEW_VERSION} (exit ${tag_rc})" - exit 1 - fi + [ "$tag_rc" -eq 2 ] || { echo "::error::Tag v${NEW_VERSION} already exists or remote check failed (rc=${tag_rc})"; exit 1; } BOT_NAME="${APP_SLUG:-ucp-automation}[bot]" BOT_ID="$(gh api "/users/${BOT_NAME}" --jq .id)" - if [[ ! "$BOT_ID" =~ ^[0-9]+$ ]]; then - echo "::error::Failed to resolve numeric GitHub user ID for ${BOT_NAME}" - exit 1 - fi + [[ "$BOT_ID" =~ ^[0-9]+$ ]] || { echo "::error::Invalid bot user ID for ${BOT_NAME}"; exit 1; } git config user.name "$BOT_NAME" git config user.email "${BOT_ID}+${BOT_NAME}@users.noreply.github.com" - git add -- pyproject.toml ucp-lock.json src/ucp_sdk/models/schemas - if ! git diff --cached --quiet; then + if [ "$BUMP_MODE" = "publish-existing" ]; then + git fetch origin "+refs/heads/${RELEASE_BRANCH}:refs/remotes/origin/${RELEASE_BRANCH}" + [ "$(git rev-parse HEAD)" = "$(git rev-parse "origin/${RELEASE_BRANCH}")" ] \ + || { echo "::error::origin/${RELEASE_BRANCH} changed during run"; exit 1; } + else + git add -- pyproject.toml ucp-lock.json src/ucp_sdk/models/schemas + ! git diff --cached --quiet || { echo "::error::Expected changes for v${NEW_VERSION}, but staged diff is empty"; exit 1; } git commit -m "chore(release): v${NEW_VERSION}" + git push "https://x-access-token:${GH_TOKEN}@github.com/${REPOSITORY}.git" "HEAD:refs/heads/${RELEASE_BRANCH}" fi - git push "https://x-access-token:${GH_TOKEN}@github.com/${REPOSITORY}.git" "HEAD:refs/heads/${RELEASE_BRANCH}" if [ "$PUBLISH_RELEASE" = "true" ]; then + LATEST_RELEASE_EPOCH="$(gh api --paginate "/repos/${REPOSITORY}/releases?per_page=100" | jq -sr ' + [.[][] | select((.draft | not) and (.prerelease | not)) | .tag_name + | capture("^v(?[0-9]{8})\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$").e | tonumber] | max // 0 + ')" + LATEST_FLAG="--latest" + if (( 10#$EPOCH < 10#$LATEST_RELEASE_EPOCH )); then LATEST_FLAG="--latest=false"; fi + git tag -a "v${NEW_VERSION}" -m "Release v${NEW_VERSION}" git push "https://x-access-token:${GH_TOKEN}@github.com/${REPOSITORY}.git" "refs/tags/v${NEW_VERSION}" - gh release create "v${NEW_VERSION}" \ - --repo "$REPOSITORY" \ - --target "$RELEASE_BRANCH" \ - --title "v${NEW_VERSION}" \ - --generate-notes + gh release create "v${NEW_VERSION}" --repo "$REPOSITORY" --verify-tag \ + --title "v${NEW_VERSION}" --generate-notes "$LATEST_FLAG" fi diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f12a984..2a91645 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -21,10 +21,15 @@ on: permissions: contents: read +concurrency: + group: pypi-release-publish + cancel-in-progress: false + jobs: pypi-publish: name: Upload release to PyPI runs-on: ubuntu-latest + timeout-minutes: 15 environment: name: release url: https://pypi.org/p/ucp-sdk @@ -49,91 +54,55 @@ jobs: RELEASE_TAG: ${{ github.ref_name }} run: | set -euo pipefail - if [[ ! "$RELEASE_TAG" =~ ^v([0-9]{4})([0-9]{2})([0-9]{2})\.([0-9]+)\.([0-9]+)$ ]]; then - echo "::error::Release tag '$RELEASE_TAG' must match vYYYYMMDD.MINOR.PATCH" + if [[ ! "$RELEASE_TAG" =~ ^v([0-9]{4})([0-9]{2})([0-9]{2})\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$ ]]; then + echo "::error::Release tag '$RELEASE_TAG' must match canonical vYYYYMMDD.MINOR.PATCH" + exit 1 + fi + DATE_STR="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]}" + if [ "$(date -u -d "$DATE_STR" +%Y-%m-%d 2>/dev/null || true)" != "$DATE_STR" ]; then + echo "::error::Invalid calendar date '${DATE_STR}' in release tag '$RELEASE_TAG'" exit 1 fi - YYYY="${BASH_REMATCH[1]}" - MM="${BASH_REMATCH[2]}" - DD="${BASH_REMATCH[3]}" - MINOR="${BASH_REMATCH[4]}" - PATCH="${BASH_REMATCH[5]}" - EPOCH="${YYYY}${MM}${DD}" + EPOCH="${DATE_STR//-/}" if (( 10#$EPOCH < 20260825 )); then - echo "::error::Release tag '$RELEASE_TAG' has epoch $EPOCH < 20260825 (CalVer applies forward-only from 2026-08-25)" + echo "::error::Release tag '$RELEASE_TAG' has epoch $EPOCH < 20260825" exit 1 fi - EXPECTED_VERSION="${EPOCH}.${MINOR}.${PATCH}" - EXPECTED_BRANCH="release/${YYYY}-${MM}-${DD}" + EXPECTED_VERSION="${EPOCH}.${BASH_REMATCH[4]}.${BASH_REMATCH[5]}" + EXPECTED_BRANCH="release/${DATE_STR}" - if [ ! -f ucp-lock.json ]; then - echo "::error::ucp-lock.json is missing" + PKG_VERSION="$(python3 -c 'import tomllib; print(tomllib.load(open("pyproject.toml", "rb"))["project"]["version"])')" + if [[ "$PKG_VERSION" == 0.0.0* || "$PKG_VERSION" != "$EXPECTED_VERSION" ]]; then + echo "::error::pyproject.toml version '$PKG_VERSION' does not match expected release version '$EXPECTED_VERSION'" exit 1 fi - UCP_SCHEMA_VERSION="$(EXPECTED_VERSION="$EXPECTED_VERSION" EXPECTED_BRANCH="$EXPECTED_BRANCH" python3 - <<'PY' - import json - import os - from pathlib import Path - import re - import sys - import tomllib - - expected_version = os.environ["EXPECTED_VERSION"] - expected_branch = os.environ["EXPECTED_BRANCH"] - - pyproject_data = tomllib.loads(Path("pyproject.toml").read_text(encoding="utf-8")) - pkg_version = pyproject_data.get("project", {}).get("version") - if not isinstance(pkg_version, str) or not pkg_version: - print("::error::Could not parse project.version from pyproject.toml", file=sys.stderr) - sys.exit(1) - if pkg_version.startswith("0.0.0"): - print(f"::error::Cannot publish draft version '{pkg_version}' to PyPI", file=sys.stderr) - sys.exit(1) - if pkg_version != expected_version: - print( - f"::error::pyproject.toml version '{pkg_version}' does not match tag version '{expected_version}'", - file=sys.stderr, - ) - sys.exit(1) - - lock = json.loads(Path("ucp-lock.json").read_text(encoding="utf-8")) - if lock.get("channel") != "release": - print( - f"::error::ucp-lock.json channel must be 'release', got {lock.get('channel')!r}", - file=sys.stderr, - ) - sys.exit(1) - if lock.get("ucp_ref") != expected_branch: - print( - f"::error::ucp-lock.json ucp_ref must be {expected_branch!r}, got {lock.get('ucp_ref')!r}", - file=sys.stderr, - ) - sys.exit(1) - ucp_sha = lock.get("ucp_sha", "") - if not re.fullmatch(r"[0-9a-f]{40}", str(ucp_sha)): - print( - f"::error::ucp-lock.json ucp_sha must be a 40-char lowercase hex SHA, got {ucp_sha!r}", - file=sys.stderr, - ) - sys.exit(1) - schema_version = str(lock.get("ucp_schema_version", "")).strip() - if not schema_version: - print("::error::ucp-lock.json is missing ucp_schema_version", file=sys.stderr) - sys.exit(1) - print(schema_version) - PY - )" + [ -f ucp-lock.json ] || { echo "::error::ucp-lock.json is missing"; exit 1; } + LOCK_FIELDS="$(jq -er --arg ref "$EXPECTED_BRANCH" ' + select(.channel == "release" and .ucp_ref == $ref) | + (.ucp_sha | select(type == "string" and test("^[0-9a-f]{40}$"))) as $sha | + (.ucp_schema_version | select(type == "string" and test("^[0-9]+\\.[0-9]+\\.[0-9]+$"))) as $ver | + "\($sha) \($ver)" + ' ucp-lock.json)" || { echo "::error::ucp-lock.json failed release provenance validation"; exit 1; } + read -r UCP_SHA UCP_SCHEMA_VERSION <<< "$LOCK_FIELDS" git fetch origin "+refs/heads/${EXPECTED_BRANCH}:refs/remotes/origin/${EXPECTED_BRANCH}" - if ! git merge-base --is-ancestor HEAD "origin/${EXPECTED_BRANCH}"; then - echo "::error::Release commit HEAD is not an ancestor of origin/${EXPECTED_BRANCH}" - exit 1 - fi - + git merge-base --is-ancestor HEAD "origin/${EXPECTED_BRANCH}" \ + || { echo "::error::Release commit HEAD is not an ancestor of origin/${EXPECTED_BRANCH}"; exit 1; } + + rm -rf "$RUNNER_TEMP/ucp-spec" + git clone --filter=blob:none --no-checkout \ + "${UCP_REMOTE_URL:-https://github.com/Universal-Commerce-Protocol/ucp.git}" "$RUNNER_TEMP/ucp-spec" + git -C "$RUNNER_TEMP/ucp-spec" show-ref --verify --quiet "refs/remotes/origin/${EXPECTED_BRANCH}" \ + || { echo "::error::Upstream branch ${EXPECTED_BRANCH} does not exist in ucp"; exit 1; } + git -C "$RUNNER_TEMP/ucp-spec" cat-file -e "${UCP_SHA}^{commit}" 2>/dev/null \ + || { echo "::error::ucp_sha '${UCP_SHA}' does not exist in ucp"; exit 1; } + git -C "$RUNNER_TEMP/ucp-spec" merge-base --is-ancestor "$UCP_SHA" "origin/${EXPECTED_BRANCH}" \ + || { echo "::error::ucp_sha '${UCP_SHA}' is not an ancestor of origin/${EXPECTED_BRANCH} in ucp"; exit 1; } + git -C "$RUNNER_TEMP/ucp-spec" checkout --detach "$UCP_SHA" echo "ucp_schema_version=${UCP_SCHEMA_VERSION}" >> "$GITHUB_OUTPUT" - - name: Install ucp-schema CLI + - name: Run unit tests, verify model drift, and build package env: UCP_SCHEMA_VERSION: ${{ steps.verify.outputs.ucp_schema_version }} run: | @@ -146,17 +115,17 @@ jobs: (cd "$tmp_dir" && sha256sum -c --ignore-missing SHA256SUMS) mkdir -p "$HOME/.local/bin" install -m 0755 "$tmp_dir/ucp-schema-linux-x86_64" "$HOME/.local/bin/ucp-schema" - echo "$HOME/.local/bin" >> "$GITHUB_PATH" + export PATH="$HOME/.local/bin:$PATH" - - name: Run unit tests and verify model drift - run: | - set -euo pipefail uv run python -m unittest discover -s tests -p "test_*.py" - ./generate_models.sh --from-lock - git diff --exit-code - - - name: Build package - run: rm -rf dist && uv build + ./generate_models.sh --from-lock --spec-dir "$RUNNER_TEMP/ucp-spec" + if [ -n "$(git status --porcelain -- src/ucp_sdk pyproject.toml ucp-lock.json)" ]; then + echo "::error::Committed models or release metadata differ from ucp-lock.json regeneration" + git status --short -- src/ucp_sdk pyproject.toml ucp-lock.json + git --no-pager diff -- src/ucp_sdk pyproject.toml ucp-lock.json + exit 1 + fi + rm -rf dist && uv build - name: Publish to PyPI - uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # release/v1 + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3