ci: label Type/State/Category (fix indent + debug PARSED)
This commit is contained in:
@@ -8,7 +8,7 @@ jobs:
|
|||||||
label:
|
label:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Apply labels from Type/State
|
- name: Apply labels from Type/State/Category
|
||||||
env:
|
env:
|
||||||
FORGE_BASE: ${{ vars.FORGE_API || vars.FORGE_BASE }}
|
FORGE_BASE: ${{ vars.FORGE_API || vars.FORGE_BASE }}
|
||||||
FORGE_TOKEN: ${{ secrets.FORGE_TOKEN }}
|
FORGE_TOKEN: ${{ secrets.FORGE_TOKEN }}
|
||||||
@@ -24,64 +24,71 @@ jobs:
|
|||||||
event_path = os.environ["EVENT_PATH"]
|
event_path = os.environ["EVENT_PATH"]
|
||||||
|
|
||||||
with open(event_path, "r", encoding="utf-8") as f:
|
with open(event_path, "r", encoding="utf-8") as f:
|
||||||
ev = json.load(f)
|
ev = json.load(f)
|
||||||
|
|
||||||
issue = ev.get("issue") or {}
|
issue = ev.get("issue") or {}
|
||||||
title = issue.get("title") or ""
|
title = issue.get("title") or ""
|
||||||
body = issue.get("body") or ""
|
body = issue.get("body") or ""
|
||||||
number = issue.get("number") or issue.get("index")
|
number = issue.get("number") or issue.get("index")
|
||||||
if not number:
|
if not number:
|
||||||
raise SystemExit("No issue number/index in event payload")
|
raise SystemExit("No issue number/index in event payload")
|
||||||
|
|
||||||
def pick_line(key):
|
def pick_line(key: str) -> str:
|
||||||
m = re.search(rf"^{re.escape(key)}:\s*([^\n\r]+)", body, flags=re.M)
|
m = re.search(rf"^\s*{re.escape(key)}\s*:\s*([^\n\r]+)", body, flags=re.M)
|
||||||
return m.group(1).strip() if m else ""
|
return m.group(1).strip() if m else ""
|
||||||
|
|
||||||
desired = set()
|
desired = set()
|
||||||
|
|
||||||
t = pick_line("Type")
|
t = pick_line("Type")
|
||||||
s = pick_line("State")
|
s = pick_line("State")
|
||||||
|
c = pick_line("Category")
|
||||||
|
|
||||||
# 1) Type / State explicit (depuis le site)
|
print("PARSED:", {"Type": t, "State": s, "Category": c})
|
||||||
if t: desired.add(t)
|
|
||||||
if s: desired.add(s)
|
|
||||||
|
|
||||||
# 2) Fallback depuis le titre si besoin
|
# 1) explicite depuis le body
|
||||||
|
if t:
|
||||||
|
desired.add(t)
|
||||||
|
if s:
|
||||||
|
desired.add(s)
|
||||||
|
if c:
|
||||||
|
desired.add(c)
|
||||||
|
|
||||||
|
# 2) fallback depuis le titre si Type absent
|
||||||
if not t:
|
if not t:
|
||||||
if title.startswith("[Correction]"):
|
if title.startswith("[Correction]"):
|
||||||
desired.add("type/correction")
|
desired.add("type/correction")
|
||||||
elif title.startswith("[Fact-check]") or title.startswith("[Vérification]"):
|
elif title.startswith("[Fact-check]") or title.startswith("[Vérification]"):
|
||||||
desired.add("type/fact-check")
|
desired.add("type/fact-check")
|
||||||
|
|
||||||
|
# 3) fallback State si absent
|
||||||
if not s:
|
if not s:
|
||||||
# état par défaut si absent
|
if "type/fact-check" in desired:
|
||||||
if "type/fact-check" in desired:
|
desired.add("state/a-sourcer")
|
||||||
desired.add("state/a-sourcer")
|
elif "type/correction" in desired:
|
||||||
elif "type/correction" in desired:
|
desired.add("state/recevable")
|
||||||
desired.add("state/recevable")
|
|
||||||
|
|
||||||
if not desired:
|
if not desired:
|
||||||
print("No labels to apply.")
|
print("No labels to apply.")
|
||||||
raise SystemExit(0)
|
raise SystemExit(0)
|
||||||
|
|
||||||
api = forge + "/api/v1"
|
api = forge + "/api/v1"
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"token {token}",
|
"Authorization": f"token {token}",
|
||||||
"Accept": "application/json",
|
"Accept": "application/json",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"User-Agent": "archicratie-auto-label/1.0",
|
"User-Agent": "archicratie-auto-label/1.0",
|
||||||
}
|
}
|
||||||
|
|
||||||
def jreq(method, url, payload=None):
|
def jreq(method, url, payload=None):
|
||||||
data = None if payload is None else json.dumps(payload).encode("utf-8")
|
data = None if payload is None else json.dumps(payload).encode("utf-8")
|
||||||
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
req = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(req) as r:
|
with urllib.request.urlopen(req, timeout=20) as r:
|
||||||
b = r.read()
|
b = r.read()
|
||||||
return json.loads(b.decode("utf-8")) if b else None
|
return json.loads(b.decode("utf-8")) if b else None
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
b = e.read().decode("utf-8", errors="replace")
|
b = e.read().decode("utf-8", errors="replace")
|
||||||
raise RuntimeError(f"HTTP {e.code} {method} {url}\n{b}") from e
|
raise RuntimeError(f"HTTP {e.code} {method} {url}\n{b}") from e
|
||||||
|
|
||||||
# labels repo
|
# labels repo
|
||||||
labels = jreq("GET", f"{api}/repos/{owner}/{repo}/labels?limit=1000") or []
|
labels = jreq("GET", f"{api}/repos/{owner}/{repo}/labels?limit=1000") or []
|
||||||
@@ -89,7 +96,7 @@ jobs:
|
|||||||
|
|
||||||
missing = [x for x in desired if x not in name_to_id]
|
missing = [x for x in desired if x not in name_to_id]
|
||||||
if missing:
|
if missing:
|
||||||
raise SystemExit("Missing labels in repo: " + ", ".join(sorted(missing)))
|
raise SystemExit("Missing labels in repo: " + ", ".join(sorted(missing)))
|
||||||
|
|
||||||
wanted_ids = [name_to_id[x] for x in desired]
|
wanted_ids = [name_to_id[x] for x in desired]
|
||||||
|
|
||||||
@@ -102,9 +109,9 @@ jobs:
|
|||||||
# set labels = union (n'enlève rien)
|
# set labels = union (n'enlève rien)
|
||||||
url = f"{api}/repos/{owner}/{repo}/issues/{number}/labels"
|
url = f"{api}/repos/{owner}/{repo}/issues/{number}/labels"
|
||||||
try:
|
try:
|
||||||
jreq("PUT", url, {"labels": final_ids})
|
jreq("PUT", url, {"labels": final_ids})
|
||||||
except Exception:
|
except Exception:
|
||||||
jreq("PUT", url, final_ids)
|
jreq("PUT", url, final_ids)
|
||||||
|
|
||||||
print(f"OK labels #{number}: {sorted(desired)}")
|
print(f"OK labels #{number}: {sorted(desired)}")
|
||||||
PY
|
PY
|
||||||
Reference in New Issue
Block a user