File size: 12,910 Bytes
7444884
 
 
 
 
 
 
 
5246c92
 
7444884
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
from __future__ import annotations

import logging
import os

import requests
from typing import Any

from backend.integrations.github_client import GitHubSession, resolve_token
from backend.state import Finding, OpenCodeReviewState

logger = logging.getLogger(__name__)

GITHUB_API_BASE = "https://api.github.com"

# ─── Helpers ─────────────────────────────────────────────────────────────────


_GITHUB_SESSION: GitHubSession | None = None
_POST_ERRORS: list[str] = []


def _gh_session() -> GitHubSession:
    """Return a lazily-initialized :class:`GitHubSession`."""
    global _GITHUB_SESSION
    if _GITHUB_SESSION is None:
        _GITHUB_SESSION = GitHubSession()
    return _GITHUB_SESSION


def _fetch_pr_head_sha(owner: str, repo: str, pr_number: int) -> str | None:
    """Fetch the latest head SHA of the PR so inline comments target the correct
    commit even if the PR was updated since ingestion."""
    url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/pulls/{pr_number}"
    try:
        resp = _gh_session().get(url, timeout=30)
        if resp.status_code == 200:
            return resp.json()["head"]["sha"]
        logger.warning(
            "Could not fetch PR metadata: HTTP %s β€” %s",
            resp.status_code, resp.text[:120],
        )
        return None
    except requests.RequestException as exc:
        logger.warning("Request failed while fetching PR metadata: %s", exc)
        return None


def _post_review_comment(
    owner: str,
    repo: str,
    pr_number: int,
    commit_id: str,
    path: str,
    line: int,
    side: str,
    body: str,
    start_line: int | None = None,
) -> bool:
    """Post an inline PR review comment at the given file/line position.

    Uses ``POST /repos/{owner}/{repo}/pulls/{pull_number}/comments``.

    Parameters
    ----------
    line : int
        The line number in the diff. For multi-line comments this is the
        *last* line of the range.
    side : str
        ``"LEFT"`` or ``"RIGHT"`` β€” which side of the diff the comment
        targets. We always use ``"RIGHT"`` (the new/post-PR version).
    start_line : int | None
        When set, creates a multi-line comment spanning *start_line* β†’
        *line*.
    """
    payload: dict[str, Any] = {
        "body": body,
        "commit_id": commit_id,
        "path": path,
        "line": line,
        "side": side,
    }
    if start_line is not None:
        payload["start_line"] = start_line
        payload["start_side"] = side

    url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/pulls/{pr_number}/comments"
    try:
        resp = _gh_session().post(url, json=payload, timeout=30)
        if resp.status_code in (201, 200):
            logger.debug(
                "Inline comment posted to %s:%d (%s)", path, line, side,
            )
            return True
        if resp.status_code == 422:
            # Stale line: the line no longer exists in the latest diff.
            logger.info(
                "Line %d no longer exists in %s (422) β€” will fall back "
                "to issue comment", line, path,
            )
            return False
        logger.warning(
            "Failed to post inline comment: HTTP %s β€” %s",
            resp.status_code, resp.text[:200],
        )
        _POST_ERRORS.append(f"inline:{resp.status_code}:{resp.text[:200]}")
        return False
    except requests.RequestException as exc:
        logger.warning("Request failed for inline comment: %s", exc)
        return False


def _post_issue_comment(owner: str, repo: str, pr_number: int, body: str) -> bool:
    """Post a general (non-inline) issue/PR comment as a fallback.

    Uses ``POST /repos/{owner}/{repo}/issues/{issue_number}/comments``.
    """
    url = f"{GITHUB_API_BASE}/repos/{owner}/{repo}/issues/{pr_number}/comments"
    try:
        resp = _gh_session().post(
            url, json={"body": body}, timeout=30,
        )
        if resp.status_code in (201, 200):
            logger.debug("Fallback issue comment posted")
            return True
        logger.warning(
            "Failed to post fallback comment: HTTP %s β€” %s",
            resp.status_code, resp.text[:200],
        )
        _POST_ERRORS.append(f"issue:{resp.status_code}:{resp.text[:200]}")
        return False
    except requests.RequestException as exc:
        logger.warning("Request failed for fallback comment: %s", exc)
        return False


