pr-review-core 0.10.1

Core engine for a self-hosted advisory AI PR reviewer: fetches a pull request diff, reviews it with a Claude model via OpenRouter, and posts line-anchored inline comments plus a summary. Works with GitHub and Bitbucket.
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! GitLab provider — REST API v4 with a personal/project access token
//! (`GITLAB_TOKEN`, sent as the `PRIVATE-TOKEN` header). Posts a summary note
//! (deduped/updated by marker) plus inline discussion comments (the bot's prior
//! inline discussions are deleted and reposted each run).
//!
//! A GitLab merge request is addressed by the URL-encoded project path plus the
//! MR `iid` (the `pr` argument throughout is the MR iid).

use anyhow::Result;
use reqwest::Client;
use serde::Deserialize;

use super::{is_bot_comment, InlineComment, PrMeta, ReviewPost};
use crate::clip;
use crate::config::{require, Config};

/// Percent-encode a single path segment, leaving RFC 3986 unreserved characters
/// (`A-Z a-z 0-9 - . _ ~`) intact and encoding everything else — including `/`.
///
/// Used both for the project id (a namespace path, e.g. `group/sub/project`) and
/// for repository file paths, which GitLab expects fully URL-encoded.
fn percent_encode(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
                out.push(b as char);
            }
            _ => out.push_str(&format!("%{b:02X}")),
        }
    }
    out
}

/// URL-encode a project path (`group/sub/project` → `group%2Fsub%2Fproject`) for
/// use as the `:id` in the GitLab API.
fn enc(repo: &str) -> String {
    percent_encode(repo)
}

fn mr_base(cfg: &Config, repo: &str, pr: u64) -> String {
    format!(
        "{}/projects/{}/merge_requests/{pr}",
        cfg.gitlab_api_base,
        enc(repo)
    )
}

/// Apply the GitLab auth header to a request builder.
fn gl(rb: reqwest::RequestBuilder, cfg: &Config) -> reqwest::RequestBuilder {
    rb.header("PRIVATE-TOKEN", &cfg.gitlab_token)
        .header("User-Agent", &cfg.user_agent)
}

pub async fn get_meta(client: &Client, cfg: &Config, repo: &str, pr: u64) -> Result<PrMeta> {
    require(&cfg.gitlab_token, "GITLAB_TOKEN")?;

    #[derive(Deserialize)]
    struct Mr {
        title: Option<String>,
        description: Option<String>,
        target_branch: Option<String>,
        sha: Option<String>,
    }

    let res = gl(client.get(mr_base(cfg, repo, pr)), cfg).send().await?;
    if !res.status().is_success() {
        let status = res.status();
        anyhow::bail!(
            "GitLab getMeta {status}: {}",
            clip(&res.text().await.unwrap_or_default(), 300)
        );
    }
    let mr: Mr = res.json().await?;
    Ok(PrMeta {
        repo: repo.to_string(),
        pr,
        title: mr.title,
        base_branch: mr.target_branch,
        head_sha: mr.sha,
        body: mr.description,
    })
}

/// Post a standalone MR note (NOT deduped) — used for `/ask` answers and
/// `/describe` confirmations. Returns the new note's URL.
///
/// # Errors
/// If `GITLAB_TOKEN` is missing or the request fails.
pub async fn post_comment(
    client: &Client,
    cfg: &Config,
    repo: &str,
    pr: u64,
    body: &str,
) -> Result<Option<String>> {
    require(&cfg.gitlab_token, "GITLAB_TOKEN")?;
    #[derive(Deserialize)]
    struct Created {
        #[serde(default)]
        web_url: Option<String>,
    }
    let marked = format!("{body}\n\n_{}_", cfg.comment_marker);
    let res = gl(
        client.post(format!("{}/notes", mr_base(cfg, repo, pr))),
        cfg,
    )
    .json(&serde_json::json!({ "body": marked }))
    .send()
    .await?;
    if !res.status().is_success() {
        let status = res.status();
        anyhow::bail!(
            "GitLab postComment {status}: {}",
            clip(&res.text().await.unwrap_or_default(), 300)
        );
    }
    let c: Created = res.json().await?;
    Ok(c.web_url)
}

