gitpoulpi-hosting 1.0.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
//! Gerrit JSON to the host-agnostic model.

use std::collections::BTreeMap;

use serde::Deserialize;
use serde_json::Value;

use crate::model::{ChecksState, PullRequestState, PullRequestSummary};
use crate::project::HostedProject;
use crate::review::model::{
    FileStatus, PatchSet, ReviewFile, ReviewState, SubmittedReview, VoteLabel, VoteValue,
};

/// `)]}'` guards every JSON answer of Gerrit against script inclusion.
pub(super) const XSSI_PREFIX: &str = ")]}'";

/// The body without the guard line.
pub(super) fn strip_prefix(body: &str) -> &str {
    let body = body.trim_start_matches('\u{feff}');
    match body.strip_prefix(XSSI_PREFIX) {
        Some(rest) => rest.trim_start_matches(['\n', '\r']),
        None => body,
    }
}

/// `2026-10-15 06:29:50.000000000` (UTC) as ISO-8601.
pub(super) fn iso(stamp: &str) -> String {
    let stamp = stamp.trim();
    if stamp.is_empty() {
        return String::new();
    }
    let (date, time) = stamp.split_once(' ').unwrap_or((stamp, "00:00:00"));
    let time = time.split('.').next().unwrap_or(time);
    format!("{date}T{time}Z")
}

pub use crate::review::{change_ref, patch_set_of_ref};

#[derive(Deserialize, Default, Clone)]
pub(super) struct Account {
    #[serde(default)]
    pub _account_id: u64,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub username: Option<String>,
    #[serde(default)]
    pub email: Option<String>,
}

impl Account {
    /// The handle: user name, else the name, else the id.
    pub fn handle(&self) -> String {
        self.username
            .clone()
            .filter(|n| !n.is_empty())
            .or_else(|| self.name.clone().filter(|n| !n.is_empty()))
            .or_else(|| self.email.clone().filter(|n| !n.is_empty()))
            .unwrap_or_else(|| self._account_id.to_string())
    }

    pub fn display(&self) -> String {
        self.name
            .clone()
            .filter(|n| !n.is_empty())
            .unwrap_or_else(|| self.handle())
    }
}

#[derive(Deserialize, Default)]
pub(super) struct CommitParent {
    #[serde(default)]
    pub commit: String,
}

#[derive(Deserialize, Default)]
pub(super) struct Commit {
    #[serde(default)]
    pub parents: Vec<CommitParent>,
    #[serde(default)]
    pub message: String,
}

#[derive(Deserialize, Default)]
pub(super) struct Revision {
    #[serde(default)]
    pub _number: u32,
    #[serde(default)]
    pub created: String,
    #[serde(default)]
    pub commit: Option<Commit>,
}

#[derive(Deserialize, Default)]
pub(super) struct LabelEntry {
    #[serde(default)]
    pub value: Option<i32>,
    #[serde(default)]
    pub _account_id: u64,
    #[serde(default)]
    pub username: Option<String>,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub date: Option<String>,
}

#[derive(Deserialize, Default)]
pub(super) struct LabelInfo {
    /// `"-2": "This shall not be submitted"` (keys may carry a leading space or `+`).
    #[serde(default)]
    pub values: BTreeMap<String, String>,
    #[serde(default)]
    pub all: Vec<LabelEntry>,
    #[serde(default)]
    pub approved: Option<serde_json::Value>,
    #[serde(default)]
    pub rejected: Option<serde_json::Value>,
    #[serde(default)]
    pub default_value: i32,
}

#[derive(Deserialize, Default)]
pub(super) struct Message {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub author: Option<Account>,
    #[serde(default)]
    pub date: String,
    #[serde(default)]
    pub message: String,
    #[serde(default)]
    pub _revision_number: u32,
    /// `autogenerated:gerrit:newPatchSet`, `...:merged`... on system messages.
    #[serde(default)]
    pub tag: Option<String>,
}

#[derive(Deserialize, Default)]
pub(super) struct Reviewers {
    #[serde(rename = "REVIEWER", default)]
    pub reviewer: Vec<Account>,
}

