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 add_labels(&self, number: u64, labels: &[String]) -> Result<()>;
139
140 async fn remove_label(&self, number: u64, label: &str) -> Result<()>;
146}
147
148#[cfg(test)]
149mod tests {
150 use super::*;
151
152 fn sample_issue_json() -> &'static str {
154 r#"{
155 "number": 42,
156 "title": "Fix login bug",
157 "body": "Reproduced on v1.2.3",
158 "state": "open",
159 "labels": [
160 {"name": "bug", "color": "d73a4a", "description": "Something isn't working"}
161 ],
162 "author": {"login": "octocat", "id": "1"},
163 "assignees": [{"login": "alice", "id": "7"}],
164 "createdAt": "2026-01-15T09:30:00Z",
165 "updatedAt": "2026-01-16T11:00:00Z",
166 "url": "https://github.com/octocat/hello-world/issues/42"
167 }"#
168 }
169
170 #[test]
171 fn test_should_deserialize_issue_data_from_gh_cli_json() {
172 let json = sample_issue_json();
173 let issue: IssueData = serde_json::from_str(json).expect("valid IssueData JSON");
174
175 assert_eq!(issue.number, 42);
176 assert_eq!(issue.title, "Fix login bug");
177 assert_eq!(issue.body.as_deref(), Some("Reproduced on v1.2.3"));
178 assert_eq!(issue.state, State::Open);
179 assert_eq!(issue.labels.len(), 1);
180 assert_eq!(issue.labels[0].name, "bug");
181 assert_eq!(issue.labels[0].color.as_deref(), Some("d73a4a"));
182 assert_eq!(issue.author.login, "octocat");
183 assert_eq!(issue.author.id, "1");
184 assert_eq!(issue.assignees.len(), 1);
185 assert_eq!(issue.assignees[0].login, "alice");
186 assert_eq!(
187 issue.url,
188 "https://github.com/octocat/hello-world/issues/42"
189 );
190 }
191
192 #[test]
193 fn test_should_roundtrip_issue_data_via_serde() {
194 let json = sample_issue_json();
195 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
196 let re_serialized = serde_json::to_string(&issue).expect("serialize");
197 let round_tripped: IssueData =
198 serde_json::from_str(&re_serialized).expect("re-deserialize");
199
200 assert_eq!(round_tripped.number, issue.number);
201 assert_eq!(round_tripped.title, issue.title);
202 assert_eq!(round_tripped.body, issue.body);
203 assert_eq!(round_tripped.state, issue.state);
204 assert_eq!(round_tripped.url, issue.url);
205 assert_eq!(round_tripped.created_at, issue.created_at);
206 assert_eq!(round_tripped.updated_at, issue.updated_at);
207 }
208
209 #[test]
210 fn test_should_deserialize_issue_with_null_optional_body() {
211 let json = r#"{
212 "number": 1,
213 "title": "t",
214 "body": null,
215 "state": "closed",
216 "labels": [],
217 "author": {"login": "u", "id": "2"},
218 "assignees": [],
219 "createdAt": "2026-02-01T00:00:00Z",
220 "updatedAt": "2026-02-02T00:00:00Z",
221 "url": "https://example.com/1"
222 }"#;
223 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
224 assert!(issue.body.is_none());
225 assert_eq!(issue.state, State::Closed);
226 assert!(issue.labels.is_empty());
227 assert!(issue.assignees.is_empty());
228 }
229
230 #[test]
231 fn test_should_omit_none_body_on_serialize() {
232 let json = sample_issue_json();
233 let mut issue: IssueData = serde_json::from_str(json).expect("deserialize");
234 issue.body = None;
235 let serialized = serde_json::to_string(&issue).expect("serialize");
236 assert!(!serialized.contains("\"body\":null"));
238 assert!(!serialized.contains("\"body\": null"));
239 }
240
241 #[test]
242 fn test_should_default_empty_labels_and_assignees_when_missing() {
243 let json = r#"{
244 "number": 7,
245 "title": "t",
246 "state": "open",
247 "author": {"login": "u", "id": "3"},
248 "createdAt": "2026-03-01T00:00:00Z",
249 "updatedAt": "2026-03-02T00:00:00Z",
250 "url": "https://example.com/7"
251 }"#;
252 let issue: IssueData = serde_json::from_str(json).expect("deserialize");
253 assert!(issue.labels.is_empty());
254 assert!(issue.assignees.is_empty());
255 }
256
257 #[test]
258 fn test_list_issue_args_default_is_empty() {
259 let args = ListIssueArgs::default();
260 assert!(args.state.is_none());
261 assert!(args.labels.is_empty());
262 assert!(args.assignee.is_none());
263 assert!(args.search.is_none());
264 assert!(args.limit.is_none());
265 }
266}