diff --git a/templates.py b/templates.py index baa91ff..185dc13 100644 --- a/templates.py +++ b/templates.py @@ -156,16 +156,16 @@ def gitea(args: argparse.Namespace) -> None: raw = response.read() return json.loads(raw) if raw else None + # Gitea happily creates a second label with the same name, so the only way to + # stay idempotent is to look first. + existing = {label["name"] for label in call("GET", f"/repos/{args.repo}/labels?limit=100") or []} for name, color, description in RENOVATE_LABELS: - try: - call("POST", f"/repos/{args.repo}/labels", - {"name": name, "color": color, "description": description}) - print(f"label {name}: created") - except urllib.error.HTTPError as error: - if error.code in (409, 422): - print(f"label {name}: already there") - else: - sys.exit(f"label {name}: {error.code} {error.read()[:200]!r}") + if name in existing: + print(f"label {name}: already there") + continue + call("POST", f"/repos/{args.repo}/labels", + {"name": name, "color": color, "description": description}) + print(f"label {name}: created") try: call("GET", f"/repos/{args.repo}/contents/{RENOVATE_FILE}")