/// Replace the MR description (the `/describe` command).
///
/// # Errors
/// If `GITLAB_TOKEN` is missing or the request fails.
pub async fn update_pr_description(
    client: &Client,
    cfg: &Config,
    repo: &str,
    pr: u64,
    description: &str,
) -> Result<()> {
    require(&cfg.gitlab_token, "GITLAB_TOKEN")?;
    let res = gl(client.put(mr_base(cfg, repo, pr)), cfg)
        .json(&serde_json::json!({ "description": description }))
        .send()
        .await?;
    if !res.status().is_success() {
        let status = res.status();
        anyhow::bail!(
            "GitLab updatePrDescription {status}: {}",
            clip(&res.text().await.unwrap_or_default(), 300)
        );
    }
    Ok(())
}

pub async fn get_diff(client: &Client, cfg: &Config, repo: &str, pr: u64) -> Result<String> {
    require(&cfg.gitlab_token, "GITLAB_TOKEN")?;

    #[derive(Deserialize)]
    struct DiffEntry {
        old_path: Option<String>,
        new_path: Option<String>,
        diff: Option<String>,
        #[serde(default)]
        new_file: bool,
        #[serde(default)]
        deleted_file: bool,
        #[serde(default)]
        renamed_file: bool,
    }

    // One page of up to 100 files. GitLab paginates the diffs endpoint; a single
    // page is sufficient for typical MRs (see note in the module/PR).
    let url = format!("{}/diffs?per_page=100", mr_base(cfg, repo, pr));
    let res = gl(client.get(url), cfg).send().await?;
    if !res.status().is_success() {
        let status = res.status();
        anyhow::bail!(
            "GitLab getDiff {status}: {}",
            clip(&res.text().await.unwrap_or_default(), 300)
        );
    }
    let entries: Vec<DiffEntry> = res.json().await?;

    // Reconstruct a standard unified diff so the rest of the pipeline
    // (parse_valid_lines, glob filter, structural, pack) works unchanged.
    let mut out = String::new();
    for e in entries {
        let old_path = e.old_path.as_deref().unwrap_or_default();
        let new_path = e.new_path.as_deref().unwrap_or_default();
        out.push_str(&format!("diff --git a/{old_path} b/{new_path}\n"));
        let _ = e.renamed_file; // parsed for completeness; header above suffices
        if e.deleted_file {
            out.push_str(&format!("--- a/{old_path}\n+++ /dev/null\n"));
        } else if e.new_file {
            out.push_str(&format!("--- /dev/null\n+++ b/{new_path}\n"));
        } else {
            out.push_str(&format!("--- a/{old_path}\n+++ b/{new_path}\n"));
        }
        if let Some(d) = &e.diff {
            out.push_str(d);
        }
    }
    Ok(out)
}

/// Fetch a repo file's text at a git ref via the raw files endpoint.
///
/// Returns `Ok(None)` when the file doesn't exist (404) so the caller can treat
/// a missing `.prbot.toml` as "no overrides" rather than an error.
///
/// # Errors
/// If `GITLAB_TOKEN` is missing, the request fails, or the response can't be read.
pub async fn get_file_contents(
    client: &Client,
    cfg: &Config,
    repo: &str,
    r#ref: &str,
    path: &str,
) -> Result<Option<String>> {
    require(&cfg.gitlab_token, "GITLAB_TOKEN")?;

    let git_ref = r#ref;
    let url = format!(
        "{}/projects/{}/repository/files/{}/raw?ref={}",
        cfg.gitlab_api_base,
        enc(repo),
        percent_encode(path),
        percent_encode(git_ref)
    );
    let res = gl(client.get(url), cfg).send().await?;
    if res.status() == reqwest::StatusCode::NOT_FOUND {
        return Ok(None);
    }
    if !res.status().is_success() {
        let status = res.status();
        anyhow::bail!(
            "GitLab getFileContents {status}: {}",
            clip(&res.text().await.unwrap_or_default(), 300)
        );
    }
    Ok(Some(res.text().await?))
}

