gitpoulpi-hosting 1.2.0

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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
//! In-app review on Gitea and Forgejo.
//!
//! Gitea has no thread object: a review holds code comments
//! (`pulls/{n}/reviews/{id}/comments`) and the web UI groups the ones on the
//! same line into a conversation. This module does the same, so a thread is
//! the comments of one `(path, side, line)` and its id is
//! `"{root comment id}|{n|o}|{line}|{path}"`: the root id is what resolve and
//! reply take, the rest lets a reply fall back to a one-comment review on
//! servers without the reply endpoint (Forgejo 11).
//!
//! A comment's position: `position` is the line in the new file, and
//! `original_position` the line in the old one for a comment on a removed
//! line (observed on both servers: new-side `(2, 0)`, old-side `(0, 2)`).
//! The API has no "outdated" flag; a comment written on another commit than
//! the head is reported outdated, so it is listed under its file instead of
//! being drawn on a line that may have moved.

use std::collections::BTreeMap;

use serde::Deserialize;
use serde_json::{json, Value};

use super::{GiteaClient, Login, PullRow};
use crate::model::ChecksState;
use crate::review::model::{
    DiffSide, FileStatus, MergeState, PullRequestDetail, ReviewCapabilities, ReviewComment,
    ReviewEvent, ReviewFile, ReviewOutcome, ReviewState, ReviewSubmission, ReviewThread,
    SubmittedReview,
};
use crate::review::{invalid, validate_submission};
use crate::{Error, Result};
use gitpoulpi_config::IntegrationKind;

#[derive(Deserialize)]
struct IssueComment {
    id: u64,
    #[serde(default)]
    body: String,
    #[serde(default)]
    created_at: String,
    user: Option<Login>,
    html_url: Option<String>,
}

impl IssueComment {
    fn into_comment(self) -> ReviewComment {
        ReviewComment {
            id: self.id.to_string(),
            author: self.user.map(|u| u.login).unwrap_or_default(),
            body: self.body,
            created_at: self.created_at,
            in_reply_to: None,
            web_url: self.html_url.filter(|u| !u.is_empty()),
        }
    }
}

#[derive(Deserialize)]
struct FileRow {
    filename: String,
    #[serde(default)]
    status: String,
    #[serde(default)]
    additions: u32,
    #[serde(default)]
    deletions: u32,
    previous_filename: Option<String>,
}

#[derive(Deserialize)]
struct ReviewRow {
    id: u64,
    #[serde(default)]
    comments_count: u32,
    #[serde(default)]
    state: String,
    #[serde(default)]
    body: String,
    #[serde(default)]
    dismissed: bool,
    #[serde(default)]
    submitted_at: String,
    user: Option<Login>,
}

#[derive(Deserialize)]
struct CodeComment {
    id: u64,
    #[serde(default)]
    body: String,
    #[serde(default)]
    created_at: String,
    user: Option<Login>,
    resolver: Option<Login>,
    #[serde(default)]
    path: String,
    #[serde(default)]
    commit_id: String,
    #[serde(default)]
    position: u32,
    #[serde(default)]
    original_position: u32,
    html_url: Option<String>,
}

impl CodeComment {
    fn side_and_line(&self) -> Option<(DiffSide, u32)> {
        if self.position > 0 {
            Some((DiffSide::New, self.position))
        } else if self.original_position > 0 {
            Some((DiffSide::Old, self.original_position))
        } else {
            None
        }
    }

    fn into_comment(self) -> ReviewComment {
        ReviewComment {
            id: self.id.to_string(),
            author: self.user.map(|u| u.login).unwrap_or_default(),
            body: self.body,
            created_at: self.created_at,
            in_reply_to: None,
            web_url: self.html_url.filter(|u| !u.is_empty()),
        }
    }
}

fn file_status(text: &str) -> FileStatus {
    match text {
        "added" | "copied" => FileStatus::Added,
        "deleted" | "removed" => FileStatus::Removed,
        "renamed" => FileStatus::Renamed,
        _ => FileStatus::Modified,
    }
}

/// `"{root}|{n|o}|{line}|{path}"`, see the module docs.
fn thread_id(root: u64, side: DiffSide, line: u32, path: &str) -> String {
    let side = if side == DiffSide::Old { 'o' } else { 'n' };
    format!("{root}|{side}|{line}|{path}")
}

