1use serde::{Deserialize, Serialize};
4
5use crate::review::model::{DiffSide, ReviewState};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum ItemKind {
11 Issue,
12 PullRequest,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
19pub struct ItemKey {
20 pub kind: ItemKind,
21 pub number: u64,
22 #[serde(default)]
23 pub id: String,
24}
25
26impl ItemKey {
27 pub fn issue(number: u64) -> Self {
28 Self {
29 kind: ItemKind::Issue,
30 number,
31 id: String::new(),
32 }
33 }
34
35 pub fn pull_request(number: u64) -> Self {
36 Self {
37 kind: ItemKind::PullRequest,
38 number,
39 id: String::new(),
40 }
41 }
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
45#[serde(rename_all = "snake_case")]
46pub enum ActorKind {
47 User,
48 Bot,
49 System,
50 #[default]
51 Unknown,
52}
53
54#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
56pub struct Actor {
57 pub login: String,
58 pub display: String,
60 pub kind: ActorKind,
61 pub web_url: Option<String>,
62}
63
64impl Actor {
65 pub fn new(login: impl Into<String>, display: impl Into<String>, kind: ActorKind) -> Self {
66 Self {
67 login: login.into(),
68 display: display.into(),
69 kind,
70 web_url: None,
71 }
72 }
73
74 pub fn unknown() -> Self {
76 Self::new("ghost", "ghost", ActorKind::Unknown)
77 }
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
82#[serde(rename_all = "snake_case")]
83pub enum MarkupFlavor {
84 #[default]
85 Gfm,
86 GitlabMd,
87 GiteaMd,
88 CommonMark,
89 AzureMd,
90 BitbucketMd,
91 Html,
92 JiraWiki,
93 Textile,
94 YoutrackMd,
95 TrelloMd,
96 PlainText,
97 ObsidianMd,
98}
99
100#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
101pub struct Markup {
102 pub text: String,
103 pub flavor: MarkupFlavor,
104}
105
106impl Markup {
107 pub fn new(text: impl Into<String>, flavor: MarkupFlavor) -> Self {
108 Self {
109 text: text.into(),
110 flavor,
111 }
112 }
113}
114
115#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
117pub struct Reaction {
118 pub name: String,
119 pub count: u32,
120}
121
122#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
123pub struct Comment {
124 pub id: String,
125 pub author: Actor,
126 pub body: Markup,
127 pub created_at: String,
128 pub updated_at: Option<String>,
129 pub edited: bool,
131 pub reactions: Vec<Reaction>,
132 pub in_reply_to: Option<String>,
134 pub discussion_id: Option<String>,
136 pub internal: bool,
138 pub can_edit: Option<bool>,
140 pub can_delete: Option<bool>,
141 pub web_url: Option<String>,
142}
143
144#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
145#[serde(tag = "kind", rename_all = "snake_case")]
146pub enum EventKind {
147 Opened,
148 Closed,
149 Reopened,
150 Merged,
151 Labeled {
152 added: Vec<String>,
153 removed: Vec<String>,
154 },
155 Assigned {
156 added: Vec<String>,
157 removed: Vec<String>,
158 },
159 Renamed {
160 from: String,
161 to: String,
162 },
163 CommitsPushed {
164 count: u32,
165 head: Option<String>,
166 },
167 CrossReferenced {
168 from: String,
169 url: Option<String>,
170 },
171 StatusChanged {
172 from: String,
173 to: String,
174 },
175 ReviewRequested {
176 who: String,
177 },
178 Other {
180 text: String,
181 },
182}
183
184#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
185pub struct Event {
186 pub id: String,
187 pub actor: Option<Actor>,
188 pub at: String,
189 pub kind: EventKind,
190}
191
192#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
194pub struct ReviewEntry {
195 pub id: String,
196 pub author: Actor,
197 pub state: ReviewState,
198 pub body: Markup,
199 pub submitted_at: String,
200}
201
202#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
204pub struct CodeThread {
205 pub id: String,
206 pub path: String,
207 pub line: Option<u32>,
208 pub original_line: Option<u32>,
209 pub side: DiffSide,
210 pub start_line: Option<u32>,
211 pub resolved: bool,
212 pub outdated: bool,
213 pub resolvable: bool,
214 pub diff_hunk: Option<String>,
216 pub comments: Vec<Comment>,
218}
219
220#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
221#[serde(tag = "entry", rename_all = "snake_case")]
222pub enum TimelineEntry {
223 Comment(Comment),
224 Review(ReviewEntry),
225 Thread(CodeThread),
226 Event(Event),
227}
228
229impl TimelineEntry {
230 pub fn at(&self) -> &str {
232 match self {
233 Self::Comment(c) => &c.created_at,
234 Self::Review(r) => &r.submitted_at,
235 Self::Thread(t) => t.comments.first().map_or("", |c| c.created_at.as_str()),
236 Self::Event(e) => &e.at,
237 }
238 }
239}
240
241#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
242pub struct ItemHeader {
243 pub key: ItemKey,
244 pub reference: String,
246 pub title: String,
247 pub state_label: String,
249 pub open: bool,
250 pub author: Actor,
251 pub body: Markup,
252 pub created_at: String,
253 pub web_url: String,
254 pub labels: Vec<String>,
255}
256
257#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
259pub struct PageCursor(pub String);
260
261#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
262pub struct TimelinePage {
263 pub header: ItemHeader,
264 pub entries: Vec<TimelineEntry>,
266 pub older: Option<PageCursor>,
268 pub total_hint: Option<u32>,
270}
271
272#[derive(Debug, Clone, Copy, PartialEq, Eq)]
273pub struct TimelineOpts {
274 pub events: bool,
276}
277
278impl Default for TimelineOpts {
279 fn default() -> Self {
280 Self { events: true }
281 }
282}