use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::{
Result,
types::{CommentData, Label, State, UserSummary, deserialize_u64_or_string},
};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IssueData {
#[serde(deserialize_with = "deserialize_u64_or_string")]
pub number: u64,
pub title: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub body: Option<String>,
pub state: State,
#[serde(default)]
pub labels: Vec<Label>,
pub author: UserSummary,
#[serde(default)]
pub assignees: Vec<UserSummary>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub url: String,
}
#[derive(Debug, Clone)]
pub struct CreateIssueArgs {
pub title: String,
pub body: Option<String>,
pub labels: Vec<String>,
pub assignees: Vec<String>,
}
#[derive(Debug, Clone, Default)]
pub struct ListIssueArgs {
pub state: Option<State>,
pub labels: Vec<String>,
pub assignee: Option<String>,
pub search: Option<String>,
pub limit: Option<u32>,
}
#[async_trait]
pub trait IssueProvider: std::fmt::Debug + Send + Sync {
async fn create(&self, args: CreateIssueArgs) -> Result<IssueData>;
async fn list(&self, args: ListIssueArgs) -> Result<Vec<IssueData>>;
async fn view(&self, number: u64) -> Result<IssueData>;
async fn close(&self, number: u64) -> Result<IssueData>;
async fn reopen(&self, number: u64) -> Result<IssueData>;
async fn comment(&self, number: u64, body: &str) -> Result<CommentData>;
async fn list_comments(&self, number: u64) -> Result<Vec<CommentData>>;
async fn add_labels(&self, number: u64, labels: &[String]) -> Result<()>;
async fn remove_label(&self, number: u64, label: &str) -> Result<()>;
}
#[cfg(test)]
mod tests {
use super::*;
fn sample_issue_json() -> &'static str {
r#"{
"number": 42,
"title": "Fix login bug",
"body": "Reproduced on v1.2.3",
"state": "open",
"labels": [
{"name": "bug", "color": "d73a4a", "description": "Something isn't working"}
],
"author": {"login": "octocat", "id": "1"},
"assignees": [{"login": "alice", "id": "7"}],
"createdAt": "2026-01-15T09:30:00Z",
"updatedAt": "2026-01-16T11:00:00Z",
"url": "https://github.com/octocat/hello-world/issues/42"
}"#
}
#[test]
fn test_should_deserialize_issue_data_from_gh_cli_json() {
let json = sample_issue_json();
let issue: IssueData = serde_json::from_str(json).expect("valid IssueData JSON");
assert_eq!(issue.number, 42);
assert_eq!(issue.title, "Fix login bug");
assert_eq!(issue.body.as_deref(), Some("Reproduced on v1.2.3"));
assert_eq!(issue.state, State::Open);
assert_eq!(issue.labels.len(), 1);
assert_eq!(issue.labels[0].name, "bug");
assert_eq!(issue.labels[0].color.as_deref(), Some("d73a4a"));
assert_eq!(issue.author.login, "octocat");
assert_eq!(issue.author.id, "1");
assert_eq!(issue.assignees.len(), 1);
assert_eq!(issue.assignees[0].login, "alice");
assert_eq!(
issue.url,
"https://github.com/octocat/hello-world/issues/42"
);
}
#[test]
fn test_should_roundtrip_issue_data_via_serde() {
let json = sample_issue_json();
let issue: IssueData = serde_json::from_str(json).expect("deserialize");
let re_serialized = serde_json::to_string(&issue).expect("serialize");
let round_tripped: IssueData =
serde_json::from_str(&re_serialized).expect("re-deserialize");
assert_eq!(round_tripped.number, issue.number);
assert_eq!(round_tripped.title, issue.title);
assert_eq!(round_tripped.body, issue.body);
assert_eq!(round_tripped.state, issue.state);
assert_eq!(round_tripped.url, issue.url);
assert_eq!(round_tripped.created_at, issue.created_at);
assert_eq!(round_tripped.updated_at, issue.updated_at);
}
#[test]
fn test_should_deserialize_issue_with_null_optional_body() {
let json = r#"{
"number": 1,
"title": "t",
"body": null,
"state": "closed",
"labels": [],
"author": {"login": "u", "id": "2"},
"assignees": [],
"createdAt": "2026-02-01T00:00:00Z",
"updatedAt": "2026-02-02T00:00:00Z",
"url": "https://example.com/1"
}"#;
let issue: IssueData = serde_json::from_str(json).expect("deserialize");
assert!(issue.body.is_none());
assert_eq!(issue.state, State::Closed);
assert!(issue.labels.is_empty());
assert!(issue.assignees.is_empty());
}
#[test]
fn test_should_omit_none_body_on_serialize() {
let json = sample_issue_json();
let mut issue: IssueData = serde_json::from_str(json).expect("deserialize");
issue.body = None;
let serialized = serde_json::to_string(&issue).expect("serialize");
assert!(!serialized.contains("\"body\":null"));
assert!(!serialized.contains("\"body\": null"));
}
#[test]
fn test_should_default_empty_labels_and_assignees_when_missing() {
let json = r#"{
"number": 7,
"title": "t",
"state": "open",
"author": {"login": "u", "id": "3"},
"createdAt": "2026-03-01T00:00:00Z",
"updatedAt": "2026-03-02T00:00:00Z",
"url": "https://example.com/7"
}"#;
let issue: IssueData = serde_json::from_str(json).expect("deserialize");
assert!(issue.labels.is_empty());
assert!(issue.assignees.is_empty());
}
#[test]
fn test_list_issue_args_default_is_empty() {
let args = ListIssueArgs::default();
assert!(args.state.is_none());
assert!(args.labels.is_empty());
assert!(args.assignee.is_none());
assert!(args.search.is_none());
assert!(args.limit.is_none());
}
}