Files
Gitea-Tools/manage_labels.py
T

114 lines
3.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Create a label set on a Gitea repo and apply labels to issues.
Auth follows the project convention: credentials are pulled from the macOS
keychain via `git credential fill` (HTTPS), then sent as Basic auth.
Usage:
./manage_labels.py # create labels, then apply the mapping below
./manage_labels.py --dry # print actions without writing
"""
import os
import sys
# Auto-execute using the project's local virtual environment Python
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
venv_python = os.path.join(PROJECT_ROOT, "venv", "bin", "python3")
if os.path.exists(venv_python) and sys.executable != venv_python:
os.execv(venv_python, [venv_python] + sys.argv)
from gitea_auth import get_auth_header, api_request, repo_api_url
HOST = "gitea.dadeschools.net"
ORG = "Contractor"
REPO = "Timesheet"
# Mirror of the eAgenda label set (i18n omitted as irrelevant to Timesheet).
LABELS = [
{"name": "chore", "color": "5319e7", "description": ""},
{"name": "critical", "color": "b60205", "description": ""},
{"name": "epic", "color": "8250df", "description": ""},
{"name": "important", "color": "fbca04", "description": ""},
{"name": "nice-to-have", "color": "0e8a16", "description": ""},
{"name": "status:in-progress", "color": "fefe2e",
"description": "Issue is being worked on"},
]
# issue number -> label names to apply
MAPPING = {
23: ["chore"],
22: ["chore"],
21: ["chore"],
20: ["chore", "status:in-progress"],
19: ["chore"],
18: ["chore"],
17: ["important"],
14: ["nice-to-have"],
13: ["nice-to-have"],
12: ["important"],
11: ["important"],
8: ["nice-to-have"],
7: ["nice-to-have"],
3: ["important"],
2: ["important"],
1: ["nice-to-have"],
}
BASE_URL = repo_api_url(HOST, ORG, REPO)
def api(method, path, auth, payload=None):
"""Thin wrapper around auth.api_request that prepends BASE_URL and
handles errors gracefully (returns None instead of raising)."""
url = f"{BASE_URL}{path}"
try:
return api_request(method, url, auth, payload)
except RuntimeError as e:
print(f" {e}", file=sys.stderr)
return None
def main():
dry = "--dry" in sys.argv
auth = get_auth_header(HOST)
if auth is None:
print("Could not get credentials from git credential fill",
file=sys.stderr)
sys.exit(1)
# 1. Existing labels -> name:id
existing = api("GET", "/labels?limit=100", auth) or []
by_name = {l["name"]: l["id"] for l in existing}
# 2. Create missing labels
for spec in LABELS:
if spec["name"] in by_name:
print(f"label exists: {spec['name']}")
continue
if dry:
print(f"[dry] create label: {spec['name']} #{spec['color']}")
continue
created = api("POST", "/labels", auth, spec)
if created:
by_name[created["name"]] = created["id"]
print(f"created label: {created['name']} (id {created['id']})")
# 3. Apply mapping
for issue, names in sorted(MAPPING.items(), reverse=True):
ids = [by_name[n] for n in names if n in by_name]
missing = [n for n in names if n not in by_name]
if missing:
print(f" #{issue}: skipping unknown labels {missing}", file=sys.stderr)
if dry:
print(f"[dry] #{issue} <- {names}")
continue
# PUT replaces the issue's labels with exactly this set (idempotent).
res = api("PUT", f"/issues/{issue}/labels", auth, {"labels": ids})
if res is not None:
applied = [l["name"] for l in res]
print(f"#{issue} labeled: {applied}")
if __name__ == "__main__":
main()