Skip to content

Commit d5fd771

Browse files
cliffhallolaservoclaude
authored
feat: add the Prepare Python Release dispatch for CalVer stamping
Stamps today's CalVer date onto each Python server with a shipped change since its last version bump, refreshes its uv.lock, and opens a PR against the branch it was dispatched on. The stamping logic is scripts/prepare-python-release.mjs, under the same script tests as the rest of the root tooling. Ported from #4604 (scripts/prepare_python_release.py), with the py.typed marker counted as a shipped file and test-only changes not. Co-authored-by: olaservo <olahungerford@gmail.com> Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent cbb9d57 commit d5fd771

3 files changed

Lines changed: 511 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# Opens the PR that stamps today's CalVer date (e.g. 2026.8.1) onto each Python
2+
# server that changed since its last version bump (#4472). Dispatched by a
3+
# maintainer when preparing a release; the logic, and why Python stays on
4+
# CalVer, is in `scripts/prepare-python-release.mjs`.
5+
#
6+
# It is the Python counterpart of the changesets "Version Packages" PR: the
7+
# version changes in a reviewed PR on the develop branch, and nothing is
8+
# published until a maintainer publishes a GitHub Release (`release.yml`).
9+
#
10+
# The PR targets the branch the workflow was dispatched on, which is `v2/main`
11+
# in the normal flow.
12+
13+
name: Prepare Python Release
14+
15+
on:
16+
workflow_dispatch:
17+
18+
jobs:
19+
prepare:
20+
timeout-minutes: 10
21+
if: github.repository_owner == 'modelcontextprotocol'
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: write
25+
pull-requests: write
26+
steps:
27+
# Full history: the script finds each package's last version bump with
28+
# `git log`, and a shallow clone would make every package look new.
29+
- uses: actions/checkout@v6
30+
with:
31+
fetch-depth: 0
32+
33+
- uses: actions/setup-node@v7
34+
with:
35+
node-version: 22
36+
37+
# The script runs `uv lock` in each package it stamps.
38+
- name: Install uv
39+
uses: astral-sh/setup-uv@v7
40+
41+
- name: Stamp CalVer versions
42+
run: node scripts/prepare-python-release.mjs > "$RUNNER_TEMP/stamped.txt"
43+
44+
- name: Check for changes
45+
id: changes
46+
run: |
47+
if git diff --quiet; then
48+
echo "No Python package needs a version stamp"
49+
echo "changed=false" >> "$GITHUB_OUTPUT"
50+
else
51+
echo "changed=true" >> "$GITHUB_OUTPUT"
52+
fi
53+
54+
- name: Create pull request
55+
if: steps.changes.outputs.changed == 'true'
56+
env:
57+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
58+
BASE: ${{ github.ref_name }}
59+
run: |
60+
DATE=$(date -u +%Y.%-m.%-d)
61+
BRANCH="release/python-$DATE"
62+
git config user.name "github-actions[bot]"
63+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
64+
git checkout -b "$BRANCH"
65+
git add -u
66+
git commit -m "chore: stamp Python CalVer versions for $DATE"
67+
git push --force origin "$BRANCH"
68+
{
69+
echo "Stamps today's CalVer date onto the Python packages that changed since their last version bump:"
70+
echo
71+
sed 's/^/- /' "$RUNNER_TEMP/stamped.txt"
72+
echo
73+
echo "Generated by the **Prepare Python Release** workflow. Nothing is published until a maintainer publishes a GitHub Release."
74+
echo
75+
echo "> [!NOTE]"
76+
echo "> A PR opened with the workflow token does not trigger CI. Close and reopen this PR to run it."
77+
} > "$RUNNER_TEMP/pr-body.md"
78+
if [ "$(gh pr list --head "$BRANCH" --state open --json number --jq length)" != "0" ]; then
79+
echo "A PR for $BRANCH is already open; its branch was updated"
80+
else
81+
gh pr create \
82+
--base "$BASE" \
83+
--head "$BRANCH" \
84+
--label v2 \
85+
--title "chore: stamp Python CalVer versions for $DATE" \
86+
--body-file "$RUNNER_TEMP/pr-body.md"
87+
fi

