gitpoulpi_hosting/review/model.rs
1//! Host-agnostic shapes of an in-app code review.
2//!
3//! # How a position maps onto the diff
4//!
5//! The diff is not fetched from the host: GitPoulpi fetches the request's
6//! head into `refs/gitpoulpi/pr/<n>` and diffs `merge-base(base, head)` to
7//! that head with its own diff code. Every comment position below is
8//! expressed in that diff's terms:
9//!
10//! * [`DiffSide::New`] and `line`: the 1-based line number in the file as
11//! it is at the head commit (the right-hand column of the diff);
12//! * [`DiffSide::Old`] and `line`: the 1-based line number in the file as
13//! it is at the merge base (the left-hand column);
14//! * an unchanged context line exists on both sides; either number
15//! addresses it (GitHub accepts the new side, GitLab wants both - see
16//! [`PendingComment::other_line`]);
17//! * a thread whose code moved on since it was written is `outdated`: its
18//! `line` is `None` (nothing to draw inline) and `original_line` tells
19//! where it was, for a list under the file.
20
21use serde::{Deserialize, Serialize};
22
23use crate::model::{ChecksState, PullRequestSummary};
24
25/// Which column of the diff a line number belongs to.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
27#[serde(rename_all = "snake_case")]
28pub enum DiffSide {
29 /// The file before the change (the merge base): deleted and context
30 /// lines, numbered as in the old file.
31 Old,
32 /// The file after the change (the request's head): added and context
33 /// lines, numbered as in the new file.
34 #[default]
35 New,
36}
37
38impl DiffSide {
39 /// `old` / `new`, the spelling the CLI takes and prints.
40 pub fn as_str(self) -> &'static str {
41 match self {
42 Self::Old => "old",
43 Self::New => "new",
44 }
45 }
46
47 pub fn parse(text: &str) -> Option<Self> {
48 match text.trim().to_ascii_lowercase().as_str() {
49 "old" | "left" | "base" => Some(Self::Old),
50 "new" | "right" | "head" => Some(Self::New),
51 _ => None,
52 }
53 }
54}
55
56/// What the host says about whether the request can be merged.
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
58#[serde(rename_all = "snake_case")]
59pub enum MergeState {
60 /// Nothing blocks it as far as the host says.
61 Clean,
62 /// The branches conflict.
63 Conflicting,
64 /// Mergeable in principle, but something else blocks it (required
65 /// reviews, failing checks, a draft).
66 Blocked,
67 /// The host has not computed it yet, or does not say.
68 #[default]
69 Unknown,
70}
71
72/// One pull/merge request, read for review.
73#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
74pub struct PullRequestDetail {
75 #[serde(flatten)]
76 pub summary: PullRequestSummary,
77 /// The description, markdown as the host stores it.
78 pub body: String,
79 /// The head commit the review is about. Pass it back in
80 /// [`ReviewSubmission::head_sha`] so a comment cannot land on a newer
81 /// push than the one the reviewer read.
82 pub head_sha: String,
83 /// The base branch tip the host last compared against (the diff's
84 /// real base is the merge base, which git computes locally).
85 pub base_sha: String,
86 pub merge_state: MergeState,
87 pub checks: Option<ChecksState>,
88 pub created_at: String,
89 /// `None` when the host does not say.
90 pub additions: Option<u32>,
91 pub deletions: Option<u32>,
92 pub changed_files: Option<u32>,
93 /// What this client lets the reviewer do (so a UI can hide the rest).
94 pub capabilities: ReviewCapabilities,
95 /// The numbered patch sets of the request, oldest first (Gerrit; empty
96 /// elsewhere). `head_sha` is the one of [`Self::shown_patch_set`].
97 #[serde(default, skip_serializing_if = "Vec::is_empty")]
98 pub patch_sets: Vec<PatchSet>,
99 /// The patch set this detail (and its files and threads) describes.
100 #[serde(default, skip_serializing_if = "Option::is_none")]
101 pub shown_patch_set: Option<u32>,
102 /// The labels the user may vote on, each with the values the server
103 /// permits for this request (Gerrit; empty elsewhere).
104 #[serde(default, skip_serializing_if = "Vec::is_empty")]
105 pub vote_labels: Vec<VoteLabel>,
106 /// The server says this request can be submitted (merged) right now
107 /// (Gerrit; `false` elsewhere, where merging is not a review action).
108 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
109 pub can_submit: bool,
110 /// The user may abandon this request (Gerrit; `false` elsewhere).
111 #[serde(default, skip_serializing_if = "std::ops::Not::not")]
112 pub can_abandon: bool,
113}
114
115/// One patch set (revision) of a Gerrit change.
116#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
117pub struct PatchSet {
118 pub number: u32,
119 /// The commit of the patch set (what a review is sent on).
120 pub sha: String,
121 pub created_at: String,
122}
123
124/// One value a label can be voted: `-2`, `0`, `+1`.
125#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
126pub struct VoteValue {
127 pub value: i32,
128 /// The server's words for it ("Looks good to me, approved").
129 pub text: String,
130}
131
132/// A label the user may vote on (`Code-Review`, `Verified`).
133#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
134pub struct VoteLabel {
135 pub name: String,
136 /// Only the values the server permits for this request and user,
137 /// lowest first.
138 pub values: Vec<VoteValue>,
139 /// The user's vote now.
140 pub current: i32,
141 /// What no vote reads as (`0`).
142 pub default: i32,
143}
144
145/// A vote sent with a review.
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
147pub struct ReviewVote {
148 pub label: String,
149 pub value: i32,
150}
151
152/// What a host and its edition let a review do. A choice that is `false`
153/// is not offered: the UI hides it, and asking anyway is
154/// [`crate::Error::Unsupported`].
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
156pub struct ReviewCapabilities {
157 /// Any of the review operations at all.
158 pub review: bool,
159 pub approve: bool,
160 /// GitHub has it; GitLab has no equivalent through its REST API.
161 pub request_changes: bool,
162 /// A comment covering several lines (`start_line`..`line`).
163 pub line_ranges: bool,
164 /// Threads can be resolved and reopened.
165 pub resolve: bool,
166}
167
168impl ReviewCapabilities {
169 /// Whether [`ReviewEvent`] `event` may be submitted.
170 pub fn allows(&self, event: ReviewEvent) -> bool {
171 self.review
172 && match event {
173 ReviewEvent::Comment => true,
174 ReviewEvent::Approve => self.approve,
175 ReviewEvent::RequestChanges => self.request_changes,
176 }
177 }
178}
179
180/// How a file changed in the request.
181#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
182#[serde(rename_all = "snake_case")]
183pub enum FileStatus {
184 Added,
185 Modified,
186 Removed,
187 Renamed,
188}
189
190/// One changed file of the request, for the file list.
191#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
192pub struct ReviewFile {
193 /// The path at the head (the old path for a removed file).
194 pub path: String,
195 /// The path at the merge base, for a rename.
196 pub previous_path: Option<String>,
197 pub status: FileStatus,
198 pub additions: u32,
199 pub deletions: u32,
200}
201
202/// One comment: on a thread, or in the conversation.
203#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
204pub struct ReviewComment {
205 /// The host's id, as a string (a number on both hosts).
206 pub id: String,
207 pub author: String,
208 /// Markdown, as written.
209 pub body: String,
210 pub created_at: String,
211 /// The comment this one answers (conversation comments only; a
212 /// thread's replies are ordered in `comments`).
213 #[serde(default, skip_serializing_if = "Option::is_none")]
214 pub in_reply_to: Option<String>,
215 #[serde(default, skip_serializing_if = "Option::is_none")]
216 pub web_url: Option<String>,
217}
218
219/// The verdict of a submitted review.
220#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
221#[serde(rename_all = "snake_case")]
222pub enum ReviewState {
223 Approved,
224 ChangesRequested,
225 Commented,
226 /// A verdict the author or a maintainer dismissed.
227 Dismissed,
228}
229
230/// A review somebody submitted: the verdict and its summary (the line
231/// comments are in the threads). Listed in the conversation next to the
232/// plain comments.
233#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
234pub struct SubmittedReview {
235 pub id: String,
236 pub author: String,
237 pub state: ReviewState,
238 /// The summary; may be empty (an approval without words).
239 pub body: String,
240 /// Empty when the host does not date it (GitLab approvals).
241 pub submitted_at: String,
242}
243
244/// A conversation attached to a place in the code.
245#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
246pub struct ReviewThread {
247 /// What `reply_to_thread` / `resolve_thread` take: GitHub's GraphQL
248 /// node id, GitLab's discussion id. Opaque.
249 pub id: String,
250 /// The path at the head (at the merge base for a removed file).
251 pub path: String,
252 /// The line, on `side`, the thread sits on now; `None` once it is
253 /// outdated (the code it was written against changed).
254 pub line: Option<u32>,
255 /// Where it was when written, kept for outdated threads.
256 pub original_line: Option<u32>,
257 pub side: DiffSide,
258 /// The first line of a multi-line comment, on `side`.
259 pub start_line: Option<u32>,
260 pub resolved: bool,
261 pub outdated: bool,
262 /// Whether it can be resolved at all (a GitLab thread may not be).
263 pub resolvable: bool,
264 /// Oldest first; the first is the thread's opening comment.
265 pub comments: Vec<ReviewComment>,
266}
267
268impl ReviewThread {
269 /// The diff line to draw the thread under, or `None` when it has no
270 /// current line (outdated).
271 pub fn anchor(&self) -> Option<(DiffSide, u32)> {
272 self.line.map(|line| (self.side, line))
273 }
274}
275
276/// How a review concludes.
277#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
278#[serde(rename_all = "snake_case")]
279pub enum ReviewEvent {
280 /// Just the comments, no verdict.
281 Comment,
282 Approve,
283 RequestChanges,
284}
285
286impl ReviewEvent {
287 pub fn as_str(self) -> &'static str {
288 match self {
289 Self::Comment => "comment",
290 Self::Approve => "approve",
291 Self::RequestChanges => "request-changes",
292 }
293 }
294
295 pub fn parse(text: &str) -> Option<Self> {
296 match text.trim().to_ascii_lowercase().replace('_', "-").as_str() {
297 "comment" => Some(Self::Comment),
298 "approve" => Some(Self::Approve),
299 "request-changes" => Some(Self::RequestChanges),
300 _ => None,
301 }
302 }
303}
304
305/// A comment on code that is held locally until the review is submitted.
306#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
307pub struct PendingComment {
308 /// The path at the head.
309 pub path: String,
310 /// The path at the merge base, when the file was renamed (GitLab
311 /// wants both; defaults to `path`).
312 #[serde(default, skip_serializing_if = "Option::is_none")]
313 pub previous_path: Option<String>,
314 /// The (last) line commented, on `side`.
315 pub line: u32,
316 pub side: DiffSide,
317 /// The first line of a range, on `side`; `None` for one line.
318 #[serde(default, skip_serializing_if = "Option::is_none")]
319 pub start_line: Option<u32>,
320 /// For an unchanged context line, its number on the other side.
321 /// GitLab needs both numbers to place such a comment; GitHub ignores
322 /// it. The diff view knows both.
323 #[serde(default, skip_serializing_if = "Option::is_none")]
324 pub other_line: Option<u32>,
325 pub body: String,
326}
327
328/// Everything a review submission sends: one host round-trip set, however
329/// many comments were collected.
330#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
331pub struct ReviewSubmission {
332 pub event: ReviewEvent,
333 /// The review's summary; may be empty for an approval or when
334 /// comments carry the message.
335 pub body: String,
336 pub comments: Vec<PendingComment>,
337 /// The head the comments were written against; the host refuses (or
338 /// marks them outdated) when the request moved. `None`: whatever is
339 /// current.
340 #[serde(default)]
341 pub head_sha: Option<String>,
342 /// Votes on labels (Gerrit's `Code-Review`, `Verified`); the event is
343 /// ignored for the labels voted this way.
344 #[serde(default, skip_serializing_if = "Vec::is_empty")]
345 pub votes: Vec<ReviewVote>,
346}
347
348/// What a submitted review reports back.
349#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
350pub struct ReviewOutcome {
351 pub event: ReviewEvent,
352 /// How many line comments were posted.
353 pub comments: usize,
354 /// The host's id of the review when it has one (GitHub).
355 pub review_id: Option<String>,
356}