112 lines
3.9 KiB
Python
112 lines
3.9 KiB
Python
"""Turns raw Gitea Actions job/step results into a plain nested status dict,
|
|
and renders that dict into Discord-ready ``Section`` objects.
|
|
|
|
Nothing here knows about any specific project's artifact names (APK, AAB,
|
|
Play Store, ...) — the step-name mapping and display labels are supplied by
|
|
the caller (see Tamagosaure's ``notify.py`` for a concrete example).
|
|
|
|
Status dicts look like::
|
|
|
|
{
|
|
"android": [
|
|
{"builds": {"apk": True, "aab": False}},
|
|
{"deploy": {"itch": True, "play-store": None}},
|
|
],
|
|
}
|
|
|
|
``True``/``False`` mean the step ran and passed/failed; ``None`` means it
|
|
never ran during this workflow run (job skipped, step never reached, ...).
|
|
"""
|
|
|
|
from typing import Any, Optional
|
|
|
|
from .core import Section, StepResult
|
|
|
|
# step name -> (section key, "builds" | "deploy", item key).
|
|
# Keyed by step name rather than job name: several reusable workflows can
|
|
# define jobs with the same name (e.g. both the web and android pipelines
|
|
# have a job called "deploy-itch"), but their step names are always distinct.
|
|
StepMapping = dict[str, tuple[str, str, str]]
|
|
|
|
|
|
def _conclusion_to_bool(conclusion: str) -> Optional[bool]:
|
|
if conclusion == "success":
|
|
return True
|
|
if conclusion == "failure":
|
|
return False
|
|
return None
|
|
|
|
|
|
def build_status(jobs: list[dict[str, Any]], mapping: StepMapping) -> dict[str, list[dict]]:
|
|
status: dict[str, list[dict]] = {}
|
|
|
|
def bucket_for(section: str, bucket: str) -> dict:
|
|
entries = status.setdefault(section, [])
|
|
for entry in entries:
|
|
if bucket in entry:
|
|
return entry[bucket]
|
|
new_bucket: dict = {}
|
|
entries.append({bucket: new_bucket})
|
|
return new_bucket
|
|
|
|
# Seed every mapped key to "not run" first, so a step that never shows
|
|
# up in this run (skipped job, earlier failure, ...) still reports as
|
|
# pending instead of silently disappearing from the payload.
|
|
for _step_name, (section, bucket, key) in mapping.items():
|
|
bucket_for(section, bucket)[key] = None
|
|
|
|
steps_by_name = {
|
|
step["name"]: step.get("conclusion", "")
|
|
for job in jobs
|
|
for step in job.get("steps", [])
|
|
}
|
|
|
|
for step_name, (section, bucket, key) in mapping.items():
|
|
if step_name in steps_by_name:
|
|
bucket_for(section, bucket)[key] = _conclusion_to_bool(steps_by_name[step_name])
|
|
|
|
return status
|
|
|
|
|
|
def sections_from_status(
|
|
status: dict[str, list[dict]],
|
|
sections_config: dict[str, dict],
|
|
run_url: Optional[str] = None,
|
|
) -> list[Section]:
|
|
"""Build Discord ``Section`` objects from a status dict.
|
|
|
|
``sections_config`` maps a status key (e.g. "android") to display
|
|
config: ``title``, ``description``, ``builds_labels``, ``deploy_labels``,
|
|
``url_suffix`` and ``author``.
|
|
"""
|
|
sections = []
|
|
for key, config in sections_config.items():
|
|
entries = status.get(key, [])
|
|
builds_raw = next((entry["builds"] for entry in entries if "builds" in entry), {})
|
|
deploy_raw = next((entry["deploy"] for entry in entries if "deploy" in entry), {})
|
|
|
|
build_labels = config.get("builds_labels", {})
|
|
deploy_labels = config.get("deploy_labels", {})
|
|
|
|
url = run_url
|
|
if run_url and config.get("url_suffix"):
|
|
url = f"{run_url}{config['url_suffix']}"
|
|
|
|
sections.append(
|
|
Section(
|
|
title=config["title"],
|
|
description=config.get("description", ""),
|
|
builds=[
|
|
StepResult(name=build_labels.get(name, name), passed=passed, log_url=run_url)
|
|
for name, passed in builds_raw.items()
|
|
],
|
|
deploys=[
|
|
StepResult(name=deploy_labels.get(name, name), passed=passed, log_url=run_url)
|
|
for name, passed in deploy_raw.items()
|
|
],
|
|
url=url,
|
|
author=config.get("author", "Gitea CI"),
|
|
)
|
|
)
|
|
return sections
|