struct ThreadRef {
    root: u64,
    side: DiffSide,
    line: u32,
    path: String,
}

fn parse_thread_id(id: &str) -> Option<ThreadRef> {
    let mut parts = id.splitn(4, '|');
    let root = parts.next()?.parse().ok()?;
    let side = match parts.next()? {
        "o" => DiffSide::Old,
        "n" => DiffSide::New,
        _ => return None,
    };
    let line = parts.next()?.parse().ok()?;
    let path = parts.next()?.to_string();
    Some(ThreadRef {
        root,
        side,
        line,
        path,
    })
}

fn comment_position(side: DiffSide, line: u32) -> (&'static str, u32) {
    match side {
        DiffSide::New => ("new_position", line),
        DiffSide::Old => ("old_position", line),
    }
}

impl GiteaClient {
    /// Gitea can resolve a conversation, Forgejo (11) cannot; neither has
    /// multi-line comments.
    pub(super) fn review_caps(&self) -> ReviewCapabilities {
        ReviewCapabilities {
            review: true,
            approve: true,
            request_changes: true,
            line_ranges: false,
            resolve: self.integration.kind == IntegrationKind::Gitea,
        }
    }

    pub(super) async fn review_detail(
        &self,
        project: &str,
        number: u64,
    ) -> Result<PullRequestDetail> {
        let raw: Value = self
            .get_json(
                self.repo_url(project, &format!("pulls/{number}")),
                &format!("reading the {} pull request", self.label()),
            )
            .await?;
        let text = |key: &str| -> String {
            raw.get(key)
                .and_then(Value::as_str)
                .unwrap_or_default()
                .to_string()
        };
        let count = |key: &str| raw.get(key).and_then(Value::as_u64).map(|n| n as u32);
        let row: PullRow =
            serde_json::from_value(raw.clone()).map_err(|e| Error::Decode(e.to_string()))?;
        let head_sha = row.head.sha.clone();
        let base_sha = row.base.sha.clone();
        let mergeable = row.mergeable;
        let summary = row.into_summary(None);
        // A work-in-progress request reports `mergeable: false` whatever its
        // branches do, so a draft can only be said to be blocked.
        let merge_state = match mergeable {
            Some(_) if summary.state == crate::model::PullRequestState::Draft => {
                MergeState::Blocked
            }
            Some(false) => MergeState::Conflicting,
            Some(true) => MergeState::Clean,
            None => MergeState::Unknown,
        };
        // A missing badge beats a failed review screen.
        let checks: Option<ChecksState> = if head_sha.is_empty() {
            None
        } else {
            self.commit_checks(project, &head_sha).await.ok().flatten()
        };
        Ok(PullRequestDetail {
            patch_sets: Vec::new(),
            shown_patch_set: None,
            vote_labels: Vec::new(),
            can_submit: false,
            can_abandon: false,
            summary,
            body: text("body"),
            head_sha,
            base_sha,
            merge_state,
            checks,
            created_at: text("created_at"),
            additions: count("additions"),
            deletions: count("deletions"),
            changed_files: count("changed_files"),
            capabilities: self.review_caps(),
        })
    }

    pub(super) async fn review_files(&self, project: &str, number: u64) -> Result<Vec<ReviewFile>> {
        let rows: Vec<FileRow> = self
            .pages(
                &self.repo_url(project, &format!("pulls/{number}/files")),
                &format!("listing the files of a {} pull request", self.label()),
            )
            .await?;
        Ok(rows
            .into_iter()
            .map(|f| ReviewFile {
                path: f.filename,
                previous_path: f.previous_filename.filter(|p| !p.is_empty()),
                status: file_status(&f.status),
                additions: f.additions,
                deletions: f.deletions,
            })
            .collect())
    }

