gitpoulpi-hosting 1.1.1

Hosting and issue-tracker API clients (GitHub, GitLab, Gitea/Forgejo, Azure DevOps, Bitbucket, Gerrit, Jira, Trello, Linear, YouTrack, Redmine, local Markdown issues): project listing, clone helpers, SSH key generation and upload, remote-URL provider matching.
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! The conversation of a GitLab issue or merge request: the header, its
//! discussions (system notes become events, positional discussions become
//! code threads) and posting, replying, editing and deleting notes.
//!
//! Issues and merge requests share one shape on GitLab: a list of
//! *discussions*, each a list of *notes*. The listing is oldest first with no
//! way to reverse it, so the newest window is found from `X-Total-Pages` and
//! read from the last page backwards.

use serde_json::{json, Value};

use super::GitLabClient;
use crate::client::encode_component;
use crate::conversation::time::is_edited;
use crate::conversation::{
    Actor, ActorKind, CodeThread, Comment, Event, EventKind, IdentityKind, ItemHeader, ItemKey,
    ItemKind, Markup, MarkupFlavor, PageCursor, PostingIdentity, TimelineEntry, TimelineOpts,
    TimelinePage, TokenSource, WINDOW,
};
use crate::gitlab::encode_gitlab_project_path;
use crate::http::HttpResponse;
use crate::review::gitlab::{thread_of, Discussion};
use crate::review::invalid;
use crate::{Error, Result};

const PAGES_PER_WINDOW: u32 = (WINDOW / 100) as u32;

fn text(v: &Value, key: &str) -> String {
    v.get(key)
        .and_then(Value::as_str)
        .unwrap_or_default()
        .to_string()
}

fn opt_text(v: &Value, key: &str) -> Option<String> {
    v.get(key)
        .and_then(Value::as_str)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
}

/// Project and group access tokens act as users named `project_<id>_bot...`
/// / `group_<id>_bot...`; newer GitLab also says so in a `bot` flag.
fn is_bot_login(login: &str) -> bool {
    let rest = login
        .strip_prefix("project_")
        .or_else(|| login.strip_prefix("group_"));
    rest.is_some_and(|r| {
        let digits = r.chars().take_while(char::is_ascii_digit).count();
        digits > 0 && r[digits..].starts_with("_bot")
    })
}

fn actor_of(user: Option<&Value>) -> Actor {
    let Some(user) = user.filter(|u| u.is_object()) else {
        return Actor::unknown();
    };
    let login = text(user, "username");
    let kind = if user["bot"].as_bool() == Some(true) || is_bot_login(&login) {
        ActorKind::Bot
    } else {
        ActorKind::User
    };
    let mut actor = Actor::new(login.clone(), opt_text(user, "name").unwrap_or(login), kind);
    actor.web_url = opt_text(user, "web_url");
    actor
}

fn note_comment(
    note: &Value,
    discussion_id: Option<&str>,
    in_reply_to: Option<String>,
    item_url: &str,
) -> Comment {
    let id = note["id"].as_u64().unwrap_or_default();
    let created_at = text(note, "created_at");
    let updated_at = opt_text(note, "updated_at");
    Comment {
        id: id.to_string(),
        author: actor_of(note.get("author")),
        body: Markup::new(text(note, "body"), MarkupFlavor::GitlabMd),
        edited: is_edited(&created_at, updated_at.as_deref()),
        created_at,
        updated_at,
        reactions: Vec::new(),
        in_reply_to,
        discussion_id: discussion_id.map(str::to_string),
        internal: note["internal"].as_bool().unwrap_or(false)
            || note["confidential"].as_bool().unwrap_or(false),
        can_edit: None,
        can_delete: None,
        web_url: (!item_url.is_empty()).then(|| format!("{item_url}#note_{id}")),
    }
}

/// Splits a system note's `~123 label` / `~"bug" label` list.
fn labels_in(body: &str, verb: &str) -> Vec<String> {
    let Some(rest) = body.strip_prefix(verb) else {
        return Vec::new();
    };
    let rest = rest
        .trim()
        .trim_end_matches(" labels")
        .trim_end_matches(" label");
    rest.split(" ~")
        .map(|l| {
            l.trim()
                .trim_start_matches('~')
                .trim_matches('"')
                .to_string()
        })
        .filter(|l| !l.is_empty())
        .collect()
}

