Files
Heimdall/scripts/ci/post_comment.py
T

43 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Poste un commentaire markdown sur une PR Forgejo.
Usage:
post_comment.py <texte> [titre]
Env requis : FORGEJO_TOKEN, REPO (owner/repo), PR_NUMBER.
Si REPO/PR_NUMBER/FORGEJO_TOKEN absents → warning, sortie 0 (commentaire ignoré).
"""
import json
import os
import sys
import urllib.request
text = sys.argv[1] if len(sys.argv) > 1 else ""
title = sys.argv[2] if len(sys.argv) > 2 else "CI"
repo = os.environ.get("REPO", "")
pr = os.environ.get("PR_NUMBER", "")
token = os.environ.get("FORGEJO_TOKEN", "")
if not text.strip():
print("Rien à commenter — sortie sans commentaire")
raise SystemExit(0)
body = f"## 🤖 {title}\n\n{text}\n\n_Généré automatiquement par la CI._"
if not repo or not pr or not token:
print("::warning::REPO/PR_NUMBER/FORGEJO_TOKEN manquants — commentaire ignoré")
raise SystemExit(0)
url = f"https://forgejo.tordais.cc/api/v1/repos/{repo}/issues/{pr}/comments"
req = urllib.request.Request(
url,
data=json.dumps({"body": body}).encode(),
headers={"Authorization": f"token {token}", "Content-Type": "application/json"},
method="POST",
)
try:
urllib.request.urlopen(req)
print(f"Commentaire posté sur la PR #{pr}")
except Exception as e:
print(f"::warning::Impossible de poster le commentaire: {e}")