#[derive(Deserialize, Default)]
pub(super) struct ChangeInfo {
    #[serde(default)]
    pub _number: u64,
    #[serde(default)]
    pub project: String,
    #[serde(default)]
    pub branch: String,
    #[serde(default)]
    pub subject: String,
    #[serde(default)]
    pub status: String,
    #[serde(default)]
    pub work_in_progress: bool,
    #[serde(default)]
    pub updated: String,
    #[serde(default)]
    pub created: String,
    #[serde(default)]
    pub owner: Option<Account>,
    #[serde(default)]
    pub submittable: Option<bool>,
    #[serde(default)]
    pub mergeable: Option<bool>,
    #[serde(default)]
    pub insertions: Option<u32>,
    #[serde(default)]
    pub deletions: Option<u32>,
    #[serde(default)]
    pub current_revision: Option<String>,
    #[serde(default)]
    pub revisions: BTreeMap<String, Revision>,
    #[serde(default)]
    pub labels: BTreeMap<String, LabelInfo>,
    #[serde(default)]
    pub permitted_labels: BTreeMap<String, Vec<String>>,
    #[serde(default)]
    pub reviewers: Reviewers,
    #[serde(default)]
    pub messages: Vec<Message>,
    #[serde(default)]
    pub hashtags: Vec<String>,
    /// `o=CHANGE_ACTIONS`: what the user may do to the change (`abandon`,
    /// `wip`, ...), only the names matter.
    #[serde(default)]
    pub actions: BTreeMap<String, Value>,
    #[serde(default)]
    pub _more_changes: bool,
}

impl ChangeInfo {
    /// The newest patch set number.
    pub fn latest_patch_set(&self) -> u32 {
        self.revisions
            .values()
            .map(|r| r._number)
            .max()
            .unwrap_or(1)
    }

    /// The revision (sha, data) of patch set `number`; `None` asks for the
    /// current one.
    pub fn revision(&self, number: Option<u32>) -> Option<(&String, &Revision)> {
        match number {
            Some(n) => self.revisions.iter().find(|(_, r)| r._number == n),
            None => self
                .current_revision
                .as_ref()
                .and_then(|sha| self.revisions.get_key_value(sha))
                .or_else(|| self.revisions.iter().max_by_key(|(_, r)| r._number)),
        }
    }

    /// The server says the change can be submitted now.
    pub fn can_submit(&self) -> bool {
        self.status == "NEW" && !self.work_in_progress && self.submittable == Some(true)
    }

    /// The change is open and the user is offered the abandon action.
    pub fn can_abandon(&self) -> bool {
        self.status == "NEW" && self.actions.contains_key("abandon")
    }

    /// The `Verified` label rolled up as the request's checks.
    pub fn checks(&self) -> Option<ChecksState> {
        let verified = self.labels.get("Verified")?;
        if verified.rejected.is_some() {
            Some(ChecksState::Failing)
        } else if verified.approved.is_some() {
            Some(ChecksState::Passing)
        } else {
            Some(ChecksState::Pending)
        }
    }

    pub fn state(&self) -> PullRequestState {
        match self.status.as_str() {
            "MERGED" => PullRequestState::Merged,
            "ABANDONED" => PullRequestState::Closed,
            _ if self.work_in_progress => PullRequestState::Draft,
            _ => PullRequestState::Open,
        }
    }

    pub fn patch_sets(&self) -> Vec<PatchSet> {
        let mut sets: Vec<PatchSet> = self
            .revisions
            .iter()
            .map(|(sha, r)| PatchSet {
                number: r._number,
                sha: sha.clone(),
                created_at: iso(&r.created),
            })
            .collect();
        sets.sort_by_key(|p| p.number);
        sets
    }

    /// The labels this account may vote on, with the values the server
    /// permits for this change (`permitted_labels`), in the label's own
    /// order.
    pub fn vote_labels(&self, me: u64) -> Vec<VoteLabel> {
        let mut out = Vec::new();
        for (name, allowed) in &self.permitted_labels {
            let Some(info) = self.labels.get(name) else {
                continue;
            };
            let mut values: Vec<VoteValue> = allowed
                .iter()
                .filter_map(|raw| {
                    let value = parse_vote(raw)?;
                    let text = info
                        .values
                        .iter()
                        .find(|(k, _)| parse_vote(k) == Some(value))
                        .map(|(_, v)| v.clone())
                        .unwrap_or_default();
                    Some(VoteValue { value, text })
                })
                .collect();
            if values.is_empty() {
                continue;
            }
            values.sort_by_key(|v| v.value);
            let current = info
                .all
                .iter()
                .find(|e| e._account_id == me)
                .and_then(|e| e.value)
                .unwrap_or(info.default_value);
            out.push(VoteLabel {
                name: name.clone(),
                values,
                current,
                default: info.default_value,
            });
        }
        // Code-Review first, then the rest alphabetically.
        out.sort_by(|a, b| {
            (a.name != "Code-Review")
                .cmp(&(b.name != "Code-Review"))
                .then_with(|| a.name.cmp(&b.name))
        });
        out
    }

