"""GitLab MR write-back helper for the publish_findings node.""" import logging import os from core.state import InvestigationState from integrations.gitlab import build_gitlab_config, post_gitlab_mr_note from platform.common.truncation import truncate logger = logging.getLogger(__name__) _GITLAB_MR_NOTE_LIMIT = 4000 def _build_mr_note(report: str) -> str: body = truncate(report.strip(), _GITLAB_MR_NOTE_LIMIT) return f"### RCA Finding\n\n
\nInvestigation summary\n\n{body}\n\n
" def post_gitlab_mr_writeback(state: InvestigationState, report: str) -> None: """Post an RCA summary as a GitLab MR note if write-back is enabled. No-ops when: - GITLAB_MR_WRITEBACK env var is not set to a truthy value - merge_request_iid or project_id are absent from state Failures are logged as warnings and never propagate to the caller. """ if os.getenv("GITLAB_MR_WRITEBACK", "").lower() not in ("true", "1", "yes"): return gl = (state.get("available_sources") or {}).get("gitlab", {}) mr_iid = gl.get("merge_request_iid", "") project_id = gl.get("project_id", "") if not mr_iid or not project_id: return try: gl_config = build_gitlab_config( { "base_url": gl.get("gitlab_url", ""), "auth_token": gl.get("gitlab_token", ""), } ) post_gitlab_mr_note( config=gl_config, project_id=project_id, mr_iid=mr_iid, body=_build_mr_note(report), ) logger.info("[publish] GitLab MR note posted: project=%s mr_iid=%s", project_id, mr_iid) except Exception as exc: logger.warning("[publish] GitLab MR write-back failed: %s", exc)