From e0299bf77b940c233fc5043a826e59ebb22e5f0d Mon Sep 17 00:00:00 2001 From: Matthijs Iriks Date: Fri, 4 Dec 2026 03:18:21 +0100 Subject: [PATCH] Packagist step submits the package when Packagist does not know it --- .gitea/scripts/packagist.py | 52 ++++++++++++++++++++++------------ .gitea/scripts/test_scripts.py | 42 +++++++++++++++++++++++++++ CHANGELOG.md | 5 ++++ README.md | 3 +- TOKENS.md | 1 + 5 files changed, 84 insertions(+), 19 deletions(-) diff --git a/.gitea/scripts/packagist.py b/.gitea/scripts/packagist.py index 8c43c75..89a0b0e 100755 --- a/.gitea/scripts/packagist.py +++ b/.gitea/scripts/packagist.py @@ -1,9 +1,9 @@ -#!/usr/bin/env python3 -"""Tells Packagist that the package has a new version. +"""Tells Packagist that the package has a new version, and submits the package when it is not there yet. Packagist reads the tags of the repository; this asks it to do that now, instead of waiting for its -own poll. The package must have been submitted on packagist.org once (by hand); until then, and -while PACKAGIST_USERNAME and PACKAGIST_TOKEN are not set, this only says so and succeeds. +own poll. The first time Packagist answers 404 (it does not know the package): then the package is +submitted through the create-package API, and Packagist reads every tag that is already there. +While PACKAGIST_USERNAME and PACKAGIST_TOKEN are not set, this only says so and succeeds. Environment: PACKAGIST_USERNAME, PACKAGIST_TOKEN (the API token of the Packagist account, a secret of the repository), PACKAGE_URL (the public address of the repository). @@ -22,27 +22,43 @@ TOKEN = os.environ.get("PACKAGIST_TOKEN", "").strip() PACKAGE_URL = os.environ.get("PACKAGE_URL", "").strip() +def call(endpoint, user, token, url): + """POST to the API of Packagist; returns the HTTP status. Raises HTTPError with the answer on a refusal.""" + query = urllib.parse.urlencode({"username": user, "apiToken": token}) + request = urllib.request.Request( + f"https://packagist.org/api/{endpoint}?{query}", + data=json.dumps({"repository": {"url": url}}).encode(), + method="POST", + headers={"Content-Type": "application/json"}, + ) + with urllib.request.urlopen(request, timeout=30) as response: + return response.status + + +def notify(user, token, url): + """Tells Packagist about the package; submits it first when Packagist does not know it. Returns what happened.""" + try: + return f"Packagist answered {call('update-package', user, token, url)}: it will read the new tag." + except urllib.error.HTTPError as e: + # Never echo the request (it holds the token); the answer of Packagist says enough. + if e.code != 404: + sys.exit(f"Packagist refused: {e.code} {e.read().decode()[:300]}") + + try: + return f"Packagist did not know the package: submitted it (answer {call('create-package', user, token, url)}). It reads every tag that is there." + except urllib.error.HTTPError as e: + sys.exit(f"Packagist did not know the package, and refused to submit it: {e.code} {e.read().decode()[:300]}") + + def main(): if not (USER and TOKEN): print("PACKAGIST_USERNAME / PACKAGIST_TOKEN are not set: Packagist was not told. " - "Set both as secrets of the repository once the package is submitted on packagist.org.") + "Set both as secrets of the repository.") return if not PACKAGE_URL.startswith("https://"): sys.exit("PACKAGE_URL must be the public https address of the repository.") - query = urllib.parse.urlencode({"username": USER, "apiToken": TOKEN}) - request = urllib.request.Request( - f"https://packagist.org/api/update-package?{query}", - data=json.dumps({"repository": {"url": PACKAGE_URL}}).encode(), - method="POST", - headers={"Content-Type": "application/json"}, - ) - try: - with urllib.request.urlopen(request, timeout=30) as response: - print(f"Packagist answered {response.status}: it will read the new tag.") - except urllib.error.HTTPError as e: - # Never echo the request (it holds the token); the answer of Packagist says enough. - sys.exit(f"Packagist refused: {e.code} {e.read().decode()[:300]}") + print(notify(USER, TOKEN, PACKAGE_URL)) if __name__ == "__main__": diff --git a/.gitea/scripts/test_scripts.py b/.gitea/scripts/test_scripts.py index 0d14c0d..c5f51b7 100644 --- a/.gitea/scripts/test_scripts.py +++ b/.gitea/scripts/test_scripts.py @@ -9,6 +9,7 @@ from datetime import datetime, timezone sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import check_flow # noqa: E402 +import packagist # noqa: E402 import prune_branches # noqa: E402 import update_dependencies # noqa: E402 import semver # noqa: E402 @@ -166,5 +167,46 @@ class DependencyUpdates(unittest.TestCase): self.assertEqual(composer["require-dev"]["c/c"], "^3.0||^4.0") +class PackagistNotice(unittest.TestCase): + def run_notice(self, answers): + """Runs notify() with fake answers per endpoint (a status number, or an HTTP error code as a string).""" + import io + import urllib.error + + calls = [] + + def fake(endpoint, user, token, url): + calls.append(endpoint) + answer = answers[endpoint] + if isinstance(answer, str): + raise urllib.error.HTTPError("x", int(answer), "refused", {}, io.BytesIO(b"nope")) + return answer + + original = packagist.call + packagist.call = fake + try: + return packagist.notify("u", "t", "https://example.org/r"), calls + finally: + packagist.call = original + + def test_a_known_package_is_only_updated(self): + message, calls = self.run_notice({"update-package": 202}) + self.assertEqual(calls, ["update-package"]) + self.assertIn("202", message) + + def test_an_unknown_package_is_submitted(self): + message, calls = self.run_notice({"update-package": "404", "create-package": 202}) + self.assertEqual(calls, ["update-package", "create-package"]) + self.assertIn("submitted", message) + + def test_another_refusal_is_not_submitted(self): + with self.assertRaises(SystemExit): + self.run_notice({"update-package": "403", "create-package": 202}) + + def test_a_refused_submission_fails(self): + with self.assertRaises(SystemExit): + self.run_notice({"update-package": "404", "create-package": "400"}) + + if __name__ == "__main__": unittest.main() diff --git a/CHANGELOG.md b/CHANGELOG.md index efbecb2..b0099e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Every release has a section here, named after its version (for example `v0.1.0 - heading). The release pull request to `production` is stopped by PR CI when its section is missing, and the text of the section is the text of the release in Gitea. +## v0.3.0 - 2026-12-04 + +- The Packagist step submits the package itself when Packagist does not know it yet (the update call answered 404), instead + of failing until someone submits it by hand. + ## v0.2.0 - 2026-12-04 - CI: the composer cache and three tries for the install in every job, so a hiccup of a download no longer breaks a run. diff --git a/README.md b/README.md index 78b96bd..8a43692 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,8 @@ Nobody opens a pull request to `develop`, and `production` and the release branc | Every night | *Prune branches*: removes `feature/*` and `fix/*` branches whose last commit is over 30 days old and that have no open pull request (a branch of a pull request can be restored from it in Gitea) | Secrets of the repository for this: `DEPLOY_TOKEN` (the organisation has it), and `PACKAGIST_USERNAME` and -`PACKAGIST_TOKEN` once the package is submitted on packagist.org (until then that step only says so). +`PACKAGIST_TOKEN` (until they are set that step only says so). When Packagist does not know the package yet, that step +submits it through the create-package API; after that it only tells Packagist about each new tag. ## What Composer installs diff --git a/TOKENS.md b/TOKENS.md index db96ee9..0c1f232 100644 --- a/TOKENS.md +++ b/TOKENS.md @@ -21,3 +21,4 @@ Per stuk werk een rij; zie `dev/token-usage.py`. | 2026-12-04 | Het Hermes-tokenscript uit bouwbedrijf overgenomen, met dezelfde rijen per model als het hoofdscript | - | claude-sonnet-5-5 | 9 | 7.044 | 20 | 15.263 | 2.258.010 | 0,01 | | 2026-12-04 | Wekelijkse update van de versiebereiken van afhankelijkheden, als pull request naar de open release | - | claude-sonnet-5-5 | 6 | 7.611 | 12 | 10.963 | 1.632.350 | 0,01 | | 2026-12-04 | De commit zonder AI-vermelding hersteld in de release, de uitleg over de nachtelijke scans gecorrigeerd en de notities in longterm-memory gemaakt | - | claude-sonnet-5-5 | 29 | 16.997 | 72 | 21.381 | 8.975.420 | 0,11 | +| 2026-12-04 | De Packagist-stap meldt het pakket zelf aan als Packagist het nog niet kent | - | claude-sonnet-5-5 | 13 | 7.095 | 30 | 11.993 | 4.256.820 | 0,06 | -- 2.47.3