1use async_trait::async_trait;
8use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10
11use crate::{
12 Result,
13 paging::Paged,
14 types::{CommentData, Label, State, UserSummary, deserialize_u64_or_string},
15};
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23pub struct IssueData {
24 #[serde(deserialize_with = "deserialize_u64_or_string")]
26 pub number: u64,
27 pub title: String,
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub body: Option<String>,
32 pub state: State,
34 #[serde(default)]
36 pub labels: Vec<Label>,
37 pub author: UserSummary,
39 #[serde(default)]
41 pub assignees: Vec<UserSummary>,
42 #[serde(skip_serializing_if = "Option::is_none")]
44 pub milestone: Option<crate::types::MilestoneRef>,
45 #[serde(skip_serializing_if = "Option::is_none")]
47 pub created_at: Option<DateTime<Utc>>,
48 #[serde(skip_serializing_if = "Option::is_none")]
50 pub updated_at: Option<DateTime<Utc>>,
51 pub url: String,
53}
54
55#[derive(Debug, Clone)]
57pub struct CreateIssueArgs {
58 pub title: String,
60 pub body: Option<String>,
62 pub labels: Vec<String>,
64 pub assignees: Vec<String>,
66 pub milestone: Option<String>,
68}
69
70#[derive(Debug, Clone, Default)]
74pub struct EditIssueArgs {
75 pub title: Option<String>,
77 pub body: Option<String>,
79 pub milestone: Option<Option<String>>,
82}
83
84#[derive(Debug, Clone, Default)]
88pub struct ListIssueArgs {
89 pub state: Option<State>,
91 pub labels: Vec<String>,
93 pub search: Option<String>,
95 pub limit: Option<u32>,
97 pub milestone: Option<String>,
99}
100
101#[async_trait]
112pub trait IssueProvider: std::fmt::Debug + Send + Sync {
113 async fn create(&self, args: CreateIssueArgs) -> Result<IssueData>;
119
120 async fn edit(&self, number: u64, args: EditIssueArgs) -> Result<IssueData>;
128
129 async fn list(&self, args: ListIssueArgs) -> Result<Paged<IssueData>>;
138
139 async fn view(&self, number: u64) -> Result<IssueData>;
145
146 async fn close(&self, number: u64) -> Result<IssueData>;
152
153 async fn reopen(&self, number: u64) -> Result<IssueData>;
159
160 async fn comment(&self, number: u64, body: &str) -> Result<CommentData>;
166
167 async fn list_comments(&self, number: u64, limit: Option<u32>) -> Result<Paged<CommentData>>;
175
176 async fn add_labels(&self, number: u64, labels: &[String]) -> Result<()>;
182
183 async fn remove_label(&self, number: u64, label: &str) -> Result<()>;
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194
195 fn sample_issue_json() -> &'static str {
197 r#"{
198 "number": 42,
199 "title": "Fix login bug",
200 "body": "Reproduced on v1.2.3",
201 "state": "open",
202 "labels": [
203 {"name": "bug", "color": "d73a4a", "description": "Something isn't working"}
204 ],
205 "author": {"login": "octocat", "id": "1"},
206 "assignees": [{"login": "alice", "id": "7"}],
207 "createdAt": "2026-01-15T09:30:00Z",
208 "updatedAt": "2026-01-16T11:00:00Z",
209 "url": "https://github.com/octocat/hello-world/issues/42"
210 }"#
211 }
212
213 #[test]
214 fn test_should_deserialize_issue_data_from_gh_cli_json() {
215 let json = sample_issue_json();
216 let issue: IssueData = serde_json::from_str(json).expect("valid IssueData JSON");
217
218 assert_eq!(issue.number, 42);
219 assert_eq!(issue.title, "Fix login bug");
220 assert_eq!(issue.body.as_deref(), Some("Reproduced on v1.2.3"));
221 assert_eq!(issue.state, State::Open);
222 assert_eq!(issue.labels.len(), 1);
223 assert_eq!(issue.labels[0].name, "bug");
224 assert_eq!(issue.labels[0].color.as_deref(), Some("d73a4a"));
225 assert_eq!(issue.author.login, "octocat");
226 assert_eq!(issue.author.id, "1");
227 assert_eq!(issue.assignees.len(), 1);
228 assert_eq!(issue.assignees[0].login, "alice");
229 assert_eq!(
230 issue.url,
231 "https://github.com/octocat/hello-world/issues/42"
232 );
233 }
234
235 #[test]
236 fn test_should_roundtrip_issue_data_via_serde() {
237 let json = sample_issue_json();
238 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
239 let re_serialized = serde_json::to_string(&issue).expect("serialize");
240 let round_tripped: IssueData =
241 serde_json::from_str(&re_serialized).expect("re-deserialize");
242
243 assert_eq!(round_tripped.number, issue.number);
244 assert_eq!(round_tripped.title, issue.title);
245 assert_eq!(round_tripped.body, issue.body);
246 assert_eq!(round_tripped.state, issue.state);
247 assert_eq!(round_tripped.url, issue.url);
248 assert_eq!(round_tripped.created_at, issue.created_at);
249 assert_eq!(round_tripped.updated_at, issue.updated_at);
250 }
251
252 #[test]
253 fn test_should_deserialize_issue_with_null_optional_body() {
254 let json = r#"{
255 "number": 1,
256 "title": "t",
257 "body": null,
258 "state": "closed",
259 "labels": [],
260 "author": {"login": "u", "id": "2"},
261 "assignees": [],
262 "createdAt": "2026-02-01T00:00:00Z",
263 "updatedAt": "2026-02-02T00:00:00Z",
264 "url": "https://example.com/1"
265 }"#;
266 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
267 assert!(issue.body.is_none());
268 assert_eq!(issue.state, State::Closed);
269 assert!(issue.labels.is_empty());
270 assert!(issue.assignees.is_empty());
271 }
272
273 #[test]
274 fn test_should_omit_none_body_on_serialize() {
275 let json = sample_issue_json();
276 let mut issue: IssueData = serde_json::from_str(json).expect("deserialize");
277 issue.body = None;
278 let serialized = serde_json::to_string(&issue).expect("serialize");
279 assert!(!serialized.contains("\"body\":null"));
281 assert!(!serialized.contains("\"body\": null"));
282 }
283
284 #[test]
285 fn test_should_default_empty_labels_and_assignees_when_missing() {
286 let json = r#"{
287 "number": 7,
288 "title": "t",
289 "state": "open",
290 "author": {"login": "u", "id": "3"},
291 "createdAt": "2026-03-01T00:00:00Z",
292 "updatedAt": "2026-03-02T00:00:00Z",
293 "url": "https://example.com/7"
294 }"#;
295 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
296 assert!(issue.labels.is_empty());
297 assert!(issue.assignees.is_empty());
298 }
299
300 #[test]
301 fn test_list_issue_args_default_is_empty() {
302 let args = ListIssueArgs::default();
303 assert!(args.state.is_none());
304 assert!(args.labels.is_empty());
305 assert!(args.search.is_none());
306 assert!(args.limit.is_none());
307 }
308
309 #[test]
310 fn test_edit_issue_args_default_is_empty() {
311 let args = EditIssueArgs::default();
312 assert!(args.title.is_none());
313 assert!(args.body.is_none());
314 }
315}