71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
"""Liest den Gitea-Actions-Event-Kontext (GitHub-kompatible Env-Vars).
|
|
|
|
Gitea spiegelt die GITHUB_*-Variablen: GITHUB_REPOSITORY (owner/repo),
|
|
GITHUB_EVENT_PATH (JSON des auslösenden Events), GITHUB_EVENT_NAME.
|
|
"""
|
|
import json
|
|
import os
|
|
|
|
|
|
def repo_slug():
|
|
"""(owner, repo) aus GITHUB_REPOSITORY."""
|
|
full = os.environ.get("GITHUB_REPOSITORY", "")
|
|
if "/" not in full:
|
|
raise SystemExit("[context] GITHUB_REPOSITORY fehlt oder ist ungültig.")
|
|
owner, repo = full.split("/", 1)
|
|
return owner, repo
|
|
|
|
|
|
def event():
|
|
"""Geparstes Event-JSON (oder {} wenn nicht vorhanden)."""
|
|
path = os.environ.get("GITHUB_EVENT_PATH", "")
|
|
if path and os.path.exists(path):
|
|
with open(path, encoding="utf-8") as fh:
|
|
return json.load(fh)
|
|
return {}
|
|
|
|
|
|
def pull_request():
|
|
"""PR-Infos aus dem Event oder None, wenn das Event kein PR ist.
|
|
|
|
Liefert dict: index, title, body, author, base_ref, head_sha.
|
|
"""
|
|
pr = event().get("pull_request")
|
|
if not pr:
|
|
return None
|
|
return {
|
|
"index": pr.get("number"),
|
|
"title": pr.get("title", ""),
|
|
"body": pr.get("body", "") or "",
|
|
"author": (pr.get("user") or {}).get("login", ""),
|
|
"base_ref": (pr.get("base") or {}).get("ref", ""),
|
|
"head_sha": (pr.get("head") or {}).get("sha", ""),
|
|
}
|
|
|
|
|
|
def event_name():
|
|
"""Name des auslösenden Events (push, pull_request, issue_comment, …)."""
|
|
return os.environ.get("GITHUB_EVENT_NAME", "")
|
|
|
|
|
|
def comment_body():
|
|
"""Body des auslösenden Kommentars (issue_comment-Event) oder ''."""
|
|
return (event().get("comment") or {}).get("body") or ""
|
|
|
|
|
|
def issue():
|
|
"""Issue/PR-Infos aus einem issue_comment-Event oder None.
|
|
|
|
Liefert dict: index, title, body, author, is_pr.
|
|
"""
|
|
iss = event().get("issue")
|
|
if not iss:
|
|
return None
|
|
return {
|
|
"index": iss.get("number"),
|
|
"title": iss.get("title", ""),
|
|
"body": iss.get("body", "") or "",
|
|
"author": (iss.get("user") or {}).get("login", ""),
|
|
"is_pr": bool(iss.get("pull_request")),
|
|
}
|