forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.09.7 MiB37e985dTahinli3 hours ago363 commits

issues.rs293 lines, 8.0 KiBHistoryRawDownload
1use crate::error::{Error, Result};
2use crate::model::Id;
3
4pub const MAX_TITLE: usize = 256;
5pub const MAX_BODY: usize = 65536;
6pub const MAX_LABEL_NAME: usize = 50;
7pub const MAX_LABEL_DESCRIPTION: usize = 500;
8pub const MAX_ASSIGNEES: usize = 10;
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum IssueState {
12 Open,
13 Closed,
14}
15
16string_enum!(IssueState {
17 Open = "open",
18 Closed = "closed",
19});
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22pub enum StateReason {
23 Completed,
24 NotPlanned,
25}
26
27string_enum!(StateReason {
28 Completed = "completed",
29 NotPlanned = "not_planned",
30});
31
32#[derive(Debug, Clone, PartialEq)]
33pub struct Issue {
34 pub id: Id,
35 pub repo_id: Id,
36 pub number: i64,
37 pub title: String,
38 pub body: String,
39 pub state: IssueState,
40 pub state_reason: Option<StateReason>,
41 pub author_id: Id,
42 pub milestone_id: Option<Id>,
43 pub comment_count: i64,
44 pub created_at: String,
45 pub updated_at: String,
46 pub closed_at: Option<String>,
47 pub locked: bool,
48 pub lock_reason: Option<String>,
49}
50
51pub const LOCK_REASONS: &[&str] = &["off_topic", "too_heated", "resolved", "spam"];
52
53#[derive(Debug, Clone, PartialEq)]
54pub struct Comment {
55 pub id: Id,
56 pub issue_id: Id,
57 pub author_id: Id,
58 pub body: String,
59 pub created_at: String,
60 pub updated_at: String,
61 pub edited: bool,
62}
63
64#[derive(Debug, Clone, PartialEq)]
65pub struct IssueEvent {
66 pub id: Id,
67 pub issue_id: Id,
68 pub actor_id: Option<Id>,
69 pub kind: String,
70 pub data: serde_json::Value,
71 pub created_at: String,
72}
73
74#[derive(Debug, Clone, PartialEq)]
75pub struct Label {
76 pub id: Id,
77 pub repo_id: Id,
78 pub name: String,
79 pub color: String,
80 pub description: String,
81}
82
83#[derive(Debug, Clone, PartialEq)]
84pub struct Milestone {
85 pub id: Id,
86 pub repo_id: Id,
87 pub title: String,
88 pub description: String,
89 pub due_on: Option<String>,
90 pub state: IssueState,
91 pub created_at: String,
92 pub closed_at: Option<String>,
93 pub open_issues: i64,
94 pub closed_issues: i64,
95}
96
97#[derive(Debug, Clone, PartialEq)]
98pub struct IssueFilter {
99 pub state: Option<IssueState>,
100 pub label: Option<String>,
101 pub milestone: Option<String>,
102 pub author: Option<String>,
103 pub assignee: Option<String>,
104 pub review_requested: Option<String>,
105 pub query: Option<String>,
106 pub draft: Option<bool>,
107 pub sort: IssueSort,
108 pub page: u32,
109 pub per_page: u32,
110}
111
112impl Default for IssueFilter {
113 fn default() -> Self {
114 IssueFilter {
115 state: Some(IssueState::Open),
116 label: None,
117 milestone: None,
118 author: None,
119 assignee: None,
120 review_requested: None,
121 query: None,
122 draft: None,
123 sort: IssueSort::Newest,
124 page: 1,
125 per_page: 25,
126 }
127 }
128}
129
130#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
131pub enum IssueSort {
132 #[default]
133 Newest,
134 Oldest,
135 RecentlyUpdated,
136}
137
138string_enum!(IssueSort {
139 Newest = "newest",
140 Oldest = "oldest",
141 RecentlyUpdated = "updated",
142});
143
144#[derive(Debug, Clone, PartialEq)]
145pub struct IssueListItem {
146 pub issue: Issue,
147 pub author: String,
148 pub labels: Vec<Label>,
149 pub assignees: Vec<String>,
150}
151
152pub fn validate_title(t: &str) -> Result<String> {
153 let title = t.trim();
154 if title.is_empty() {
155 return Err(Error::Invalid("title is empty".into()));
156 }
157 if title.chars().count() > MAX_TITLE {
158 return Err(Error::Invalid(format!(
159 "title is longer than {MAX_TITLE} characters"
160 )));
161 }
162 if title.chars().any(char::is_control) {
163 return Err(Error::Invalid("title contains control characters".into()));
164 }
165 Ok(title.to_string())
166}
167
168pub fn validate_body(b: &str) -> Result<()> {
169 if b.len() > MAX_BODY {
170 return Err(Error::Invalid(format!(
171 "body is longer than {MAX_BODY} bytes"
172 )));
173 }
174 Ok(())
175}
176
177pub fn validate_label(name: &str, color: &str, description: &str) -> Result<(String, String)> {
178 let name = name.trim();
179 let len = name.chars().count();
180 if len == 0 || len > MAX_LABEL_NAME {
181 return Err(Error::Invalid(format!(
182 "label name must be 1 to {MAX_LABEL_NAME} characters"
183 )));
184 }
185 if name.chars().any(char::is_control) {
186 return Err(Error::Invalid(
187 "label name contains control characters".into(),
188 ));
189 }
190 let color = normalize_color(color)
191 .ok_or_else(|| Error::Invalid(format!("label color {color:?} is not #rgb or #rrggbb")))?;
192 if description.chars().count() > MAX_LABEL_DESCRIPTION {
193 return Err(Error::Invalid(format!(
194 "label description must be at most {MAX_LABEL_DESCRIPTION} characters"
195 )));
196 }
197 Ok((name.to_string(), color))
198}
199
200fn normalize_color(color: &str) -> Option<String> {
201 let hex = color.strip_prefix('#')?;
202 if !hex.chars().all(|c| c.is_ascii_hexdigit()) {
203 return None;
204 }
205 let lower = hex.to_ascii_lowercase();
206 match lower.len() {
207 3 => {
208 let mut out = String::with_capacity(7);
209 out.push('#');
210 for c in lower.chars() {
211 out.push(c);
212 out.push(c);
213 }
214 Some(out)
215 }
216 6 => Some(format!("#{lower}")),
217 _ => None,
218 }
219}
220
221#[cfg(test)]
222mod tests {
223 use super::*;
224
225 #[test]
226 fn title_is_trimmed_and_checked() {
227 assert_eq!(validate_title(" Fix it ").unwrap(), "Fix it");
228 assert!(validate_title(" ").is_err());
229 assert!(validate_title("").is_err());
230 assert!(validate_title("a\tb").is_err());
231 assert!(validate_title("a\nb").is_err());
232 assert!(validate_title(&"x".repeat(MAX_TITLE)).is_ok());
233 assert!(validate_title(&"x".repeat(MAX_TITLE + 1)).is_err());
234 assert!(validate_title(&"é".repeat(MAX_TITLE)).is_ok());
235 assert!(validate_title(&"é".repeat(MAX_TITLE + 1)).is_err());
236 }
237
238 #[test]
239 fn body_is_checked_in_bytes() {
240 assert!(validate_body("").is_ok());
241 assert!(validate_body(&"x".repeat(MAX_BODY)).is_ok());
242 assert!(validate_body(&"x".repeat(MAX_BODY + 1)).is_err());
243 assert!(validate_body(&"é".repeat(MAX_BODY / 2)).is_ok());
244 assert!(validate_body(&"é".repeat(MAX_BODY / 2 + 1)).is_err());
245 }
246
247 #[test]
248 fn label_name_and_color_are_normalized() {
249 assert_eq!(
250 validate_label(" bug ", "#FF0000", "").unwrap(),
251 ("bug".to_string(), "#ff0000".to_string())
252 );
253 assert_eq!(
254 validate_label("x", "#AbC", "").unwrap(),
255 ("x".to_string(), "#aabbcc".to_string())
256 );
257 assert!(validate_label("", "#fff", "").is_err());
258 assert!(validate_label(" ", "#fff", "").is_err());
259 assert!(validate_label(&"a".repeat(MAX_LABEL_NAME), "#fff", "").is_ok());
260 assert!(validate_label(&"a".repeat(MAX_LABEL_NAME + 1), "#fff", "").is_err());
261 assert!(validate_label("a\u{7}", "#fff", "").is_err());
262 assert!(validate_label("x", "#fff", &"d".repeat(MAX_LABEL_DESCRIPTION + 1)).is_err());
263 }
264
265 #[test]
266 fn label_color_must_be_hex() {
267 for bad in [
268 "", "fff", "#ff", "#ffff", "#fffffff", "#gggggg", "red", "# fff", "#ff00 0",
269 ] {
270 assert!(validate_label("x", bad, "").is_err(), "{bad:?} accepted");
271 }
272 }
273
274 #[test]
275 fn enums_round_trip() {
276 for s in [IssueState::Open, IssueState::Closed] {
277 assert_eq!(s.as_str().parse::<IssueState>().ok(), Some(s));
278 }
279 assert!("Open".parse::<IssueState>().is_err());
280 for r in [StateReason::Completed, StateReason::NotPlanned] {
281 assert_eq!(r.as_str().parse::<StateReason>().ok(), Some(r));
282 }
283 assert!("duplicate".parse::<StateReason>().is_err());
284 for s in [
285 IssueSort::Newest,
286 IssueSort::Oldest,
287 IssueSort::RecentlyUpdated,
288 ] {
289 assert_eq!(s.as_str().parse::<IssueSort>().ok(), Some(s));
290 }
291 assert!("bogus".parse::<IssueSort>().is_err());
292 }
293}