1use async_trait::async_trait;
8use chrono::{DateTime, Utc};
9use serde::{Deserialize, Serialize};
10
11use crate::{
12 Result,
13 types::{CommentData, Label, State, UserSummary, deserialize_u64_or_string},
14};
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
21#[serde(rename_all = "camelCase")]
22pub struct IssueData {
23 #[serde(deserialize_with = "deserialize_u64_or_string")]
25 pub number: u64,
26 pub title: String,
28 #[serde(skip_serializing_if = "Option::is_none")]
30 pub body: Option<String>,
31 pub state: State,
33 #[serde(default)]
35 pub labels: Vec<Label>,
36 pub author: UserSummary,
38 #[serde(default)]
40 pub assignees: Vec<UserSummary>,
41 pub created_at: DateTime<Utc>,
43 pub updated_at: DateTime<Utc>,
45 pub url: String,
47}
48
49#[derive(Debug, Clone)]
51pub struct CreateIssueArgs {
52 pub title: String,
54 pub body: Option<String>,
56 pub labels: Vec<String>,
58 pub assignees: Vec<String>,
60}
61
62#[derive(Debug, Clone, Default)]
66pub struct ListIssueArgs {
67 pub state: Option<State>,
69 pub labels: Vec<String>,
71 pub assignee: Option<String>,
73 pub search: Option<String>,
75 pub limit: Option<u32>,
77}
78
79#[async_trait]
90pub trait IssueProvider: std::fmt::Debug + Send + Sync {
91 async fn create(&self, args: CreateIssueArgs) -> Result<IssueData>;
97
98 async fn list(&self, args: ListIssueArgs) -> Result<Vec<IssueData>>;
104
105 async fn view(&self, number: u64) -> Result<IssueData>;
111
112 async fn close(&self, number: u64) -> Result<IssueData>;
118
119 async fn reopen(&self, number: u64) -> Result<IssueData>;
125
126 async fn comment(&self, number: u64, body: &str) -> Result<CommentData>;
132
133 async fn list_comments(&self, number: u64) -> Result<Vec<CommentData>>;
139
140 async fn add_labels(&self, number: u64, labels: &[String]) -> Result<()>;
146
147 async fn remove_label(&self, number: u64, label: &str) -> Result<()>;
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 fn sample_issue_json() -> &'static str {
161 r#"{
162 "number": 42,
163 "title": "Fix login bug",
164 "body": "Reproduced on v1.2.3",
165 "state": "open",
166 "labels": [
167 {"name": "bug", "color": "d73a4a", "description": "Something isn't working"}
168 ],
169 "author": {"login": "octocat", "id": "1"},
170 "assignees": [{"login": "alice", "id": "7"}],
171 "createdAt": "2026-01-15T09:30:00Z",
172 "updatedAt": "2026-01-16T11:00:00Z",
173 "url": "https://github.com/octocat/hello-world/issues/42"
174 }"#
175 }
176
177 #[test]
178 fn test_should_deserialize_issue_data_from_gh_cli_json() {
179 let json = sample_issue_json();
180 let issue: IssueData = serde_json::from_str(json).expect("valid IssueData JSON");
181
182 assert_eq!(issue.number, 42);
183 assert_eq!(issue.title, "Fix login bug");
184 assert_eq!(issue.body.as_deref(), Some("Reproduced on v1.2.3"));
185 assert_eq!(issue.state, State::Open);
186 assert_eq!(issue.labels.len(), 1);
187 assert_eq!(issue.labels[0].name, "bug");
188 assert_eq!(issue.labels[0].color.as_deref(), Some("d73a4a"));
189 assert_eq!(issue.author.login, "octocat");
190 assert_eq!(issue.author.id, "1");
191 assert_eq!(issue.assignees.len(), 1);
192 assert_eq!(issue.assignees[0].login, "alice");
193 assert_eq!(
194 issue.url,
195 "https://github.com/octocat/hello-world/issues/42"
196 );
197 }
198
199 #[test]
200 fn test_should_roundtrip_issue_data_via_serde() {
201 let json = sample_issue_json();
202 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
203 let re_serialized = serde_json::to_string(&issue).expect("serialize");
204 let round_tripped: IssueData =
205 serde_json::from_str(&re_serialized).expect("re-deserialize");
206
207 assert_eq!(round_tripped.number, issue.number);
208 assert_eq!(round_tripped.title, issue.title);
209 assert_eq!(round_tripped.body, issue.body);
210 assert_eq!(round_tripped.state, issue.state);
211 assert_eq!(round_tripped.url, issue.url);
212 assert_eq!(round_tripped.created_at, issue.created_at);
213 assert_eq!(round_tripped.updated_at, issue.updated_at);
214 }
215
216 #[test]
217 fn test_should_deserialize_issue_with_null_optional_body() {
218 let json = r#"{
219 "number": 1,
220 "title": "t",
221 "body": null,
222 "state": "closed",
223 "labels": [],
224 "author": {"login": "u", "id": "2"},
225 "assignees": [],
226 "createdAt": "2026-02-01T00:00:00Z",
227 "updatedAt": "2026-02-02T00:00:00Z",
228 "url": "https://example.com/1"
229 }"#;
230 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
231 assert!(issue.body.is_none());
232 assert_eq!(issue.state, State::Closed);
233 assert!(issue.labels.is_empty());
234 assert!(issue.assignees.is_empty());
235 }
236
237 #[test]
238 fn test_should_omit_none_body_on_serialize() {
239 let json = sample_issue_json();
240 let mut issue: IssueData = serde_json::from_str(json).expect("deserialize");
241 issue.body = None;
242 let serialized = serde_json::to_string(&issue).expect("serialize");
243 assert!(!serialized.contains("\"body\":null"));
245 assert!(!serialized.contains("\"body\": null"));
246 }
247
248 #[test]
249 fn test_should_default_empty_labels_and_assignees_when_missing() {
250 let json = r#"{
251 "number": 7,
252 "title": "t",
253 "state": "open",
254 "author": {"login": "u", "id": "3"},
255 "createdAt": "2026-03-01T00:00:00Z",
256 "updatedAt": "2026-03-02T00:00:00Z",
257 "url": "https://example.com/7"
258 }"#;
259 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
260 assert!(issue.labels.is_empty());
261 assert!(issue.assignees.is_empty());
262 }
263
264 #[test]
265 fn test_list_issue_args_default_is_empty() {
266 let args = ListIssueArgs::default();
267 assert!(args.state.is_none());
268 assert!(args.labels.is_empty());
269 assert!(args.assignee.is_none());
270 assert!(args.search.is_none());
271 assert!(args.limit.is_none());
272 }
273}