‎scripts/prepare-python-release.mjs‎

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#!/usr/bin/env node
2+
// Stamp today's CalVer date onto each Python server that changed since its
3+
// last version bump (#4472).
4+
//
5+
// The Python servers stay on CalVer (`2026.8.1`) while the TypeScript ones move
6+
// to semver under changesets: pip and uv have no dist-tags and always install
7+
// the numerically highest version, so the `2026.x` line on PyPI has to keep
8+
// climbing. What changes is WHEN the version is written. The old pipeline
9+
// (`scripts/release.py`) stamped it inside the release run and pushed the
10+
// result as a tag only, so `v2/main` never held the version that shipped. Now
11+
// the version in `pyproject.toml` is the source of truth, it changes only in a
12+
// reviewed PR, and `release.yml` publishes whatever version it finds that the
13+
// registry does not have yet. This script is what writes that PR's content;
14+
// `.github/workflows/prepare-python-release.yml` runs it and opens the PR.
15+
//
16+
// For each `src/*/pyproject.toml`:
17+
//
18+
// 1. Find the last commit that changed its `version` line.
19+
// 2. If a file that ships in the package changed since then, set the version
20+
// to today's date and refresh `uv.lock`, which records the project's own
21+
// version and is installed `--locked` in CI.
22+
//
23+
// "Ships in the package" is read off the path: Python sources, the `py.typed`
24+
// marker, Markdown (the README is the sdist's long description) and
25+
// `pyproject.toml` itself (a dependency change). A test-only change does not
26+
// count: nothing a user installs is different. Over-stamping would be harmless
27+
// anyway, since the registry guard in `release.yml` decides what publishes;
28+
// under-stamping is the failure that matters, because a changed package whose
29+
// version did not move is silently not released.
30+
//
31+
// It is JavaScript rather than the Python it replaces so that it sits under the
32+
// same format, lint and `node --test` checks as every other script here, with
33+
// no second toolchain for the gate to cover. Only `uv lock` needs `uv`.
34+
//
35+
// stdout carries one `name: old -> new` line per stamped package, which the
36+
// workflow uses as the PR body; everything else goes to stderr.
37+
38+
import { spawnSync } from "node:child_process";
39+
import { existsSync, readdirSync, readFileSync, writeFileSync } from "node:fs";
40+
import path from "node:path";
41+
import { fileURLToPath, pathToFileURL } from "node:url";
42+
43+
const repoRoot = path.resolve(
44+
path.dirname(fileURLToPath(import.meta.url)),
45+
"..",
46+
);
47+
48+
/**
49+
* The CalVer version for a date: `YEAR.MONTH.DAY`, no zero padding, in UTC so
50+
* the result does not depend on the runner's timezone.
51+
*
52+
* @param {Date} date
53+
* @returns {string}
54+
*/
55+
export function calver(date) {
56+
return `${date.getUTCFullYear()}.${date.getUTCMonth() + 1}.${date.getUTCDate()}`;
57+
}
58+
59+
/**
60+
* Does a change to this path change what the package ships?
61+
*
62+
* @param {string} file a path relative to the package directory, POSIX-style
63+
* @returns {boolean}
64+
*/
65+
export function shipsInPackage(file) {
66+
const parts = file.split("/");
67+
if (parts.some((p) => p === "tests" || p === "test")) return false;
68+
const name = parts.at(-1) ?? "";
69+
return name === "pyproject.toml" || /\.(?:py|md|typed)$/.test(name);
70+
}
71+
72+
/**
73+
* The `[project]` table's `name` and `version`, read without a TOML parser:
74+
* both are plain one-line strings in every server here, and a manifest where
75+
* they are not fails loudly rather than being guessed at.
76+
*
77+
* @param {string} toml the text of a pyproject.toml
78+
* @returns {{ name: string, version: string }}
79+
*/
80+
export function readProject(toml) {
81+
const table = projectTable(toml);
82+
const field = (key) => {
83+
const m = new RegExp(`^${key}\\s*=\\s*"([^"]*)"\\s*(?:#.*)?$`, "m").exec(
84+
table.text,
85+
);
86+
if (!m)
87+
throw new Error(`pyproject.toml: no \`${key} = "…"\` line in [project]`);
88+
return m[1];
89+
};
90+
return { name: field("name"), version: field("version") };
91+
}
92+
93+
/**
94+
* `toml` with the `[project]` version replaced, everything else byte-identical.
95+
*
96+
* @param {string} toml the text of a pyproject.toml
97+
* @param {string} version
98+
* @returns {string}
99+
*/
100+
export function stampVersion(toml, version) {
101+
const table = projectTable(toml);
102+
const line = /^version\s*=\s*"[^"]*"/m;
103+
if (!line.test(table.text))
104+
throw new Error('pyproject.toml: no `version = "…"` line in [project]');
105+
return (
106+
toml.slice(0, table.start) +
107+
table.text.replace(line, `version = "${version}"`) +
108+
toml.slice(table.end)
109+
);
110+
}
111+
112+
/** The span of the `[project]` table: from its header to the next table. */
113+
function projectTable(toml) {
114+
const header = /^\[project\]\s*$/m.exec(toml);
115+
if (!header) throw new Error("pyproject.toml: no [project] table");
116+
const start = header.index + header[0].length;
117+
const next = /^\[/m.exec(toml.slice(start));
118+
const end = next ? start + next.index : toml.length;
119+
return { start, end, text: toml.slice(start, end) };
120+
}
121+
122+
function run(command, args, cwd) {
123+
const res = spawnSync(command, args, { cwd, encoding: "utf8" });
124+
if (res.error) throw res.error;
125+
if (res.status !== 0)
126+
throw new Error(
127+
`${command} ${args.join(" ")} exited ${res.status} in ${cwd}: ${res.stderr.trim()}`,
128+
);
129+
return res.stdout.trim();
130+
}
131+
132+
/** The real git and uv; `main` takes a replacement as a test seam. */
133+
export const TOOLS = {
134+
/** The last commit that changed the version line, or "" when there is none. */
135+
lastVersionBump: (dir) =>
136+
run(
137+
"git",
138+
[
139+
"log",
140+
"-1",
141+
"--format=%H",
142+
"-G",
143+
"^version[[:space:]]*=",
144+
"--",
145+
"pyproject.toml",
146+
],
147+
dir,
148+
),
149+
/** Paths under `dir` that differ from `commit`, relative to `dir`. */
150+
changedSince: (dir, commit) =>
151+
run("git", ["diff", "--name-only", "--relative", commit, "--", "."], dir)
152+
.split("\n")
153+
.filter(Boolean),
154+
/** Bring `uv.lock` in line with the stamped pyproject.toml. */
155+
lock: (dir) => {
156+
run("uv", ["lock"], dir);
157+
},
158+
};
159+
160+
/**
161+
* @param {object} [options]
162+
* @param {string} [options.root] the repository root
163+
* @param {Date} [options.today]
164+
* @param {typeof TOOLS} [options.tools]
165+
* @returns {number} the exit code
166+
*/
167+
export function main({
168+
root = repoRoot,
169+
today = new Date(),
170+
tools = TOOLS,
171+
} = {}) {
172+
const src = path.join(root, "src");
173+
const packages = readdirSync(src, { withFileTypes: true })
174+
.filter((e) => e.isDirectory())
175+
.map((e) => path.join(src, e.name))
176+
.filter((dir) => existsSync(path.join(dir, "pyproject.toml")))
177+
.sort();
178+
// A moved `src/` would otherwise read as "nothing to stamp" and the release
179+
// would ship no Python package without anyone being told.
180+
if (packages.length === 0) {
181+
console.error(`prepare-python-release: no pyproject.toml under ${src}`);
182+
return 1;
183+
}
184+
185+
const version = calver(today);
186+
for (const dir of packages) {
187+
const manifest = path.join(dir, "pyproject.toml");
188+
const toml = readFileSync(manifest, "utf8");
189+
const { name, version: current } = readProject(toml);
190+
191+
if (current === version) {
192+
console.error(`${name}: already at ${version}, skipping`);
193+
continue;
194+
}
195+
// No bump commit at all means the history is shallow or the package is
196+
// new. Stamp it: the registry guard makes a needless stamp harmless.
197+
const since = tools.lastVersionBump(dir);
198+
if (since && !tools.changedSince(dir, since).some(shipsInPackage)) {
199+
console.error(`${name}: no shipped change since ${current}, skipping`);
200+
continue;
201+
}
202+
203+
writeFileSync(manifest, stampVersion(toml, version));
204+
tools.lock(dir);
205+
console.error(`${name}: ${current} -> ${version}`);
206+
console.log(`${name}: ${current} -> ${version}`);
207+
}
208+
return 0;
209+
}
210+
211+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
212+
process.exit(main());

0 commit comments

Comments
 (0)