/// Authenticated HTTPS clone URL (`oauth2:<token>` basic form).
///
/// The host is derived from `gitlab_api_base` by stripping the scheme and the
/// trailing `/api/v4`, so self-managed instances clone from the right host —
/// e.g. `https://gitlab.com/api/v4` → `gitlab.com`.
pub fn clone_url(cfg: &Config, repo: &str) -> Result<String> {
    require(&cfg.gitlab_token, "GITLAB_TOKEN")?;
    let host = cfg
        .gitlab_api_base
        .trim_end_matches('/')
        .trim_end_matches("/api/v4")
        .trim_start_matches("https://")
        .trim_start_matches("http://")
        .trim_end_matches('/');
    Ok(format!(
        "https://oauth2:{}@{host}/{repo}.git",
        cfg.gitlab_token
    ))
}

// ── summary note (upsert by marker) ──────────────────────────────────────────

/// A merge request note (comment). Inline discussion comments carry a `position`.
#[derive(Deserialize)]
struct Note {
    id: u64,
    body: Option<String>,
    #[serde(default)]
    position: Option<serde_json::Value>,
}

/// One discussion (thread) — a wrapper around one or more notes.
#[derive(Deserialize)]
struct Discussion {
    id: String,
    notes: Option<Vec<Note>>,
}

async fn list_notes(client: &Client, cfg: &Config, repo: &str, pr: u64) -> Result<Vec<Note>> {
    let mut page = 1u32;
    let mut all: Vec<Note> = Vec::new();
    loop {
        let url = format!("{}/notes?per_page=100&page={page}", mr_base(cfg, repo, pr));
        let res = gl(client.get(url), cfg).send().await?;
        if !res.status().is_success() {
            let status = res.status();
            anyhow::bail!(
                "GitLab listNotes {status}: {}",
                clip(&res.text().await.unwrap_or_default(), 300)
            );
        }
        let notes: Vec<Note> = res.json().await?;
        let n = notes.len();
        all.extend(notes);
        if n < 100 {
            return Ok(all);
        }
        page += 1;
    }
}

async fn find_summary_note(
    client: &Client,
    cfg: &Config,
    repo: &str,
    pr: u64,
) -> Result<Option<u64>> {
    let notes = list_notes(client, cfg, repo, pr).await?;
    Ok(notes
        .into_iter()
        // A summary note is a plain (non-inline) note authored by the bot.
        .find(|n| n.position.is_none() && n.body.as_deref().is_some_and(|b| is_bot_comment(cfg, b)))
        .map(|n| n.id))
}

async fn upsert_summary(
    client: &Client,
    cfg: &Config,
    repo: &str,
    pr: u64,
    body: &str,
) -> Result<Option<String>> {
    #[derive(Deserialize)]
    struct Created {
        #[serde(default)]
        web_url: Option<String>,
    }
    let marked = format!("{body}\n\n_{}_", cfg.comment_marker);
    let (req, action) = match find_summary_note(client, cfg, repo, pr).await? {
        Some(id) => (
            client.put(format!("{}/notes/{id}", mr_base(cfg, repo, pr))),
            "updateNote",
        ),
        None => (
            client.post(format!("{}/notes", mr_base(cfg, repo, pr))),
            "postNote",
        ),
    };
    let res = gl(req, cfg)
        .json(&serde_json::json!({ "body": marked }))
        .send()
        .await?;
    if !res.status().is_success() {
        let status = res.status();
        anyhow::bail!(
            "GitLab {action} {status}: {}",
            clip(&res.text().await.unwrap_or_default(), 300)
        );
    }
    let c: Created = res.json().await?;
    Ok(c.web_url)
}

// ── inline discussions (delete prior, post new) ──────────────────────────────

/// The three shas GitLab needs to anchor an inline discussion to the diff.
#[derive(Deserialize, Clone)]
struct DiffRefs {
    base_sha: Option<String>,
    start_sha: Option<String>,
    head_sha: Option<String>,
}