fn mentions_in(body: &str, verb: &str) -> Vec<String> {
    body.strip_prefix(verb)
        .map(|r| {
            r.split([',', ' '])
                .filter(|w| w.starts_with('@'))
                .map(|w| w.trim_start_matches('@').to_string())
                .collect()
        })
        .unwrap_or_default()
}

/// What a system note says, as an event kind.
fn system_kind(body: &str) -> EventKind {
    let b = body.trim();
    match b {
        "closed" => return EventKind::Closed,
        "reopened" => return EventKind::Reopened,
        _ => {}
    }
    if b.starts_with("merged") {
        return EventKind::Merged;
    }
    if b.starts_with("added ") && (b.ends_with(" label") || b.ends_with(" labels")) {
        return EventKind::Labeled {
            added: labels_in(b, "added "),
            removed: Vec::new(),
        };
    }
    if b.starts_with("removed ") && (b.ends_with(" label") || b.ends_with(" labels")) {
        return EventKind::Labeled {
            added: Vec::new(),
            removed: labels_in(b, "removed "),
        };
    }
    if let Some(rest) = b.strip_prefix("assigned to ") {
        return EventKind::Assigned {
            added: mentions_in(&format!("assigned to {rest}"), "assigned to "),
            removed: Vec::new(),
        };
    }
    if b.starts_with("unassigned ") {
        return EventKind::Assigned {
            added: Vec::new(),
            removed: mentions_in(b, "unassigned "),
        };
    }
    if let Some(rest) = b.strip_prefix("changed title from ") {
        let strip = |s: &str| s.trim().trim_matches('*').to_string();
        if let Some((from, to)) = rest.split_once(" to ") {
            return EventKind::Renamed {
                from: strip(from),
                to: strip(to),
            };
        }
    }
    if let Some(rest) = b.strip_prefix("added ") {
        if let Some((n, tail)) = rest.split_once(' ') {
            if let (Ok(count), true) = (n.parse::<u32>(), tail.starts_with("commit")) {
                return EventKind::CommitsPushed { count, head: None };
            }
        }
    }
    if let Some(from) = b.strip_prefix("mentioned in ") {
        return EventKind::CrossReferenced {
            from: from.to_string(),
            url: None,
        };
    }
    if b.starts_with("requested review from ") {
        return EventKind::ReviewRequested {
            who: mentions_in(b, "requested review from ").join(", "),
        };
    }
    EventKind::Other {
        text: b.to_string(),
    }
}

/// `last` page from `X-Total-Pages`; 1 when the host does not say.
fn total_pages(response: &HttpResponse) -> u32 {
    response
        .headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("x-total-pages"))
        .and_then(|(_, v)| v.trim().parse().ok())
        .filter(|n| *n >= 1)
        .unwrap_or(1)
}

fn parse_cursor(cursor: &PageCursor) -> Option<u32> {
    cursor.0.parse().ok().filter(|p| *p >= 1)
}

impl GitLabClient {
    /// `<api>/projects/<id>/{issues|merge_requests}/<iid><rest>`.
    fn item_url(&self, project: &str, key: &ItemKey, rest: &str) -> String {
        let route = match key.kind {
            ItemKind::Issue => "issues",
            ItemKind::PullRequest => "merge_requests",
        };
        format!(
            "{}/projects/{}/{route}/{}{rest}",
            self.integration.api_base(),
            encode_gitlab_project_path(project),
            key.number
        )
    }

    async fn conv_item_row(&self, project: &str, key: &ItemKey) -> Result<Value> {
        let response = self
            .transport
            .send(self.get(self.item_url(project, key, "")))
            .await?;
        response.error_for_status("reading a GitLab issue or merge request")?;
        response.json()
    }

    fn header_of(&self, row: &Value, key: &ItemKey) -> ItemHeader {
        let state = row["state"].as_str().unwrap_or_default();
        let (state_label, open) = match state {
            "opened" => ("open", true),
            "merged" => ("merged", false),
            _ => ("closed", false),
        };
        ItemHeader {
            key: key.clone(),
            reference: match key.kind {
                ItemKind::Issue => format!("#{}", key.number),
                ItemKind::PullRequest => format!("!{}", key.number),
            },
            title: text(row, "title"),
            state_label: state_label.to_string(),
            open,
            author: actor_of(row.get("author")),
            body: Markup::new(text(row, "description"), MarkupFlavor::GitlabMd),
            created_at: text(row, "created_at"),
            web_url: text(row, "web_url"),
            labels: row["labels"]
                .as_array()
                .into_iter()
                .flatten()
                .filter_map(|l| l.as_str().or_else(|| l["name"].as_str()))
                .map(str::to_string)
                .collect(),
        }
    }