# ─── Formatting ──────────────────────────────────────────────────────────────

SEVERITY_EMOJI = {
    "critical": "\U0001f6a8",  # 🚨
    "high": "\u26a0\ufe0f",    # ⚠️
    "medium": "\U0001f4cc",    # πŸ“Œ
    "low": "\U0001f4a1",       # πŸ’‘
    "info": "\u2139\ufe0f",    # ℹ️
}
VERDICT_ICON = {
    "approve": "\u2705",
    "request_changes": "\U0001f504",
    "block": "\U0001f6ab",
    "comment": "\U0001f4ac",
}


def _finding_body(finding: Finding) -> str:
    """Format a single finding as Markdown for the comment body."""
    emoji = SEVERITY_EMOJI.get(finding.severity.value, "\U0001f50d")
    fix = ""
    if finding.suggested_fix:
        fix = f"\n\n**Suggested fix:**\n```\n{finding.suggested_fix}\n```"
    return (
        f"{emoji} **{finding.severity.value.upper()}** β€” "
        f"*{finding.category}*  \n"
        f"**Model confidence (uncalibrated):** {finding.confidence:.0%}  \n"
        f"**Evidence:** {finding.evidence_status} β€” source location checked; behavior not independently reproduced.  \n\n"
        f"{finding.comment}{fix}"
    )


def _finding_fallback_body(finding: Finding) -> str:
    """Format a finding for a fallback issue comment when the line is stale."""
    return (
        f"*(This comment refers to a previous version of the code β€” "
        f"the line may no longer exist in the latest diff.)*\n\n"
        f"{_finding_body(finding)}\n\n"
        f"**Original location:** `{finding.file_path}` "
        f"(lines {finding.line_start}\u2013{finding.line_end})"
    )


def _verdict_body(verdict_dict: dict) -> str:
    """Format the overall verdict as a Markdown PR comment."""
    emoji = VERDICT_ICON.get(verdict_dict.get("recommendation", ""), "\U0001f4cb")
    summary = verdict_dict.get("summary", "")
    score = verdict_dict.get("overall_score", 0)
    return (
        f"## OpenCodeReview \u2014 {emoji} "
        f"{verdict_dict['recommendation'].upper()}\n\n"
        f"**Risk estimate:** {score}/10 (not answer correctness)  \n"
        f"**Summary:** {summary}\n"
    )


# ─── Node ────────────────────────────────────────────────────────────────────


