Elffuss BraidHub

utopia-ia/elffuss-braid public

main · 14.9 KiBRawPermalink
1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 UtopiaIA
3
4//! Issues, comments, labels and assignees (section 18): the bodies of the
5//! routes under `/v1/r/{owner}/{name}/issues`, `…/comments`, `…/labels` and
6//! `…/threads`, and of `GET /v1/me/issues`.
7//!
8//! Issues and change requests share one numbering in their repository: a
9//! number names one thread, whichever kind it is. Times are RFC 3339, in
10//! UTC; logins are lowercase.
11
12use serde::{Deserialize, Serialize};
13
14use crate::cards::RepoRef;
15
16/// An issue, or a change request, that is open.
17pub const OPEN: &str = "open";
18
19/// An issue that is closed.
20pub const CLOSED: &str = "closed";
21
22/// What a closed issue was closed as: done.
23pub const COMPLETED: &str = "completed";
24
25/// What a closed issue was closed as: not to be done.
26pub const NOT_PLANNED: &str = "not_planned";
27
28/// The reasons a writer hides a comment for.
29pub const HIDE_REASONS: [&str; 4] = ["spam", "off_topic", "outdated", "resolved"];
30
31/// How many colours labels choose from: `0` to `11`.
32pub const COLORS: u8 = 12;
33
34/// The longest name of a label, in characters.
35pub const MAX_LABEL: usize = 50;
36
37/// The longest description of a label, in characters.
38pub const MAX_LABEL_DESCRIPTION: usize = 200;
39
40/// What a timeline's entries say happened, besides a comment: the title
41/// or the text changed, the state, a label, an assignee or the lock, or
42/// another thread named this one (`#4` in its repository, `owner/name#4`
43/// in another).
44pub const EVENTS: [&str; 12] = [
45 "renamed",
46 "edited",
47 "closed",
48 "reopened",
49 "labeled",
50 "unlabeled",
51 "assigned",
52 "unassigned",
53 "locked",
54 "unlocked",
55 "referenced",
56 "cross_referenced",
57];
58
59/// Whether `reason` is one a comment may be hidden for.
60pub fn is_hide_reason(reason: &str) -> bool {
61 HIDE_REASONS.contains(&reason)
62}
63
64/// Whether `reason` is one an issue may be closed as.
65pub fn is_close_reason(reason: &str) -> bool {
66 reason == COMPLETED || reason == NOT_PLANNED
67}
68
69/// An issue.
70#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
71pub struct Issue {
72 /// Its number in its repository.
73 pub number: u64,
74 /// Its title.
75 pub title: String,
76 /// Its text, Markdown.
77 #[serde(default)]
78 pub body: String,
79 /// `open` or `closed`.
80 pub state: String,
81 /// For a closed issue, what it was closed as: `completed` or
82 /// `not_planned`.
83 #[serde(default)]
84 pub state_reason: Option<String>,
85 /// Who opened it.
86 pub author: String,
87 /// The label of the agent that opened it, if one said.
88 #[serde(default)]
89 pub agent: Option<String>,
90 /// Its labels, by name.
91 #[serde(default)]
92 pub labels: Vec<String>,
93 /// The accounts it is assigned to.
94 #[serde(default)]
95 pub assignees: Vec<String>,
96 /// Whether only the repository's writers may comment on it.
97 #[serde(default)]
98 pub locked: bool,
99 /// How many comments it has.
100 #[serde(default)]
101 pub comments: u64,
102 /// When it was opened.
103 pub created: String,
104 /// When it last changed.
105 pub updated: String,
106 /// When it was closed, while it is.
107 #[serde(default)]
108 pub closed: Option<String>,
109 /// Who closed it, unless a landing did.
110 #[serde(default)]
111 pub closed_by: Option<String>,
112 /// The changeset whose landing closed it (`fixes #4` in its message).
113 #[serde(default)]
114 pub closed_by_changeset: Option<String>,
115 /// Its text as HTML, as the caller's pages show it: given by the
116 /// routes that answer one issue.
117 #[serde(default, skip_serializing_if = "Option::is_none")]
118 pub html: Option<String>,
119}
120
121/// A page of issues.
122#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
123pub struct Issues {
124 /// The issues.
125 pub issues: Vec<Issue>,
126 /// The cursor of the next page, or `None` on the last one.
127 #[serde(default)]
128 pub next: Option<String>,
129}
130
131/// `POST …/issues`: a new issue.
132#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
133pub struct NewIssue {
134 /// Its title: 1 to 256 characters, one line.
135 pub title: String,
136 /// Its text, Markdown, at most 64 KiB.
137 #[serde(default, skip_serializing_if = "Option::is_none")]
138 pub body: Option<String>,
139 /// Labels to give it, by name: the repository's writers only.
140 #[serde(default, skip_serializing_if = "Vec::is_empty")]
141 pub labels: Vec<String>,
142 /// Accounts to assign it to: the repository's writers only.
143 #[serde(default, skip_serializing_if = "Vec::is_empty")]
144 pub assignees: Vec<String>,
145 /// The label of the agent that opens it, recorded with it.
146 #[serde(default, skip_serializing_if = "Option::is_none")]
147 pub agent: Option<String>,
148}
149
150/// `PATCH …/issues/{n}`: the fields to change.
151#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
152pub struct IssuePatch {
153 /// A new title.
154 #[serde(default, skip_serializing_if = "Option::is_none")]
155 pub title: Option<String>,
156 /// A new text.
157 #[serde(default, skip_serializing_if = "Option::is_none")]
158 pub body: Option<String>,
159 /// `open` or `closed`.
160 #[serde(default, skip_serializing_if = "Option::is_none")]
161 pub state: Option<String>,
162 /// With `closed`: `completed` (the default) or `not_planned`.
163 #[serde(default, skip_serializing_if = "Option::is_none")]
164 pub state_reason: Option<String>,
165 /// Its labels, all of them, by name.
166 #[serde(default, skip_serializing_if = "Option::is_none")]
167 pub labels: Option<Vec<String>>,
168 /// The accounts it is assigned to, all of them.
169 #[serde(default, skip_serializing_if = "Option::is_none")]
170 pub assignees: Option<Vec<String>>,
171 /// Whether only writers may comment on it.
172 #[serde(default, skip_serializing_if = "Option::is_none")]
173 pub locked: Option<bool>,
174}
175
176/// A comment on a thread.
177#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
178pub struct Comment {
179 /// Its id in its repository.
180 pub id: u64,
181 /// Who wrote it.
182 pub author: String,
183 /// The label of the agent that wrote it, if one said.
184 #[serde(default)]
185 pub agent: Option<String>,
186 /// Its text, Markdown; empty when it is hidden from the caller.
187 #[serde(default)]
188 pub body: String,
189 /// When it was written.
190 pub created: String,
191 /// When it was last edited, if it was.
192 #[serde(default)]
193 pub edited: Option<String>,
194 /// Why a writer hid it, if one did: `spam`, `off_topic`, `outdated`
195 /// or `resolved`; to the repository's writers, `suspended` for a
196 /// comment of a suspended account, which others do not see at all.
197 #[serde(default)]
198 pub hidden: Option<String>,
199 /// Its text as HTML, as the caller's pages show it: given by the
200 /// routes that answer one comment.
201 #[serde(default, skip_serializing_if = "Option::is_none")]
202 pub html: Option<String>,
203}
204
205/// A page of comments, oldest first.
206#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
207pub struct Comments {
208 /// The comments.
209 pub comments: Vec<Comment>,
210 /// The cursor of the next page, or `None` on the last one.
211 #[serde(default)]
212 pub next: Option<String>,
213}
214
215/// `POST …/issues/{n}/comments`: a new comment.
216#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
217pub struct NewComment {
218 /// Its text, Markdown: 1 byte to 64 KiB.
219 pub body: String,
220 /// The label of the agent that writes it, recorded with it.
221 #[serde(default, skip_serializing_if = "Option::is_none")]
222 pub agent: Option<String>,
223}
224
225/// `PATCH …/comments/{id}`: a comment's new text.
226#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
227pub struct CommentPatch {
228 /// The new text.
229 pub body: String,
230}
231
232/// `POST …/comments/{id}/hide`: why a comment is hidden, or `""` to show
233/// it again.
234#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
235pub struct HideComment {
236 /// One of [`HIDE_REASONS`], or empty.
237 pub reason: String,
238}
239
240/// A label of a repository.
241#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
242pub struct Label {
243 /// Its name: 1 to 50 characters, without `,`; compared without regard
244 /// to case.
245 pub name: String,
246 /// Its colour in the palette, `0` to `11`.
247 pub color: u8,
248 /// What it means.
249 #[serde(default)]
250 pub description: String,
251}
252
253/// `GET …/labels`: a repository's labels, by name.
254#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
255pub struct Labels {
256 /// The labels.
257 pub labels: Vec<Label>,
258}
259
260/// `PUT …/labels/{label}`: a label made, or changed.
261#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
262pub struct LabelPut {
263 /// Its colour, `0` to `11`: kept when it is left out, `0` for a new
264 /// label.
265 #[serde(default, skip_serializing_if = "Option::is_none")]
266 pub color: Option<u8>,
267 /// What it means: kept when it is left out, empty for a new label.
268 #[serde(default, skip_serializing_if = "Option::is_none")]
269 pub description: Option<String>,
270}
271
272/// Where a reference to a thread was made.
273#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
274pub struct ThreadSource {
275 /// The repository that made it, when it is another one than the
276 /// thread's.
277 #[serde(default, skip_serializing_if = "Option::is_none")]
278 pub repo: Option<RepoRef>,
279 /// The number of the thread that made it.
280 pub number: u64,
281}
282
283/// An entry of a thread's timeline: a comment, or something that happened.
284#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
285pub struct TimelineItem {
286 /// `comment`, or one of [`EVENTS`].
287 pub kind: String,
288 /// Who did it; empty for what the hub did by itself, such as locking a
289 /// thread that holds the most comments one may.
290 pub actor: String,
291 /// When.
292 pub created: String,
293 /// The comment, for a `comment`.
294 #[serde(default, skip_serializing_if = "Option::is_none")]
295 pub comment: Option<Comment>,
296 /// The label, for `labeled` and `unlabeled`.
297 #[serde(default, skip_serializing_if = "Option::is_none")]
298 pub label: Option<String>,
299 /// The account, for `assigned` and `unassigned`.
300 #[serde(default, skip_serializing_if = "Option::is_none")]
301 pub assignee: Option<String>,
302 /// Why: what `closed` closed it as, or why the hub `locked` it.
303 #[serde(default, skip_serializing_if = "Option::is_none")]
304 pub reason: Option<String>,
305 /// The title before, for `renamed`.
306 #[serde(default, skip_serializing_if = "Option::is_none")]
307 pub from: Option<String>,
308 /// The title after, for `renamed`.
309 #[serde(default, skip_serializing_if = "Option::is_none")]
310 pub to: Option<String>,
311 /// The thread that referenced this one, for `referenced` and
312 /// `cross_referenced`.
313 #[serde(default, skip_serializing_if = "Option::is_none")]
314 pub source: Option<ThreadSource>,
315 /// The changeset whose landing closed it, for `closed`.
316 #[serde(default, skip_serializing_if = "Option::is_none")]
317 pub changeset: Option<String>,
318}
319
320/// A page of a thread's timeline, oldest first.
321#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
322pub struct Timeline {
323 /// The entries.
324 pub items: Vec<TimelineItem>,
325 /// The cursor of the next page, or `None` on the last one.
326 #[serde(default)]
327 pub next: Option<String>,
328}
329
330/// `GET …/threads/{n}`: what a number names.
331#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
332pub struct ThreadInfo {
333 /// `issue` or `request`.
334 pub kind: String,
335 /// Its number.
336 pub number: u64,
337 /// Its title.
338 pub title: String,
339 /// `open` or `closed`, and for a change request `landed`.
340 pub state: String,
341}
342
343/// An issue of some repository, in `GET /v1/me/issues`.
344#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
345pub struct RepoIssue {
346 /// Its repository.
347 pub repo: RepoRef,
348 /// The issue.
349 #[serde(flatten)]
350 pub issue: Issue,
351}
352
353/// `GET /v1/me/issues`: a page of the issues the caller is assigned to,
354/// opened or is mentioned in, latest first.
355#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
356pub struct RepoIssues {
357 /// The issues.
358 pub issues: Vec<RepoIssue>,
359 /// The cursor of the next page, or `None` on the last one.
360 #[serde(default)]
361 pub next: Option<String>,
362}
363
364#[cfg(test)]
365mod tests {
366 use super::*;
367
368 #[test]
369 fn issues_on_the_wire() {
370 let issue = Issue {
371 number: 4,
372 title: "Crash on start".into(),
373 state: OPEN.into(),
374 author: "ada".into(),
375 labels: vec!["bug".into()],
376 created: "2026-10-18T12:00:00Z".into(),
377 updated: "2026-10-18T12:00:00Z".into(),
378 ..Issue::default()
379 };
380 let text = serde_json::to_string(&issue).unwrap();
381 // What is unknown is null, never missing: but the HTML, which only
382 // the routes of one issue give.
383 assert!(text.contains(r#""state_reason":null"#), "{text}");
384 assert!(text.contains(r#""closed_by_changeset":null"#), "{text}");
385 assert!(!text.contains("html"), "{text}");
386 assert_eq!(serde_json::from_str::<Issue>(&text).unwrap(), issue);
387 // In a list of the caller's issues, with its repository beside it.
388 let mine = RepoIssue {
389 repo: RepoRef {
390 owner: "ada".into(),
391 name: "demo".into(),
392 },
393 issue: issue.clone(),
394 };
395 let value = serde_json::to_value(&mine).unwrap();
396 assert_eq!(value["repo"]["name"], "demo");
397 assert_eq!(value["number"], 4);
398 assert_eq!(serde_json::from_value::<RepoIssue>(value).unwrap(), mine);
399 }
400
401 #[test]
402 fn bodies_as_clients_send_them() {
403 let new: NewIssue = serde_json::from_str(r#"{"title":"x"}"#).unwrap();
404 assert_eq!(
405 (new.title.as_str(), new.body.as_deref(), new.labels.len()),
406 ("x", None, 0)
407 );
408 assert_eq!(serde_json::to_string(&new).unwrap(), r#"{"title":"x"}"#);
409 let patch: IssuePatch = serde_json::from_str(r#"{"state":"closed","labels":[]}"#).unwrap();
410 assert_eq!(patch.state.as_deref(), Some(CLOSED));
411 assert_eq!(patch.labels, Some(Vec::new()));
412 assert_eq!(patch.assignees, None);
413 let put: LabelPut = serde_json::from_str(r#"{"color":3}"#).unwrap();
414 assert_eq!((put.color, put.description), (Some(3), None));
415 let item: TimelineItem =
416 serde_json::from_str(r#"{"kind":"closed","actor":"ada","created":"t"}"#).unwrap();
417 assert_eq!(item.reason, None);
418 let text = serde_json::to_string(&item).unwrap();
419 assert_eq!(text, r#"{"kind":"closed","actor":"ada","created":"t"}"#);
420 }
421
422 #[test]
423 fn reasons() {
424 for reason in HIDE_REASONS {
425 assert!(is_hide_reason(reason));
426 }
427 assert!(!is_hide_reason("suspended") && !is_hide_reason(""));
428 assert!(is_close_reason(COMPLETED) && is_close_reason(NOT_PLANNED));
429 assert!(!is_close_reason("done"));
430 }
431}