    async fn discussions_page(
        &self,
        project: &str,
        key: &ItemKey,
        page: u32,
    ) -> Result<(Vec<Value>, HttpResponse)> {
        let url = self.item_url(
            project,
            key,
            &format!("/discussions?per_page=100&page={page}"),
        );
        let response = self.transport.send(self.get(url)).await?;
        response.error_for_status("reading the discussions of a GitLab issue or merge request")?;
        let rows: Vec<Value> = response.json()?;
        Ok((rows, response))
    }

    /// Newest window of discussions (the last pages first) or the window
    /// ending at page `cursor`, the next older page and a count hint.
    async fn discussions_window(
        &self,
        project: &str,
        key: &ItemKey,
        cursor: Option<u32>,
    ) -> Result<(Vec<Value>, Option<u32>, Option<u32>)> {
        let mut first: Option<(Vec<Value>, HttpResponse)> = None;
        let (high, last_known) = match cursor {
            Some(c) => (c, None),
            None => {
                let page_one = self.discussions_page(project, key, 1).await?;
                let last = total_pages(&page_one.1);
                first = Some(page_one);
                (last, Some(last))
            }
        };
        let low = high.saturating_sub(PAGES_PER_WINDOW - 1).max(1);
        let mut rows: Vec<Value> = Vec::new();
        let mut last_len = 0usize;
        for page in low..=high {
            let page_rows = match first.take() {
                Some((r, _)) if page == 1 => r,
                kept => {
                    first = kept;
                    self.discussions_page(project, key, page).await?.0
                }
            };
            if page == high {
                last_len = page_rows.len();
            }
            rows.extend(page_rows);
        }
        let hint = last_known.map(|last| (last - 1) * 100 + last_len as u32);
        Ok((rows, (low > 1).then(|| low - 1), hint))
    }

    pub(super) async fn conv_timeline(
        &self,
        project: &str,
        key: &ItemKey,
        older: Option<&PageCursor>,
        opts: &TimelineOpts,
    ) -> Result<TimelinePage> {
        let cursor = match older {
            Some(c) => Some(
                parse_cursor(c)
                    .ok_or_else(|| invalid("reading a conversation", "bad page cursor"))?,
            ),
            None => None,
        };
        let row = self.conv_item_row(project, key).await?;
        let header = self.header_of(&row, key);
        let head_sha = text(&row, "sha");
        let (discussions, next, total_hint) = self.discussions_window(project, key, cursor).await?;
        let mut entries = Vec::new();
        for d in discussions {
            self.entries_of_discussion(&d, &header.web_url, &head_sha, opts, &mut entries);
        }
        Ok(TimelinePage {
            header,
            entries,
            older: next.map(|p| PageCursor(p.to_string())),
            total_hint,
        })
    }

    fn entries_of_discussion(
        &self,
        d: &Value,
        item_url: &str,
        head_sha: &str,
        opts: &TimelineOpts,
        out: &mut Vec<TimelineEntry>,
    ) {
        let id = text(d, "id");
        let notes = d["notes"].as_array().cloned().unwrap_or_default();
        let Some(first) = notes.first() else {
            return;
        };
        if first["system"].as_bool() == Some(true) {
            if opts.events {
                for n in notes.iter().filter(|n| n["system"].as_bool() == Some(true)) {
                    out.push(TimelineEntry::Event(Event {
                        id: n["id"].as_u64().unwrap_or_default().to_string(),
                        actor: Some(actor_of(n.get("author"))),
                        at: text(n, "created_at"),
                        kind: system_kind(&text(n, "body")),
                    }));
                }
            }
            return;
        }
        let first_id = first["id"].as_u64().unwrap_or_default().to_string();
        let comments: Vec<Comment> = notes
            .iter()
            .filter(|n| n["system"].as_bool() != Some(true))
            .enumerate()
            .map(|(i, n)| note_comment(n, Some(&id), (i > 0).then(|| first_id.clone()), item_url))
            .collect();
        if first.get("position").is_some_and(|p| !p.is_null()) {
            let parsed = serde_json::from_value::<Discussion>(d.clone())
                .ok()
                .and_then(|disc| thread_of(disc, head_sha));
            if let Some(t) = parsed {
                out.push(TimelineEntry::Thread(CodeThread {
                    id: t.id,
                    path: t.path,
                    line: t.line,
                    original_line: t.original_line,
                    side: t.side,
                    start_line: t.start_line,
                    resolved: t.resolved,
                    outdated: t.outdated,
                    resolvable: t.resolvable,
                    diff_hunk: None,
                    comments,
                }));
                return;
            }
        }
        out.extend(comments.into_iter().map(TimelineEntry::Comment));
    }