def executor_node(state: OpenCodeReviewState) -> dict:
    """Post approved findings as GitHub **PR review comments** at the correct
    file/line positions.

    Workflow
    --------
    1. Only runs if ``state.human_approved is True``.
    2. Fetches the latest PR head SHA every time (handles PR updates).
    3. For each finding:
       a. Try ``POST /pulls/{pr}/comments`` (inline review comment).
       b. On **422** (stale line): fall back to
          ``POST /issues/{pr}/comments`` (general comment).
    4. Posts the verdict as a general issue comment.

    Requires ``GITHUB_TOKEN`` to post.  Without it, logs what it *would*
    have posted.
    """
    if state.verdict and state.verdict.recommendation == "incomplete":
        return {"posting_result": {"status": "skipped", "reason": "incomplete_review"}}
    if not state.human_approved:
        logger.info("Not approved \u2014 skipping executor")
        return {"posting_result": {"status": "skipped", "reason": "not_approved"}}

    postable_findings = [finding for finding in state.final_findings if finding.evidence_status == "source_grounded"]
    withheld = len(state.final_findings) - len(postable_findings)
    _POST_ERRORS.clear()

    parts = state.repo.split("/")
    if len(parts) != 2:
        logger.warning("Cannot post results: invalid repo '%s'", state.repo)
        return {"posting_result": {"status": "failed", "reason": "invalid_repo"}}
    owner, repo_name = parts
    pr_number = state.pr_number

    gh_token = os.environ.get("GITHUB_TOKEN", "").strip()
    if not gh_token:
        gh_token = resolve_token() or ""
    if not gh_token:
        logger.warning(
            "No GitHub token available \u2014 cannot post to %s/%s#%d. "
            "Run 'opencodereview auth login' or set GITHUB_TOKEN.",
            owner, repo_name, pr_number,
        )
        if state.verdict:
            logger.info("[WOULD POST] Verdict: %s", state.verdict.summary)
        for f in postable_findings:
            logger.info(
                "[WOULD POST] %s:%d\u2013%d %s \u2014 %s",
                f.file_path, f.line_start, f.line_end,
                f.severity.value, f.comment[:60],
            )
        return {"posting_result": {"status": "dry_run", "inline_posted": 0, "fallback_posted": 0, "failed": len(state.final_findings)}}

    # ── 1. Fetch latest head SHA ─────────────────────────────────────────
    commit_id = _fetch_pr_head_sha(owner, repo_name, pr_number)
    if commit_id is None:
        logger.warning(
            "Could not fetch PR head SHA \u2014 cannot post inline "
            "comments. Falling back to issue-level comments."
        )

    # ── 2. Post each finding ─────────────────────────────────────────────
    inline_ok = 0
    inline_failed = 0
    fallback_ok = 0

    for f in postable_findings:
        body = _finding_body(f)

        if commit_id:
            # Try inline review comment
            if f.line_start == f.line_end:
                ok = _post_review_comment(
                    owner, repo_name, pr_number,
                    commit_id, f.file_path, f.line_start, "RIGHT", body,
                )
            else:
                # Multi-line: end_line is the "line" param, start_line
                # marks the range start.
                ok = _post_review_comment(
                    owner, repo_name, pr_number,
                    commit_id, f.file_path, f.line_end, "RIGHT", body,
                    start_line=f.line_start,
                )

            if ok:
                inline_ok += 1
                continue
            inline_failed += 1

        # Fallback: issue-level comment with stale-line context
        fallback_body = (
            _finding_fallback_body(f) if commit_id else body
        )
        if _post_issue_comment(owner, repo_name, pr_number, fallback_body):
            fallback_ok += 1

    # ── 3. Post the verdict ──────────────────────────────────────────────
    verdict_posted = False
    if state.verdict:
        verdict_body = _verdict_body(state.verdict.model_dump())
        verdict_posted = _post_issue_comment(owner, repo_name, pr_number, verdict_body)

    total = len(postable_findings)
    logger.info(
        "Executor: %d/%d inline, %d/%d fallback, verdict %s \u2014 "
        "%s/%s#%d",
        inline_ok, total, fallback_ok, total - inline_ok,
        "posted" if state.verdict else "skipped",
        owner, repo_name, pr_number,
    )
    finding_failed = total - inline_ok - fallback_ok
    verdict_failed = int(state.verdict is not None and not verdict_posted)
    failed = finding_failed + verdict_failed
    # A review is fully posted only when every finding and the verdict were
    # accepted by GitHub. Keep the per-part counts so the API/UI can explain a
    # partial result instead of reporting a misleading success.
    successful_parts = inline_ok + fallback_ok + int(verdict_posted)
    expected_parts = total + int(state.verdict is not None)
    if expected_parts == 0:
        status = "posted"
    elif successful_parts == expected_parts:
        status = "posted"
    elif successful_parts:
        status = "partially_posted"
    else:
        status = "post_failed"
    if withheld and status == "posted":
        status = "partially_posted"
    return {"posting_result": {
        "status": status,
        "withheld_unverified": withheld,
        "total_findings": total,
        "inline_posted": inline_ok,
        "fallback_posted": fallback_ok,
        "failed": failed,
        "finding_failed": finding_failed,
        "verdict_failed": verdict_failed,
        "verdict_posted": verdict_posted,
        "errors": list(_POST_ERRORS),
    }}