    pub(super) async fn review_threads(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<ReviewThread>> {
        let reviews: Vec<ReviewRow> = self
            .pages(
                &self.repo_url(project, &format!("pulls/{number}/reviews")),
                &format!("listing the reviews of a {} pull request", self.label()),
            )
            .await?;
        let mut comments: Vec<CodeComment> = Vec::new();
        for review in reviews.into_iter().filter(|r| r.comments_count > 0) {
            let rows: Vec<CodeComment> = self
                .get_json(
                    self.repo_url(
                        project,
                        &format!("pulls/{number}/reviews/{}/comments", review.id),
                    ),
                    "reading the comments of a review",
                )
                .await?;
            comments.extend(rows);
        }
        if comments.is_empty() {
            return Ok(Vec::new());
        }
        let head: PullRow = self
            .get_json(
                self.repo_url(project, &format!("pulls/{number}")),
                "reading the head of a pull request",
            )
            .await?;
        let head_sha = head.head.sha;
        comments.sort_by_key(|c| c.id);
        // Forgejo (11) has no resolve endpoint: its threads cannot be resolved.
        let resolvable = self.review_caps().resolve;

        // One thread per (path, side, line), ordered by its root comment.
        let mut groups: BTreeMap<(String, bool, u32), Vec<CodeComment>> = BTreeMap::new();
        let mut unanchored: Vec<CodeComment> = Vec::new();
        for c in comments {
            match c.side_and_line() {
                Some((side, line)) => groups
                    .entry((c.path.clone(), side == DiffSide::Old, line))
                    .or_default()
                    .push(c),
                None => unanchored.push(c),
            }
        }
        let mut threads: Vec<ReviewThread> = groups
            .into_iter()
            .map(|((path, old, line), group)| {
                let side = if old { DiffSide::Old } else { DiffSide::New };
                let root = &group[0];
                let outdated = !root.commit_id.is_empty() && root.commit_id != head_sha;
                ReviewThread {
                    id: thread_id(root.id, side, line, &path),
                    resolved: group.iter().any(|c| c.resolver.is_some()),
                    outdated,
                    line: (!outdated).then_some(line),
                    original_line: Some(line),
                    side,
                    start_line: None,
                    resolvable,
                    comments: group.into_iter().map(CodeComment::into_comment).collect(),
                    path,
                }
            })
            .collect();
        // A comment with no line at all (its code is gone): one outdated
        // thread each, nothing to draw inline.
        for c in unanchored {
            threads.push(ReviewThread {
                id: thread_id(c.id, DiffSide::New, 0, &c.path),
                path: c.path.clone(),
                line: None,
                original_line: None,
                side: DiffSide::New,
                start_line: None,
                resolved: c.resolver.is_some(),
                outdated: true,
                resolvable,
                comments: vec![c.into_comment()],
            });
        }
        threads.sort_by_key(|t| {
            t.comments
                .first()
                .and_then(|c| c.id.parse::<u64>().ok())
                .unwrap_or(0)
        });
        Ok(threads)
    }

    /// The submitted reviews, oldest first. A `COMMENT` review without a
    /// summary only carries code comments (they are the threads), and
    /// `REQUEST_REVIEW` / `PENDING` rows are requests and drafts, not reviews.
    pub(super) async fn review_list_reviews(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<SubmittedReview>> {
        let rows: Vec<ReviewRow> = self
            .pages(
                &self.repo_url(project, &format!("pulls/{number}/reviews")),
                &format!("listing the reviews of a {} pull request", self.label()),
            )
            .await?;
        Ok(rows
            .into_iter()
            .filter_map(|r| {
                let state = if r.dismissed {
                    ReviewState::Dismissed
                } else {
                    match r.state.to_ascii_uppercase().as_str() {
                        "APPROVED" => ReviewState::Approved,
                        "REQUEST_CHANGES" => ReviewState::ChangesRequested,
                        "COMMENT" if !r.body.trim().is_empty() => ReviewState::Commented,
                        _ => return None,
                    }
                };
                Some(SubmittedReview {
                    id: r.id.to_string(),
                    author: r.user.map(|u| u.login).unwrap_or_default(),
                    state,
                    body: r.body,
                    submitted_at: r.submitted_at,
                })
            })
            .collect())
    }

    pub(super) async fn review_conversation(
        &self,
        project: &str,
        number: u64,
    ) -> Result<Vec<ReviewComment>> {
        let rows: Vec<IssueComment> = self
            .pages(
                &self.repo_url(project, &format!("issues/{number}/comments")),
                &format!("listing the comments of a {} pull request", self.label()),
            )
            .await?;
        Ok(rows.into_iter().map(IssueComment::into_comment).collect())
    }

    pub(super) async fn review_add_comment(
        &self,
        project: &str,
        number: u64,
        body: &str,
    ) -> Result<ReviewComment> {
        if body.trim().is_empty() {
            return Err(invalid(
                "commenting on a pull request",
                "the comment is empty",
            ));
        }
        let url = self.repo_url(project, &format!("issues/{number}/comments"));
        let response = self
            .transport
            .send(self.post(url, &json!({ "body": body })))
            .await?;
        response.error_for_status("commenting on a pull request")?;
        Ok(response.json::<IssueComment>()?.into_comment())
    }

    pub(super) async fn review_reply(
        &self,
        project: &str,
        number: u64,
        thread_id: &str,
        body: &str,
    ) -> Result<ReviewComment> {
        if body.trim().is_empty() {
            return Err(invalid("replying to a thread", "the reply is empty"));
        }
        let thread = parse_thread_id(thread_id)
            .ok_or_else(|| invalid("replying to a thread", "not a thread of this host"))?;
        let url = self.repo_url(
            project,
            &format!("pulls/{number}/comments/{}/replies", thread.root),
        );
        let response = self
            .transport
            .send(self.post(url, &json!({ "body": body })))
            .await?;
        // A server without the reply endpoint (Forgejo 11): say it on the
        // same line in a review of its own, which the UI groups the same.
        if !matches!(response.status, 404 | 405) {
            response.error_for_status("replying to a review thread")?;
            return Ok(response.json::<CodeComment>()?.into_comment());
        }
        let (key, line) = comment_position(thread.side, thread.line);
        let review = json!({
            "event": "COMMENT",
            "comments": [{ "path": thread.path, "body": body, key: line }],
        });
        let url = self.repo_url(project, &format!("pulls/{number}/reviews"));
        let response = self.transport.send(self.post(url, &review)).await?;
        response.error_for_status("replying to a review thread")?;
        let posted: ReviewRow = response.json()?;
        let rows: Vec<CodeComment> = self
            .get_json(
                self.repo_url(
                    project,
                    &format!("pulls/{number}/reviews/{}/comments", posted.id),
                ),
                "reading the posted reply",
            )
            .await?;
        rows.into_iter()
            .next()
            .map(CodeComment::into_comment)
            .ok_or_else(|| Error::Decode("the reply was posted but not returned".to_string()))
    }

    pub(super) async fn review_resolve(
        &self,
        project: &str,
        thread_id: &str,
        resolved: bool,
    ) -> Result<()> {
        let thread = parse_thread_id(thread_id)
            .ok_or_else(|| invalid("resolving a thread", "not a thread of this host"))?;
        let verb = if resolved { "resolve" } else { "unresolve" };
        let url = self.repo_url(project, &format!("pulls/comments/{}/{verb}", thread.root));
        let response = self.transport.send(self.post(url, &json!({}))).await?;
        // The endpoint is newer than the rest of the API.
        if matches!(response.status, 404 | 405) {
            return Err(Error::Unsupported(format!(
                "resolving threads on {}",
                self.label()
            )));
        }
        response.error_for_status("resolving a review thread")
    }

    pub(super) async fn review_submit(
        &self,
        project: &str,
        number: u64,
        review: &ReviewSubmission,
    ) -> Result<ReviewOutcome> {
        validate_submission(self.review_caps(), review)?;
        let event = match review.event {
            ReviewEvent::Approve => "APPROVED",
            ReviewEvent::RequestChanges => "REQUEST_CHANGES",
            ReviewEvent::Comment => "COMMENT",
        };
        let comments: Vec<Value> = review
            .comments
            .iter()
            .map(|c| {
                let (key, line) = comment_position(c.side, c.line);
                json!({ "path": c.path, "body": c.body, key: line })
            })
            .collect();
        let mut body = json!({ "event": event, "comments": comments });
        if !review.body.trim().is_empty() {
            body["body"] = json!(review.body);
        }
        if let Some(sha) = review.head_sha.as_deref().filter(|s| !s.is_empty()) {
            body["commit_id"] = json!(sha);
        }
        let url = self.repo_url(project, &format!("pulls/{number}/reviews"));
        let response = self.transport.send(self.post(url, &body)).await?;
        response.error_for_status("submitting a review")?;
        let posted: Value = response.json()?;
        Ok(ReviewOutcome {
            event: review.event,
            comments: review.comments.len(),
            review_id: posted
                .get("id")
                .and_then(Value::as_u64)
                .map(|i| i.to_string()),
        })
    }
}