    /// The votes on Code-Review, as verdicts.
    pub fn code_review_votes(&self) -> Vec<SubmittedReview> {
        let Some(label) = self.labels.get("Code-Review") else {
            return Vec::new();
        };
        label
            .all
            .iter()
            .filter_map(|e| {
                let value = e.value.filter(|v| *v != 0)?;
                Some(SubmittedReview {
                    id: format!("{}-{}", e._account_id, value),
                    author: e
                        .username
                        .clone()
                        .or_else(|| e.name.clone())
                        .unwrap_or_default(),
                    state: if value > 0 {
                        ReviewState::Approved
                    } else {
                        ReviewState::ChangesRequested
                    },
                    body: format!("Code-Review {value:+}"),
                    submitted_at: iso(e.date.as_deref().unwrap_or("")),
                })
            })
            .collect()
    }

    pub fn summary(&self, site: &str, me: Option<&str>) -> PullRequestSummary {
        let (_, latest) = self.revision(None).unzip();
        let ps = latest.map(|r| r._number).unwrap_or(1);
        let owner = self.owner.clone().unwrap_or_default();
        let reviewers: Vec<String> = self
            .reviewers
            .reviewer
            .iter()
            .map(Account::handle)
            .collect();
        PullRequestSummary {
            number: self._number,
            iid_label: format!("#{}", self._number),
            title: self.subject.clone(),
            state: self.state(),
            source_branch: change_ref(self._number, ps),
            target_branch: self.branch.clone(),
            source_project: None,
            author: owner.handle(),
            web_url: change_url(site, &self.project, self._number),
            updated_at: iso(&self.updated),
            review_requested: me.is_some_and(|m| reviewers.iter().any(|r| r == m))
                && owner.handle() != me.unwrap_or_default(),
            assignees: Vec::new(),
            reviewers,
            checks: self.checks(),
            has_conflicts: self.mergeable.map(|m| !m),
        }
    }
}

/// `-2`, `+1`, ` 0`, `1`.
pub(super) fn parse_vote(raw: &str) -> Option<i32> {
    raw.trim().trim_start_matches('+').parse().ok()
}

pub(super) fn change_url(site: &str, project: &str, number: u64) -> String {
    format!("{}/c/{project}/+/{number}", site.trim_end_matches('/'))
}

#[derive(Deserialize)]
pub(super) struct FileInfo {
    #[serde(default)]
    pub status: Option<String>,
    #[serde(default)]
    pub old_path: Option<String>,
    #[serde(default)]
    pub lines_inserted: u32,
    #[serde(default)]
    pub lines_deleted: u32,
}

pub(super) fn map_files(files: BTreeMap<String, FileInfo>) -> Vec<ReviewFile> {
    files
        .into_iter()
        .filter(|(path, _)| !path.starts_with('/'))
        .map(|(path, f)| {
            let status = match f.status.as_deref() {
                Some("A") | Some("C") => FileStatus::Added,
                Some("D") => FileStatus::Removed,
                Some("R") => FileStatus::Renamed,
                _ => FileStatus::Modified,
            };
            ReviewFile {
                path,
                previous_path: f.old_path.filter(|p| !p.is_empty()),
                status,
                additions: f.lines_inserted,
                deletions: f.lines_deleted,
            }
        })
        .collect()
}

#[derive(Deserialize, Default, Clone)]
pub(super) struct Range {
    #[serde(default)]
    pub start_line: u32,
    #[serde(default)]
    pub end_line: u32,
}

#[derive(Deserialize, Default, Clone)]
pub(super) struct CommentInfo {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub patch_set: u32,
    #[serde(default)]
    pub line: Option<u32>,
    #[serde(default)]
    pub range: Option<Range>,
    #[serde(default)]
    pub side: Option<String>,
    #[serde(default)]
    pub in_reply_to: Option<String>,
    #[serde(default)]
    pub message: String,
    #[serde(default)]
    pub updated: String,
    #[serde(default)]
    pub author: Option<Account>,
    #[serde(default)]
    pub unresolved: Option<bool>,
    /// Filled in by the reader: the file the comment was listed under.
    #[serde(skip)]
    pub path: String,
    /// Filled in by the reader: a draft of the current user.
    #[serde(skip)]
    pub draft: bool,
}

pub(super) fn hosted_project(
    site: &str,
    integration_id: &str,
    name: &str,
    description: Option<String>,
) -> HostedProject {
    let base = site.trim_end_matches('/');
    HostedProject {
        integration_id: integration_id.to_string(),
        id: name.to_string(),
        name: name.rsplit('/').next().unwrap_or(name).to_string(),
        full_name: name.to_string(),
        description: description.filter(|d| !d.is_empty()),
        default_branch: None,
        web_url: Some(format!("{base}/admin/repos/{name}")),
        ssh_url: None,
        https_url: Some(format!("{base}/{name}")),
        private: false,
    }
}