diff --git a/.github/scripts/connector_notes.py b/.github/scripts/connector_notes.py new file mode 100644 index 000000000..3a8954916 --- /dev/null +++ b/.github/scripts/connector_notes.py @@ -0,0 +1,182 @@ +# /// script +# requires-python = ">=3.11" +# dependencies = ["GitPython>=3.1"] +# +# [tool.ty.rules] +# unresolved-import = "ignore" +# /// +"""Print the release notes of timenet-connectors between two refs. + +The notes list the connectors that were added, changed or removed, and the changes to the shared +connector code (the "framework"). Core changes that only touched connector files on the way are +left out; the timenet notes list them. + +- A connector is a ``datasets///`` folder with a ``dataset.yaml`` card, the rule that + ``scripts/gen_dataset_docs.py`` uses. Connectors are matched by the card's ``dataset_id``, so a + moved folder that keeps its id is changed, and a new id is one removal and one addition. +- An added connector cites the PR that added it, a removed one the PR that removed it. A changed + connector cites its connector PRs, and is left out when it has none. +- The framework section lists the connector PRs outside ``datasets/``, breaking ones first. +- A connector PR is a ``feat``, a ``fix`` or a breaking change that did not touch + ``packages/timenet``. A core PR edits connectors on the way, and the timenet notes list it. + +Called by .github/workflows/release.yml. Run it the way the workflow does:: + + uv run --no-project .github/scripts/connector_notes.py v0.11.0 HEAD \ + --release timenet-connectors-v0.12.0 +""" + +import argparse +import re + +from git import Repo + + +PACKAGE = "packages/timenet-connectors" +DATASETS = f"{PACKAGE}/src/timenet_connectors/datasets" +PR_URL = "https://github.com/OpenTSLM/TimeNet/pull/" +# A squash-merged PR title: type(scope)!: description (#123). +SUBJECT = re.compile(r"^(?P\w+)(?:\((?P[^)]*)\))?(?P!)?: (?P.+?)(?: \(#(?P\d+)\))?$") +# The commits cliff.toml skips: release and development-version commits. +SKIP = re.compile(r"^chore(\(release\)|: start .* development)") + + +def repo() -> Repo: + """Open the repository around the current directory. + + Returns: + The repository. + """ + return Repo(search_parent_directories=True) + + +def commits(base: str, head: str, *paths: str) -> list[tuple[str, str]]: + """List the commits between ``base`` and ``head`` that touched ``paths``, oldest first. + + Returns: + ``(short hash, subject)`` pairs, without release and development-version commits. + """ + found = repo().iter_commits(f"{base}..{head}", paths=list(paths), reverse=True) + pairs = [(commit.hexsha[:9], str(commit.summary)) for commit in found] + return [(sha, subject) for sha, subject in pairs if not SKIP.match(subject)] + + +def notable(subject: str) -> bool: + """Return whether a commit is a feature, a fix or a breaking change.""" + match = SUBJECT.match(subject) + return bool(match) and (match["type"] in {"feat", "fix"} or bool(match["bang"])) + + +def link(sha: str, subject: str) -> str: + """Link the PR of a commit, or name the commit when it has no PR number. + + Returns: + A markdown link, or the short hash. + """ + number = re.search(r"\(#(\d+)\)$", subject) + return f"[#{number[1]}]({PR_URL}{number[1]})" if number else sha + + +def connectors(ref: str) -> dict[str, tuple[str, str]]: + """Map each dataset id at ``ref`` to its folder and display name. + + Returns: + ``{dataset_id: (folder, name)}``. + """ + found = {} + try: + datasets = repo().commit(ref).tree / DATASETS + except KeyError: + return found + for folder in (folder for org in datasets.trees for folder in org.trees): + try: + text = (folder / "dataset.yaml").data_stream.read().decode() + except KeyError: + continue + dataset_id = re.search(r"^dataset_id:\s*(\S+)", text, re.M) + name = re.search(r"^name:\s*(.+)$", text, re.M) + key = dataset_id[1] if dataset_id else "/".join(folder.path.split("/")[-2:]) + found[key] = (folder.path, name[1].strip() if name else key) + return found + + +def connector_sections(base: str, head: str, core: set[str]) -> tuple[dict[str, list[str]], set[str]]: + """List the connectors that were added, changed or removed. + + Returns: + The entries of each section, and the commits that added a connector. + """ + old, new = connectors(base), connectors(head) + sections: dict[str, list[str]] = {"Added connectors": [], "Changed connectors": [], "Removed connectors": []} + added: set[str] = set() + for dataset_id in sorted(old.keys() | new.keys()): + folders = sorted({entry[0] for entry in (old.get(dataset_id), new.get(dataset_id)) if entry}) + touched = commits(base, head, *folders) + if dataset_id not in old: + section, cited = "Added connectors", touched[:1] + added.update(sha for sha, _ in cited) + elif dataset_id not in new: + section, cited = "Removed connectors", touched[-1:] + else: + section, cited = "Changed connectors", [c for c in touched if c[0] not in core and notable(c[1])] + if cited: + name = (new.get(dataset_id) or old[dataset_id])[1] + links = ", ".join(dict.fromkeys(link(*c) for c in cited)) + sections[section].append(f"- {name} (`{dataset_id}`): {links}") + return sections, added + + +def framework_entries(base: str, head: str, skip: set[str]) -> list[str]: + """List the connector PRs outside ``datasets/``, breaking ones first. + + Returns: + The entries, without the commits in ``skip``. + """ + entries = [] + for sha, subject in commits(base, head, PACKAGE, f":(exclude){DATASETS}"): + match = SUBJECT.match(subject) + if match and sha not in skip and notable(subject): + breaking = "**Breaking:** " if match["bang"] else "" + scope = f"**{match['scope']}:** " if match["scope"] else "" + text = match["text"][:1].upper() + match["text"][1:] + entries.append((not match["bang"], f"- {breaking}{scope}{text} ({link(sha, subject)})")) + return [entry for _, entry in sorted(entries, key=lambda pair: pair[0])] + + +def notes(base: str, head: str) -> str: + """Render the notes as markdown. + + Returns: + The markdown, or an empty string when nothing in the package changed. + """ + core = {sha for sha, _ in commits(base, head, "packages/timenet")} + sections, added = connector_sections(base, head, core) + sections["Connector framework"] = framework_entries(base, head, core | added) + out = [] + for title, entries in sections.items(): + if entries: + out += [f"### {title}", "", *entries, ""] + return "\n".join(out) + + +def badge(release: str) -> str: + """Render the PyPI badge of a ``-vX.Y.Z`` tag, like the one cliff.toml writes. + + Returns: + The markdown badge and a blank line. + """ + package, version = release.rsplit("-v", 1) + label = package.replace("-", "--") + image = f"https://img.shields.io/badge/{label}-{version}-blue?logo=pypi&logoColor=white" + return f"[![{package} {version}]({image})](https://pypi.org/project/{package}/{version}/)\n\n" + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument("base", help="the last release tag") + parser.add_argument("head", help="the commit to release") + parser.add_argument( + "--release", help="the release tag, for example timenet-connectors-v0.12.0, to add a PyPI badge" + ) + args = parser.parse_args() + print((badge(args.release) if args.release else "") + notes(args.base, args.head), end="") diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 38c57d279..15377a5b3 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,5 +1,9 @@ name: Publish to PyPI +# Uploads the wheel and the sdist that release.yml built and attached to the GitHub release of a +# -vX.Y.Z tag. It does not build. release.yml starts the TestPyPI run, and a successful +# TestPyPI run starts the PyPI run of the same tag. + on: workflow_dispatch: inputs: @@ -20,8 +24,12 @@ on: - both - timenet - timenet-connectors + ref: + description: Release tag to publish, for example timenet-v0.12.0 + required: true + type: string -run-name: Publish ${{ inputs.package }} to ${{ inputs.target }} +run-name: Publish ${{ inputs.package }} ${{ inputs.ref }} to ${{ inputs.target }} env: BRANCH_NAME: ${{ github.head_ref || github.ref_name }} @@ -39,67 +47,80 @@ jobs: # tokens are stored as repo secrets. id-token: write contents: read + # Start the PyPI run after a TestPyPI run. + actions: write steps: - - name: Guard PyPI to main + - name: Guard to main run: | echo "Branch: $BRANCH_NAME" - if [ "${{ inputs.target }}" = "PyPI" ] && [ "$BRANCH_NAME" != "main" ]; then - echo "::error title=Invalid branch::PyPI releases must come from main." + if [ "$BRANCH_NAME" != "main" ]; then + echo "::error title=Invalid branch::Run publish.yml from main." exit 1 fi - name: Checkout uses: actions/checkout@v5 - - name: Set up uv - uses: astral-sh/setup-uv@v7 - - name: Build - # build_dists.py records the commit inside each package, so a build can name its code. + with: + ref: ${{ inputs.ref }} + fetch-depth: 0 + - name: Guard the release tag + env: + REF: ${{ inputs.ref }} run: | - if [ "${{ inputs.package }}" = "both" ]; then - uv run scripts/build_dists.py --all-packages - else - uv run scripts/build_dists.py --package "${{ inputs.package }}" + if ! [[ "$REF" =~ ^(timenet|timenet-connectors)-v[0-9]+\.[0-9]+\.[0-9]+$ ]] || ! git merge-base --is-ancestor HEAD origin/main; then + echo "::error title=Invalid ref::Publish only a -vX.Y.Z tag on main, not '$REF'." + exit 1 fi - - name: Smoke test built artifacts - # Import each built wheel and sdist in an isolated environment to catch missing files or - # broken metadata before uploading. Each artifact must also report this commit, clean, as - # the code it was built from. timenet-connectors depends on timenet, so hand it the - # wheel we just built when it is present (a `both` run); otherwise timenet resolves from the - # default index, which requires it to be published already. + - name: Set up uv + uses: astral-sh/setup-uv@v7 + - name: Download the release files + # Each published package needs its own tag on this commit, and the files its GitHub + # release holds: one wheel and one sdist at the tagged version, nothing else. + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + PACKAGE: ${{ inputs.package }} run: | - set -euo pipefail - shopt -s nullglob - timenet_whl=(dist/timenet-*.whl) - smoke() { - local module="$1" dist="$2"; shift 2 - local args=() artifact - for artifact in "$@"; do args+=(--with "$artifact"); done - echo "::group::import $module" - uv run --isolated --no-project "${args[@]}" \ - python -c "import $module; print('smoke ok:', $module.__name__)" - uv run --isolated --no-project "${args[@]}" python -c " - from timenet.manifest import GitSource - from timenet.provenance import PackageSnapshot - commit = PackageSnapshot.read('$dist').source - expected = GitSource(sha='$GITHUB_SHA', contains_local_changes=False) - assert commit == expected, f'$dist records {commit}, expected {expected}' - print('commit ok:', commit)" - echo "::endgroup::" - } - for artifact in dist/*; do - case "$artifact" in - dist/timenet-*.whl | dist/timenet-*.tar.gz) - smoke timenet timenet "$artifact" ;; - dist/timenet_connectors-*.whl | dist/timenet_connectors-*.tar.gz) - smoke timenet_connectors timenet-connectors ${timenet_whl[@]+"${timenet_whl[@]}"} "$artifact" ;; - esac + if [ "$PACKAGE" = both ]; then packages="timenet timenet-connectors"; else packages=$PACKAGE; fi + expected=() + for package in $packages; do + version=$(uv version --package "$package" --short) + tag="$package-v$version" + if [ "$(git rev-parse -q --verify "refs/tags/$tag^{commit}")" != "$(git rev-parse HEAD)" ]; then + echo "::error title=Not released::$package is at $version, but $tag does not point at this commit. Release it first." + exit 1 + fi + gh release download "$tag" --dir dist + expected+=("${package//-/_}-$version-py3-none-any.whl" "${package//-/_}-$version.tar.gz") done + if [ "$(ls dist | sort)" != "$(printf '%s\n' "${expected[@]}" | sort)" ]; then + echo "::error title=Unexpected files::The releases hold $(ls dist | xargs), expected ${expected[*]}." + exit 1 + fi + ls -l dist - name: Publish run: | if [ "${{ inputs.target }}" = "TestPyPI" ]; then - uv publish --publish-url https://test.pypi.org/legacy/ + uv publish --publish-url https://test.pypi.org/legacy/ dist/* else - uv publish + uv publish dist/* + fi + - name: Start the PyPI run + # A TestPyPI run starts the PyPI run of the same tag, which waits for approval. So PyPI + # can only be approved after TestPyPI took the same files. + if: inputs.target == 'TestPyPI' + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + PACKAGE: ${{ inputs.package }} + REF: ${{ inputs.ref }} + run: | + name="Publish $PACKAGE $REF to PyPI" + if gh run list --workflow publish.yml --event workflow_dispatch --limit 100 --json displayTitle \ + --jq '.[].displayTitle' | grep -qxF "$name"; then + echo "Skipping '$name': it was already started." + exit 0 fi + gh workflow run publish.yml --ref main -f target=PyPI -f package="$PACKAGE" -f ref="$REF" - name: Summary if: success() run: | diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..c81b142fa --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,384 @@ +name: Release + +# Releases timenet, timenet-connectors, or both. Each package has its own version and its own tag, +# -vX.Y.Z. The timenet notes list the commits that touched packages/timenet. The +# timenet-connectors notes list the connectors added, changed or removed, and the changes to the +# shared connector code (.github/scripts/connector_notes.py). +# 1. Run this workflow on main and pick the package. It opens a release PR that sets the version +# and shows the notes. +# 2. Merge the release PR. For every package whose version the PR changed, this builds and +# smoke-tests the wheel and the sdist, tags -vX.Y.Z, and creates a GitHub release +# with the two files attached. It starts a TestPyPI run of publish.yml, which waits for +# approval in the pypi environment, and opens a PR that moves the released packages to their +# next development version, X.(Y+1).0.dev0. +# 3. Approve the TestPyPI run. Once it has uploaded, it starts the PyPI run, which waits for +# approval too. Check TestPyPI, approve the PyPI run, then merge the development-version PR. +# Releasing both raises the timenet floor of timenet-connectors to the new timenet version. +# Releasing one package alone keeps the floor. The release PR warns when the other package has +# changes that the release leaves out. +# The version is fixed when the release PR opens. If main gets changes that need a bigger bump +# before you merge it, close the release PR and run this workflow again. +# Every job can be re-run: steps that already happened are skipped. +# Only admins and maintainers can start a release, merge the release PR, or re-run a job. If +# someone else merges the release PR, nothing is tagged until an admin or maintainer re-runs it. +# +# Needs a GitHub App with contents and pull-requests write access, installed on this repository, +# with its id in the RELEASE_APP_ID variable and its key in the RELEASE_APP_PRIVATE_KEY secret. +# PRs opened with GITHUB_TOKEN are blocked by the repository settings and would not run CI. + +on: + workflow_dispatch: + inputs: + package: + description: Package to release + required: true + default: both + type: choice + options: + - both + - timenet + - timenet-connectors + version: + description: Version to release, for example 0.12.0. Leave empty to compute it from the commits. + required: false + type: string + pull_request: + types: [closed] + branches: [main] + +permissions: + contents: read + +env: + GIT_CLIFF: git-cliff==2.14.2 + GH_REPO: ${{ github.repository }} + +jobs: + authorize: + if: >- + github.event_name == 'workflow_dispatch' + || (github.event.pull_request.merged == true + && github.event.pull_request.head.repo.full_name == github.repository + && startsWith(github.event.pull_request.head.ref, 'release/timenet')) + runs-on: ubuntu-latest + steps: + - name: Allow only admins and maintainers + env: + GH_TOKEN: ${{ github.token }} + # The user who started the run, merged the release PR, or re-ran a job. + ACTOR: ${{ github.triggering_actor }} + run: | + role=$(gh api "repos/$GITHUB_REPOSITORY/collaborators/$ACTOR/permission" --jq .role_name) + if [ "$role" != admin ] && [ "$role" != maintain ]; then + echo "::error title=Not allowed::$ACTOR has the $role role. Only admins and maintainers can release." + exit 1 + fi + + prepare: + needs: authorize + if: github.event_name == 'workflow_dispatch' + runs-on: ubuntu-latest + steps: + - name: Guard to main + if: github.ref != 'refs/heads/main' + run: | + echo "::error title=Invalid branch::Start a release from main." + exit 1 + - id: app-token + uses: actions/create-github-app-token@v2 + with: + app-id: ${{ vars.RELEASE_APP_ID }} + private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} + permission-contents: write + permission-pull-requests: write + - uses: actions/checkout@v5 + with: + fetch-depth: 0 + token: ${{ steps.app-token.outputs.token }} + - uses: astral-sh/setup-uv@v8.2.0 + - name: Set the versions and write the notes + id: versions + env: + PACKAGE: ${{ inputs.package }} + REQUESTED: ${{ inputs.version }} + run: | + # The legacy vX.Y.Z tags count as the last release of both packages. + cliff() { + local package=$1; shift + uvx "$GIT_CLIFF" --include-path "packages/$package/**" \ + --tag-pattern "^($package-)?v[0-9]+\.[0-9]+\.[0-9]+$" "$@" + } + if [ "$PACKAGE" = both ]; then packages="timenet timenet-connectors"; else packages=$PACKAGE; fi + titles=() tags=() + : > "$RUNNER_TEMP/notes.md" + for package in $packages; do + version=${REQUESTED:-$(cliff "$package" --bumped-version)} + version=${version##*v} + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "::error title=Invalid version::'$version' is not X.Y.Z." + exit 1 + fi + for tag in "$package-v$version" "v$version"; do + if git rev-parse -q --verify "refs/tags/$tag" >/dev/null; then + echo "::error title=Version exists::Tag $tag already exists. Is $package unchanged since then?" + exit 1 + fi + done + uv version --package "$package" "$version" --frozen + { + echo "## $package $version" + echo + if [ "$package" = timenet-connectors ]; then + last=$(git describe --tags --abbrev=0 --match "$package-v*" --match 'v*') + uv run --no-project .github/scripts/connector_notes.py "$last" HEAD --release "$package-v$version" + else + cliff "$package" --unreleased --tag "$package-v$version" --strip header + fi + echo + } >> "$RUNNER_TEMP/notes.md" + titles+=("$package $version") + tags+=("$package-v$version") + done + connectors=packages/timenet-connectors/pyproject.toml + if [ "$PACKAGE" = both ]; then + # Connectors needs the timenet release that ships with it. + timenet_version=$(uv version --package timenet --short) + sed -i -E "s/\"timenet\[build\]>=[^\"]*\"/\"timenet[build]>=$timenet_version\"/" "$connectors" + grep -q "\"timenet\[build\]>=$timenet_version\"" "$connectors" + else + # Warn when the other package has changes this release leaves out. Skip the commits + # cliff.toml skips too: release and development-version commits. + if [ "$PACKAGE" = timenet ]; then other=timenet-connectors; else other=timenet; fi + last=$(git describe --tags --abbrev=0 --match "$other-v*" --match 'v*') + unreleased=$(git log --format=%s "$last..HEAD" -- "packages/$other" \ + | grep -cvE '^chore(\(release\)|: start .* development)' || true) + if [ "$unreleased" -gt 0 ]; then + floor=$(grep -o '"timenet\[build\]>=[^"]*"' "$connectors") + { + echo "> [!WARNING]" + echo "> $other has $unreleased commits since $last that this release leaves out, and" + echo "> timenet-connectors keeps $floor. If those commits go with the $PACKAGE changes," + echo "> release both instead." + echo + } >> "$RUNNER_TEMP/notes.md" + fi + fi + uv lock + title="chore(release): $(IFS=,; echo "${titles[*]}" | sed 's/,/, /g')" + branch="release/$(IFS=-; echo "${tags[*]}")" + echo "title=$title" >> "$GITHUB_OUTPUT" + echo "branch=$branch" >> "$GITHUB_OUTPUT" + - name: Open the release PR + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + TITLE: ${{ steps.versions.outputs.title }} + BRANCH: ${{ steps.versions.outputs.branch }} + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git switch -c "$BRANCH" + git commit -am "$TITLE" + git push --force origin "$BRANCH" + { + echo "Merging this PR tags each package below, creates its GitHub release with these notes," + echo "and starts the TestPyPI publish run for approval. A successful TestPyPI run starts the" + echo "PyPI run. To change the notes, edit them in the GitHub release after it is created." + echo + echo "---" + echo + cat "$RUNNER_TEMP/notes.md" + } > "$RUNNER_TEMP/body.md" + if [ -n "$(gh pr list --head "$BRANCH" --state open --json number --jq '.[].number')" ]; then + gh pr edit "$BRANCH" --body-file "$RUNNER_TEMP/body.md" + else + gh pr create --base main --head "$BRANCH" --title "$TITLE" --body-file "$RUNNER_TEMP/body.md" + fi + + release: + needs: authorize + if: >- + github.event_name == 'pull_request' + && github.event.pull_request.merged == true + && github.event.pull_request.head.repo.full_name == github.repository + && startsWith(github.event.pull_request.head.ref, 'release/timenet') + runs-on: ubuntu-latest + permissions: + contents: write + actions: write + outputs: + released: ${{ steps.released.outputs.released }} + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ github.event.pull_request.merge_commit_sha }} + fetch-depth: 0 + - uses: astral-sh/setup-uv@v8.2.0 + - name: Find the packages this PR released + id: released + run: | + released=() + for package in timenet timenet-connectors; do + version=$(uv version --package "$package" --short) + before=$(git show "HEAD^:packages/$package/pyproject.toml" | sed -n -E 's/^version = "(.*)"$/\1/p') + if [ "$version" = "$before" ]; then continue; fi + if ! [[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echo "::error title=Invalid version::$package is at $version, not X.Y.Z." + exit 1 + fi + released+=("$package=$version") + done + if [ ${#released[@]} -eq 0 ]; then + echo "::error title=Nothing to release::This PR changed no package version." + exit 1 + fi + echo "released=${released[*]}" >> "$GITHUB_OUTPUT" + - name: Build + # build_dists.py records the commit inside each package, so a build can name its code. + env: + RELEASED: ${{ steps.released.outputs.released }} + run: | + for entry in $RELEASED; do + uv run scripts/build_dists.py --package "${entry%=*}" + done + - name: Smoke test built artifacts + # Import each built wheel and sdist in an isolated environment to catch missing files or + # broken metadata before uploading. Each artifact must also report this commit, clean, as + # the code it was built from. timenet-connectors depends on timenet, so hand it the + # wheel we just built when it is present (a `both` run); otherwise timenet resolves from the + # default index, which requires it to be published already. + run: | + set -euo pipefail + shopt -s nullglob + timenet_whl=(dist/timenet-*.whl) + # The merge commit that the tags point at. + expected_sha=$(git rev-parse HEAD) + smoke() { + local module="$1" dist="$2"; shift 2 + local args=() artifact + for artifact in "$@"; do args+=(--with "$artifact"); done + echo "::group::import $module" + uv run --isolated --no-project "${args[@]}" \ + python -c "import $module; print('smoke ok:', $module.__name__)" + uv run --isolated --no-project "${args[@]}" python -c " + from timenet.manifest import GitSource + from timenet.provenance import PackageSnapshot + commit = PackageSnapshot.read('$dist').source + expected = GitSource(sha='$expected_sha', contains_local_changes=False) + assert commit == expected, f'$dist records {commit}, expected {expected}' + print('commit ok:', commit)" + echo "::endgroup::" + } + for artifact in dist/*; do + case "$artifact" in + dist/timenet-*.whl | dist/timenet-*.tar.gz) + smoke timenet timenet "$artifact" ;; + dist/timenet_connectors-*.whl | dist/timenet_connectors-*.tar.gz) + smoke timenet_connectors timenet-connectors ${timenet_whl[@]+"${timenet_whl[@]}"} "$artifact" ;; + esac + done + - name: Tag and create the GitHub releases + env: + GH_TOKEN: ${{ github.token }} + RELEASED: ${{ steps.released.outputs.released }} + run: | + cliff() { + local package=$1; shift + uvx "$GIT_CLIFF" --include-path "packages/$package/**" \ + --tag-pattern "^($package-)?v[0-9]+\.[0-9]+\.[0-9]+$" "$@" + } + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + for entry in $RELEASED; do + package=${entry%=*} version=${entry#*=} + tag="$package-v$version" + if git rev-parse -q --verify "refs/tags/$tag" >/dev/null; then + # A re-run: the tag is already pushed. It must point at this release. + if [ "$(git rev-parse "$tag^{commit}")" != "$(git rev-parse HEAD)" ]; then + echo "::error title=Tag mismatch::$tag exists on another commit." + exit 1 + fi + else + git tag -a "$tag" -m "$package $version" + git push origin "$tag" + fi + if ! gh release view "$tag" >/dev/null 2>&1; then + if [ "$package" = timenet-connectors ]; then + # The new tag is on HEAD, so the last release is the newest tag before it. + last=$(git describe --tags --abbrev=0 --match "$package-v*" --match 'v*' HEAD^) + uv run --no-project .github/scripts/connector_notes.py "$last" HEAD --release "$tag" > "$RUNNER_TEMP/notes.md" + else + cliff "$package" --latest --strip header --output "$RUNNER_TEMP/notes.md" + fi + gh release create "$tag" --verify-tag --title "$package $version" --notes-file "$RUNNER_TEMP/notes.md" + fi + # Attach the wheel and the sdist. A re-run uploads only the files that are missing, so + # the files a publish run may already have taken stay as they are. + attached=$(gh release view "$tag" --json assets --jq '.assets[].name') + for file in "dist/${package//-/_}-$version"-*.whl "dist/${package//-/_}-$version".tar.gz; do + if ! grep -qxF "$(basename "$file")" <<< "$attached"; then + gh release upload "$tag" "$file" + fi + done + done + - name: Start the TestPyPI publish run + env: + GH_TOKEN: ${{ github.token }} + RELEASED: ${{ steps.released.outputs.released }} + run: | + read -r -a released <<< "$RELEASED" + first=${released[0]} + package=${first%=*} + if [ ${#released[@]} -eq 2 ]; then package=both; fi + # Both tags of a release point at the same commit, so either one builds both packages. + ref="${first%=*}-v${first#*=}" + # A re-run: skip the publish run if this job already started it. Its name comes from + # publish.yml's run-name. A successful TestPyPI run starts the PyPI run. + name="Publish $package $ref to TestPyPI" + if gh run list --workflow publish.yml --event workflow_dispatch --limit 100 --json displayTitle \ + --jq '.[].displayTitle' | grep -qxF "$name"; then + echo "Skipping '$name': it was already started." + exit 0 + fi + gh workflow run publish.yml --ref main -f target=TestPyPI -f package="$package" -f ref="$ref" + + next-development-version: + needs: release + runs-on: ubuntu-latest + steps: + - id: app-token + uses: actions/create-github-app-token@v2 + with: + app-id: ${{ vars.RELEASE_APP_ID }} + private-key: ${{ secrets.RELEASE_APP_PRIVATE_KEY }} + permission-contents: write + permission-pull-requests: write + - uses: actions/checkout@v5 + with: + ref: ${{ github.event.pull_request.merge_commit_sha }} + token: ${{ steps.app-token.outputs.token }} + - uses: astral-sh/setup-uv@v8.2.0 + - name: Open the development version PR + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + RELEASED: ${{ needs.release.outputs.released }} + HEAD_REF: ${{ github.event.pull_request.head.ref }} + run: | + names=() + for entry in $RELEASED; do + package=${entry%=*} + uv version --package "$package" --bump minor --bump dev=0 --frozen + next=$(uv version --package "$package" --short) + names+=("$package ${next%.dev0}") + done + uv lock + title="chore: start $(IFS=,; echo "${names[*]}" | sed 's/,/ and /g') development" + branch="chore/start-development-after-${HEAD_REF#release/}" + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git switch -c "$branch" + git commit -am "$title" + git push --force origin "$branch" + if [ -z "$(gh pr list --head "$branch" --state open --json number --jq '.[].number')" ]; then + gh pr create --base main --head "$branch" --title "$title" \ + --body "Moves the released packages to their next development version ($RELEASED released), so builds from main are not mistaken for a release." + fi diff --git a/cliff.toml b/cliff.toml new file mode 100644 index 000000000..eb357ff1f --- /dev/null +++ b/cliff.toml @@ -0,0 +1,61 @@ +# Release notes for TimeNet, generated from the Conventional Commit subjects that squash merges +# leave on main. Used by .github/workflows/release.yml, once per package. Preview the next timenet +# release locally with (use timenet-connectors for the other package): +# uvx git-cliff==2.14.2 --include-path 'packages/timenet/**' \ +# --tag-pattern '^(timenet-)?v[0-9]+\.[0-9]+\.[0-9]+$' --unreleased --bump --strip header + +[changelog] +header = "" +body = """ +{%- macro entry(commit) -%} +- {% if commit.breaking %}**Breaking:** {% endif %}\ +{% if commit.scope %}**{{ commit.scope }}:** {% endif %}\ +{{ commit.message | split(pat="\n") | first | trim | upper_first }} +{%- endmacro -%} +{% if version and version is containing("-v") %}{% set package = version | split(pat="-v") | first %}{% set v = version | split(pat="-v") | last %} +[![{{ package }} {{ v }}](https://img.shields.io/badge/{{ package | replace(from="-", to="--") }}-{{ v }}-blue?logo=pypi&logoColor=white)](https://pypi.org/project/{{ package }}/{{ v }}/) +{% endif %} +{% for group, commits in commits | group_by(attribute="group") %} +### {{ group | striptags | trim }} +{% for commit in commits | filter(attribute="breaking", value=true) %} +{{ self::entry(commit=commit) }} +{%- endfor %} +{%- for commit in commits %}{% if not commit.breaking %} +{{ self::entry(commit=commit) }} +{%- endif %}{% endfor %} +{% endfor %} +""" +footer = "" +trim = true + +[git] +conventional_commits = true +filter_unconventional = false +split_commits = false +# Only release tags count. This skips the registry-dev/... and backup/... tags. release.yml narrows +# it to one package with --tag-pattern; the legacy vX.Y.Z tags count for both packages. +tag_pattern = "^((timenet|timenet-connectors)-)?v[0-9]+\\.[0-9]+\\.[0-9]+$" +topo_order = false +sort_commits = "newest" +protect_breaking_commits = true +filter_commits = false +# Link PR numbers explicitly, so they also work outside this repository's GitHub pages. +commit_preprocessors = [ + { pattern = '\(#([0-9]+)\)', replace = "([#${1}](https://github.com/OpenTSLM/TimeNet/pull/${1}))" }, +] +commit_parsers = [ + { message = "^chore\\(release\\)", skip = true }, + { message = "^chore: start .* development", skip = true }, + { message = "^feat[(!:]", group = "Features" }, + { message = "^fix[(!:]", group = "Bug fixes" }, + { message = "^perf[(!:]", group = "Performance" }, + { message = "^refactor[(!:]", group = "Refactoring" }, + { message = "^docs[(!:]", group = "Documentation" }, + { message = "^(build|ci|chore|test|style)[(!:]", group = "Maintenance" }, + { message = ".*", group = "Other" }, +] + +[bump] +# Pre-1.0: a breaking change bumps the minor version, a feature bumps the minor version too. +features_always_bump_minor = true +breaking_always_bump_major = false