#!/usr/bin/env python3 """Tells Packagist that the package has a new version. 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. 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). The token goes in the request and never in the log. Standard library only. """ import json import os import sys import urllib.error import urllib.parse import urllib.request USER = os.environ.get("PACKAGIST_USERNAME", "").strip() TOKEN = os.environ.get("PACKAGIST_TOKEN", "").strip() PACKAGE_URL = os.environ.get("PACKAGE_URL", "").strip() 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.") 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]}") if __name__ == "__main__": main()