    pub(super) async fn conv_post(
        &self,
        project: &str,
        key: &ItemKey,
        body: &str,
        internal: bool,
    ) -> Result<Comment> {
        let label = match key.kind {
            ItemKind::PullRequest => "commenting on a merge request",
            ItemKind::Issue => "commenting on an issue",
        };
        check_body(label, body)?;
        let mut payload = json!({ "body": body });
        if internal {
            payload["internal"] = json!(true);
        }
        let response = self
            .transport
            .send(self.post(self.item_url(project, key, "/notes"), &payload))
            .await?;
        response.error_for_status("commenting on a GitLab issue or merge request")?;
        Ok(note_comment(&response.json::<Value>()?, None, None, ""))
    }

    pub(super) async fn conv_reply(
        &self,
        project: &str,
        key: &ItemKey,
        discussion_id: &str,
        body: &str,
    ) -> Result<Comment> {
        check_body("replying to a discussion", body)?;
        let url = self.item_url(
            project,
            key,
            &format!("/discussions/{}/notes", encode_component(discussion_id)),
        );
        let response = self
            .transport
            .send(self.post(url, &json!({ "body": body })))
            .await?;
        response.error_for_status("replying to a GitLab discussion")?;
        Ok(note_comment(
            &response.json::<Value>()?,
            Some(discussion_id),
            None,
            "",
        ))
    }

    pub(super) async fn conv_edit(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
        body: &str,
    ) -> Result<Comment> {
        check_body("editing a comment", body)?;
        let url = self.item_url(
            project,
            key,
            &format!("/notes/{}", encode_component(comment_id)),
        );
        let response = self
            .transport
            .send(self.put(url, &json!({ "body": body })))
            .await?;
        response.error_for_status("editing a GitLab note")?;
        Ok(note_comment(&response.json::<Value>()?, None, None, ""))
    }

    pub(super) async fn conv_delete(
        &self,
        project: &str,
        key: &ItemKey,
        comment_id: &str,
    ) -> Result<()> {
        let url = self.item_url(
            project,
            key,
            &format!("/notes/{}", encode_component(comment_id)),
        );
        let response = self.transport.send(self.delete(url)).await?;
        response.error_for_status("deleting a GitLab note")
    }

    /// Who a post is made as: the token's own user (`GET /user`); a project
    /// or group access token answers `bot: true` (or is named like one).
    pub(super) async fn conv_identity(&self, project: &str) -> Result<PostingIdentity> {
        let id = &self.integration.id;
        let source = if self.scopes.acts_as_bot(id, project) {
            TokenSource::RepoTokenCommand
        } else {
            TokenSource::Profile { id: id.clone() }
        };
        let url = format!("{}/user", self.integration.api_base());
        let response = self.transport.send(self.get(url)).await?;
        response.error_for_status("reading the GitLab token's user")?;
        let user: Value = response.json()?;
        let login = text(&user, "username");
        if login.is_empty() {
            return Err(Error::Decode("no username in the GitLab user".to_string()));
        }
        let kind = if user["bot"].as_bool() == Some(true) || is_bot_login(&login) {
            IdentityKind::ProjectOrGroupBot
        } else {
            IdentityKind::PersonalToken
        };
        Ok(PostingIdentity::new(Some(login), kind, source))
    }
}

fn check_body(what: &str, body: &str) -> Result<()> {
    if body.trim().is_empty() {
        return Err(invalid(what, "the comment is empty"));
    }
    Ok(())
}