From 09b0af24bd72546865bea773c8f70325cc46eb62 Mon Sep 17 00:00:00 2001 From: Juan Manuel Castillo Pinto <33883620+jmmana@users.noreply.github.com> Date: Fri, 4 Dec 2026 01:56:09 -0500 Subject: [PATCH] Recover adoption snapshots from the published dashboard history --- .github/workflows/docs.yml | 15 +++++++++------ docs/adoption.md | 2 +- scripts/adoption_snapshot.py | 24 ++++++++++++++++------- tests/test_adoption_snapshot.py | 34 +++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index c1c5692..2522ac5 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -31,13 +31,16 @@ jobs: GH_TOKEN: ${{ github.token }} run: | previous="$RUNNER_TEMP/adoption-history.json" - artifact=$(gh api "repos/$GITHUB_REPOSITORY/actions/artifacts?name=adoption-history&per_page=30" --jq '[.artifacts[] | select(.expired == false and .workflow_run.head_branch == "main")] - | max_by(.created_at) | .id // empty') - if [ -n "$artifact" ]; then - gh api "repos/$GITHUB_REPOSITORY/actions/artifacts/$artifact/zip" > "$RUNNER_TEMP/adoption-history.zip" - unzip -p "$RUNNER_TEMP/adoption-history.zip" adoption-history.json > "$previous" + archive="$RUNNER_TEMP/adoption-history.zip" + artifact=$(gh api "repos/$GITHUB_REPOSITORY/actions/artifacts?name=adoption-history&per_page=30" \ + --jq '[.artifacts[] | select(.expired == false and .workflow_run.head_branch == "main")] + | max_by(.created_at) | .id // empty') || artifact="" + if [ -n "$artifact" ] \ + && gh api "repos/$GITHUB_REPOSITORY/actions/artifacts/$artifact/zip" > "$archive" \ + && unzip -p "$archive" adoption-history.json > "$previous.tmp"; then + mv "$previous.tmp" "$previous" fi - python scripts/adoption_snapshot.py --previous "$previous" + python scripts/adoption_snapshot.py --previous "$previous" --restore-published - name: Sum downloads across PyPI, npm and NuGet if: github.ref == 'refs/heads/main' && (github.event_name == 'push' || github.event_name == 'schedule') run: python scripts/total_downloads.py diff --git a/docs/adoption.md b/docs/adoption.md index 6292f81..7198033 100644 --- a/docs/adoption.md +++ b/docs/adoption.md @@ -21,7 +21,7 @@ Baseline **2026-11-29** (UTC). Waiting for the first snapshot on or after **2027 ## Snapshots -The scheduled Docs workflow keeps the published baseline and carries later snapshots forward. For captures on the same UTC date, the latest successful value is kept for each metric. npm values must also describe the same download window. +The scheduled Docs workflow keeps the published baseline and carries later snapshots forward. The public history is used if the previous CI artifact is unavailable. For captures on the same UTC date, the latest successful value is kept for each metric. npm values must also describe the same download window. | Date | Stars | Forks | External contributors | npm downloads | PyPI downloads | NuGet downloads | Public directory listings | Verified integrations | | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | diff --git a/scripts/adoption_snapshot.py b/scripts/adoption_snapshot.py index b6fcb0f..b248f80 100644 --- a/scripts/adoption_snapshot.py +++ b/scripts/adoption_snapshot.py @@ -11,6 +11,7 @@ REPO = "https://github.com/jmmana/sqlguardllm" API = "https://api.github.com/repos/jmmana/sqlguardllm" +PUBLISHED_HISTORY = "https://jmmana.github.io/sqlguardllm/assets/adoption-history.json" PYPI_STATS = "https://pypistats.org/api/packages/sqlguardllm/recent" NUGET_QUERY = "/query?q=packageid%3ASqlGuardLlm&prerelease=true" NUGET = "https://azuresearch-usnc.nuget.org" + NUGET_QUERY @@ -170,15 +171,18 @@ def record_snapshot(history, row): history[row["date"]] = merged -def load_history(previous): +def load_history(previous, *, restore_published=False): """Restore daily captures while keeping the published baseline authoritative.""" history = {BASELINE["date"]: BASELINE} - if previous is None or not previous.is_file(): + if previous and previous.is_file(): + rows = json.loads(previous.read_text(encoding="utf-8")) + elif restore_published: + rows = get_json(PUBLISHED_HISTORY) + else: return history - rows = json.loads(previous.read_text(encoding="utf-8")) - if not isinstance(rows, list): - raise ValueError("Adoption history must be a list of daily captures") + if not isinstance(rows, list) or (restore_published and not rows): + raise ValueError("Adoption history must contain a list of daily captures") for row in rows: if not isinstance(row, dict) or not isinstance(row.get("date"), str): raise ValueError("Adoption history contains a capture without a date") @@ -274,7 +278,8 @@ def render(history): "## Snapshots", "", "The scheduled Docs workflow keeps the published baseline and carries later " - "snapshots forward. For captures on the same UTC date, " + "snapshots forward. The public history is used if the previous CI artifact " + "is unavailable. For captures on the same UTC date, " "the latest successful value is kept for " "each metric. npm values must also describe the same download window.", "", @@ -332,11 +337,16 @@ def render(history): def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--previous", type=Path) + parser.add_argument( + "--restore-published", + action="store_true", + help="use the published history if the previous artifact is unavailable", + ) parser.add_argument( "--offline", action="store_true", help="Render without collecting live metrics" ) args = parser.parse_args() - history = load_history(args.previous) + history = load_history(args.previous, restore_published=args.restore_published) if not args.offline: row = collect(datetime.now(timezone.utc)) if row["date"] > BASELINE["date"]: diff --git a/tests/test_adoption_snapshot.py b/tests/test_adoption_snapshot.py index 519fbef..40a3e8a 100644 --- a/tests/test_adoption_snapshot.py +++ b/tests/test_adoption_snapshot.py @@ -164,3 +164,37 @@ def test_public_directory_sources_have_distribution_evidence() -> None: for name, url in script["LISTINGS"].items(): row = next(line for line in tracker.splitlines() if line.startswith(f"| {name} |")) assert f"]({url})" in row + + +def test_published_history_is_used_only_when_artifact_is_missing( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + script = runpy.run_path(str(SCRIPT)) + load_history = script["load_history"] + baseline = script["BASELINE"] + published = {**baseline, "date": "2026-12-04", "stars": 20} + calls = [] + + def read(url: str) -> list: + calls.append(url) + return [baseline, published] + + monkeypatch.setitem(load_history.__globals__, "get_json", read) + artifact = tmp_path / "adoption-history.json" + assert load_history(artifact, restore_published=True)[published["date"]] == published + assert calls == [script["PUBLISHED_HISTORY"]] + + newer = {**published, "date": "2026-12-05", "stars": 21} + artifact.write_text(json.dumps([baseline, newer]), encoding="utf-8") + assert load_history(artifact, restore_published=True)[newer["date"]] == newer + assert calls == [script["PUBLISHED_HISTORY"]] + + +def test_empty_published_history_fails_before_replacing_captures( + monkeypatch: pytest.MonkeyPatch, +) -> None: + load_history = runpy.run_path(str(SCRIPT))["load_history"] + monkeypatch.setitem(load_history.__globals__, "get_json", lambda _url: []) + + with pytest.raises(ValueError, match="list of daily captures"): + load_history(None, restore_published=True)