async fn get_diff_refs(client: &Client, cfg: &Config, repo: &str, pr: u64) -> Result<DiffRefs> {
    #[derive(Deserialize)]
    struct Mr {
        diff_refs: Option<DiffRefs>,
    }
    let res = gl(client.get(mr_base(cfg, repo, pr)), cfg).send().await?;
    if !res.status().is_success() {
        let status = res.status();
        anyhow::bail!(
            "GitLab getDiffRefs {status}: {}",
            clip(&res.text().await.unwrap_or_default(), 300)
        );
    }
    let mr: Mr = res.json().await?;
    mr.diff_refs
        .ok_or_else(|| anyhow::anyhow!("GitLab MR has no diff_refs"))
}

async fn list_discussions(
    client: &Client,
    cfg: &Config,
    repo: &str,
    pr: u64,
) -> Result<Vec<Discussion>> {
    let mut page = 1u32;
    let mut all: Vec<Discussion> = Vec::new();
    loop {
        let url = format!(
            "{}/discussions?per_page=100&page={page}",
            mr_base(cfg, repo, pr)
        );
        let res = gl(client.get(url), cfg).send().await?;
        if !res.status().is_success() {
            let status = res.status();
            anyhow::bail!(
                "GitLab listDiscussions {status}: {}",
                clip(&res.text().await.unwrap_or_default(), 300)
            );
        }
        let discussions: Vec<Discussion> = res.json().await?;
        let n = discussions.len();
        all.extend(discussions);
        if n < 100 {
            return Ok(all);
        }
        page += 1;
    }
}

/// Delete the bot's prior inline discussion notes (best-effort).
async fn delete_prior_inline(client: &Client, cfg: &Config, repo: &str, pr: u64) -> Result<()> {
    let discussions = list_discussions(client, cfg, repo, pr).await?;
    for d in discussions {
        for note in d.notes.into_iter().flatten() {
            // Only our own inline notes (those carrying a position).
            let ours = note.position.is_some()
                && note.body.as_deref().is_some_and(|b| is_bot_comment(cfg, b));
            if !ours {
                continue;
            }
            let url = format!(
                "{}/discussions/{}/notes/{}",
                mr_base(cfg, repo, pr),
                d.id,
                note.id
            );
            let _ = gl(client.delete(url), cfg).send().await; // best-effort
        }
    }
    Ok(())
}

async fn post_inline(
    client: &Client,
    cfg: &Config,
    repo: &str,
    pr: u64,
    refs: &DiffRefs,
    c: &InlineComment,
) -> Result<()> {
    let body = format!("{}\n\n_{}_", c.body, cfg.comment_marker);
    let res = gl(
        client.post(format!("{}/discussions", mr_base(cfg, repo, pr))),
        cfg,
    )
    .json(&serde_json::json!({
        "body": body,
        "position": {
            "position_type": "text",
            "base_sha": refs.base_sha,
            "start_sha": refs.start_sha,
            "head_sha": refs.head_sha,
            "new_path": c.path,
            "new_line": c.line,
        }
    }))
    .send()
    .await?;
    if !res.status().is_success() {
        let status = res.status();
        // Don't abort the whole run on one bad anchor — log and move on.
        tracing::warn!(
            "GitLab inline comment failed ({status}) on {}:{}: {}",
            c.path,
            c.line,
            clip(&res.text().await.unwrap_or_default(), 200)
        );
    }
    Ok(())
}

pub async fn post_review(
    client: &Client,
    cfg: &Config,
    meta: &PrMeta,
    review: &ReviewPost,
) -> Result<Option<String>> {
    require(&cfg.gitlab_token, "GITLAB_TOKEN")?;
    let repo = &meta.repo;
    let pr = meta.pr;

    // Refresh inline comments: delete the bot's prior ones, post the new set.
    if !review.inline.is_empty() {
        match get_diff_refs(client, cfg, repo, pr).await {
            Ok(refs) => {
                delete_prior_inline(client, cfg, repo, pr).await?;
                for c in &review.inline {
                    post_inline(client, cfg, repo, pr, &refs, c).await?;
                }
            }
            Err(e) => {
                tracing::warn!("no diff_refs for {repo}!{pr} ({e:#}); skipping inline comments");
            }
        }
    }

    upsert_summary(client, cfg, repo, pr, &review.summary).await
}