use async_trait::async_trait;
use chrono::{DateTime, Utc};
use gitflow_core::{
CoreError, Result,
issue::{CreateIssueArgs, IssueData, IssueProvider, ListIssueArgs},
types::{CommentData, Label, State, UserSummary},
};
use serde::Deserialize;
use tracing::debug;
use crate::{
error::parse_gitcode_error,
runner::{CommandRunner, RealCommandRunner},
};
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
struct IssueApiResponse {
number: String,
title: String,
body: Option<String>,
state: String,
#[serde(default)]
labels: Option<Vec<LabelApi>>,
user: Option<UserApi>,
#[serde(default)]
assignees: Option<Vec<UserApi>>,
created_at: Option<String>,
updated_at: Option<String>,
html_url: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "lowercase")]
struct LabelApi {
name: String,
#[serde(default)]
color: Option<String>,
#[serde(default)]
description: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
struct UserApi {
login: String,
#[serde(default)]
id: Option<String>,
}
impl From<IssueApiResponse> for IssueData {
fn from(api: IssueApiResponse) -> Self {
Self {
number: api.number.parse().unwrap_or(0),
title: api.title,
body: api.body,
state: match api.state.as_str() {
"closed" => State::Closed,
_ => State::Open,
},
labels: api
.labels
.unwrap_or_default()
.into_iter()
.map(Label::from)
.collect(),
author: api.user.map_or(
UserSummary {
login: "unknown".into(),
id: String::new(),
},
UserSummary::from,
),
assignees: api
.assignees
.unwrap_or_default()
.into_iter()
.map(UserSummary::from)
.collect(),
created_at: api
.created_at
.and_then(|s| {
DateTime::parse_from_rfc3339(&s)
.ok()
.map(|d| d.with_timezone(&Utc))
})
.unwrap_or_else(Utc::now),
updated_at: api
.updated_at
.and_then(|s| {
DateTime::parse_from_rfc3339(&s)
.ok()
.map(|d| d.with_timezone(&Utc))
})
.unwrap_or_else(Utc::now),
url: api.html_url,
}
}
}
impl From<LabelApi> for Label {
fn from(api: LabelApi) -> Self {
Self {
name: api.name,
color: api.color,
description: api.description,
}
}
}
impl From<UserApi> for UserSummary {
fn from(api: UserApi) -> Self {
Self {
login: api.login,
id: api.id.unwrap_or_default(),
}
}
}
#[derive(Debug, Clone, Deserialize)]
struct CommentApiResponse {
#[serde(deserialize_with = "gitflow_core::types::deserialize_u64_or_string")]
id: u64,
#[serde(default)]
body: String,
#[serde(default)]
author: Option<String>,
#[serde(default)]
user: Option<UserApi>,
#[serde(default)]
created_at: Option<String>,
}
impl From<CommentApiResponse> for CommentData {
fn from(api: CommentApiResponse) -> Self {
let author = api.user.map_or_else(
|| UserSummary {
login: api.author.unwrap_or_else(|| "unknown".into()),
id: String::new(),
},
UserSummary::from,
);
let created_at = api.created_at.as_deref().map_or_else(Utc::now, |s| {
DateTime::parse_from_rfc3339(s)
.map(|dt| dt.with_timezone(&Utc))
.or_else(|_| {
chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
.map(|ndt| ndt.and_utc())
})
.unwrap_or_else(|_| Utc::now())
});
Self {
id: api.id,
body: api.body,
author,
created_at,
}
}
}
#[derive(Debug, Clone, Deserialize)]
struct CloseApiResponse {
number: u64,
state: String,
url: String,
}
impl From<CloseApiResponse> for IssueData {
fn from(api: CloseApiResponse) -> Self {
Self {
number: api.number,
title: String::new(),
body: None,
state: match api.state.as_str() {
"closed" => State::Closed,
_ => State::Open,
},
labels: Vec::new(),
author: UserSummary {
login: "unknown".into(),
id: String::new(),
},
assignees: Vec::new(),
created_at: Utc::now(),
updated_at: Utc::now(),
url: api.url,
}
}
}
#[derive(Debug, Clone)]
pub struct GitCodeIssueProvider<R: CommandRunner = RealCommandRunner> {
repo: String,
runner: R,
}
impl GitCodeIssueProvider<RealCommandRunner> {
#[must_use]
pub fn new(repo: impl Into<String>) -> Self {
Self {
repo: repo.into(),
runner: RealCommandRunner,
}
}
}
impl<R: CommandRunner> GitCodeIssueProvider<R> {
#[must_use]
pub fn with_runner(repo: impl Into<String>, runner: R) -> Self {
Self {
repo: repo.into(),
runner,
}
}
async fn ensure_label_exists(&self, name: &str) -> Result<()> {
let binary = crate::gitcode_binary();
debug!(repo = %self.repo, name, "auto-creating missing label via `gc label create`");
let output = self
.runner
.run(
&binary,
&[
"label", "create", name, "--color", "ededed", "-R", &self.repo,
],
)
.await
.map_err(|e| {
CoreError::Platform(format!("Failed to spawn gitcode label create: {e}"))
})?;
if !output.status.success() {
let gitcode_err = parse_gitcode_error(&output.stderr);
tracing::debug!(error = %gitcode_err, "Failed to auto-create label");
return Err(CoreError::Platform(format!(
"Failed to auto-create label '{name}': {}",
gitcode_err.user_message
)));
}
Ok(())
}
}
#[async_trait]
impl<R: CommandRunner + 'static> IssueProvider for GitCodeIssueProvider<R> {
async fn create(&self, args: CreateIssueArgs) -> Result<IssueData> {
let binary = crate::gitcode_binary();
let mut cmd_args: Vec<&str> = vec![
"issue",
"create",
"-R",
&self.repo,
"--title",
&args.title,
"--json",
];
if let Some(body) = &args.body {
cmd_args.push("--body");
cmd_args.push(body);
}
for label in &args.labels {
cmd_args.push("--label");
cmd_args.push(label);
}
for assignee in &args.assignees {
cmd_args.push("--assignee");
cmd_args.push(assignee);
}
debug!(repo = %self.repo, title = %args.title, "spawning gitcode issue create");
let output = self
.runner
.run(&binary, &cmd_args)
.await
.map_err(|e| CoreError::Platform(format!("{e}")))?;
if !output.status.success() {
let missing = extract_missing_labels_from_error(&output.stderr);
if !missing.is_empty() {
debug!(
repo = %self.repo,
missing_count = missing.len(),
"auto-creating missing label(s) before retrying issue create"
);
for label in &missing {
self.ensure_label_exists(label).await?;
}
let retry_output = self.runner.run(&binary, &cmd_args).await.map_err(|e| {
CoreError::Platform(format!("Failed to spawn gitcode on retry: {e}"))
})?;
if !retry_output.status.success() {
return Err(parse_gitcode_error(&retry_output.stderr).into());
}
return serde_json::from_slice::<IssueApiResponse>(&retry_output.stdout)
.map(IssueData::from)
.map_err(CoreError::Serialization);
}
return Err(parse_gitcode_error(&output.stderr).into());
}
serde_json::from_slice::<IssueApiResponse>(&output.stdout)
.map(IssueData::from)
.map_err(CoreError::Serialization)
}
async fn list(&self, args: ListIssueArgs) -> Result<Vec<IssueData>> {
let binary = crate::gitcode_binary();
let mut cmd_args: Vec<&str> = vec!["issue", "list", "-R", &self.repo, "--json"];
if let Some(ref state) = args.state {
cmd_args.push("--state");
cmd_args.push(match state {
State::Open => "open",
State::Closed => "closed",
});
}
if let Some(ref search) = args.search {
cmd_args.push("--search");
cmd_args.push(search);
}
let limit_str = args.limit.map(|limit| limit.to_string());
if let Some(ref limit) = limit_str {
cmd_args.push("--limit");
cmd_args.push(limit);
}
for label in &args.labels {
cmd_args.push("--label");
cmd_args.push(label);
}
debug!(repo = %self.repo, "spawning gitcode issue list");
let output = self
.runner
.run(&binary, &cmd_args)
.await
.map_err(|e| CoreError::Platform(format!("{e}")))?;
if !output.status.success() {
return Err(parse_gitcode_error(&output.stderr).into());
}
let issues: Vec<IssueApiResponse> =
serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;
Ok(issues.into_iter().map(IssueData::from).collect())
}
async fn view(&self, number: u64) -> Result<IssueData> {
let binary = crate::gitcode_binary();
let number_str = number.to_string();
debug!(repo = %self.repo, number, "spawning gitcode issue view");
let output = self
.runner
.run(
&binary,
&["issue", "view", &number_str, "-R", &self.repo, "--json"],
)
.await
.map_err(|e| CoreError::Platform(format!("{e}")))?;
if !output.status.success() {
return Err(parse_gitcode_error(&output.stderr).into());
}
serde_json::from_slice::<IssueApiResponse>(&output.stdout)
.map(IssueData::from)
.map_err(CoreError::Serialization)
}
async fn close(&self, number: u64) -> Result<IssueData> {
let binary = crate::gitcode_binary();
let number_str = number.to_string();
debug!(repo = %self.repo, number, "spawning gitcode issue close");
let output = self
.runner
.run(
&binary,
&[
"issue",
"close",
&number_str,
"-R",
&self.repo,
"--yes",
"--json",
],
)
.await
.map_err(|e| CoreError::Platform(format!("{e}")))?;
if !output.status.success() {
return Err(parse_gitcode_error(&output.stderr).into());
}
serde_json::from_slice::<CloseApiResponse>(&output.stdout)
.map(IssueData::from)
.map_err(CoreError::Serialization)
}
async fn reopen(&self, number: u64) -> Result<IssueData> {
let binary = crate::gitcode_binary();
let number_str = number.to_string();
debug!(repo = %self.repo, number, "spawning gitcode issue reopen");
let output = self
.runner
.run(
&binary,
&[
"issue",
"reopen",
&number_str,
"-R",
&self.repo,
"--yes",
"--json",
],
)
.await
.map_err(|e| CoreError::Platform(format!("{e}")))?;
if !output.status.success() {
return Err(parse_gitcode_error(&output.stderr).into());
}
serde_json::from_slice::<CloseApiResponse>(&output.stdout)
.map(IssueData::from)
.map_err(CoreError::Serialization)
}
async fn comment(&self, number: u64, body: &str) -> Result<CommentData> {
let binary = crate::gitcode_binary();
let number_str = number.to_string();
debug!(repo = %self.repo, number, "spawning `gc issue comment`");
let output = self
.runner
.run(
&binary,
&[
"issue",
"comment",
&number_str,
"-R",
&self.repo,
"--body",
body,
"--json",
],
)
.await
.map_err(|e| CoreError::Platform(format!("Failed to spawn gitcode: {e}")))?;
if !output.status.success() {
return Err(parse_gitcode_error(&output.stderr).into());
}
let api: CommentApiResponse =
serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;
Ok(CommentData::from(api))
}
async fn list_comments(&self, number: u64) -> Result<Vec<CommentData>> {
let binary = crate::gitcode_binary();
debug!(repo = %self.repo, number, "spawning `gitcode api` GET issue comments");
let api_path = format!("/repos/{}/issues/{}/comments", self.repo, number);
let output = self
.runner
.run(&binary, &["api", &api_path])
.await
.map_err(|e| CoreError::Platform(format!("Failed to spawn gitcode api: {e}")))?;
if !output.status.success() {
return Err(parse_gitcode_error(&output.stderr).into());
}
let comments: Vec<CommentApiResponse> =
serde_json::from_slice(&output.stdout).map_err(CoreError::Serialization)?;
Ok(comments.into_iter().map(CommentData::from).collect())
}
async fn add_labels(&self, number: u64, labels: &[String]) -> Result<()> {
if labels.is_empty() {
return Ok(());
}
let binary = crate::gitcode_binary();
let number_str = number.to_string();
let joined = labels.join(",");
debug!(
repo = %self.repo,
number,
label_count = labels.len(),
"spawning `gitcode issue label --add`"
);
let cmd_args: Vec<&str> = vec![
"issue",
"label",
&number_str,
"--add",
&joined,
"-R",
&self.repo,
];
let output = self
.runner
.run(&binary, &cmd_args)
.await
.map_err(|e| CoreError::Platform(format!("Failed to spawn gitcode: {e}")))?;
if output.status.success() {
return Ok(());
}
let missing = extract_missing_labels_from_error(&output.stderr);
if missing.is_empty() {
return Err(parse_gitcode_error(&output.stderr).into());
}
debug!(
repo = %self.repo,
missing_count = missing.len(),
"auto-creating missing label(s) before retry"
);
for label in &missing {
self.ensure_label_exists(label).await?;
}
let retry_output =
self.runner.run(&binary, &cmd_args).await.map_err(|e| {
CoreError::Platform(format!("Failed to spawn gitcode on retry: {e}"))
})?;
if !retry_output.status.success() {
return Err(parse_gitcode_error(&retry_output.stderr).into());
}
Ok(())
}
async fn remove_label(&self, number: u64, label: &str) -> Result<()> {
let binary = crate::gitcode_binary();
let number_str = number.to_string();
debug!(repo = %self.repo, number, label, "spawning `gitcode issue label --remove`");
let output = self
.runner
.run(
&binary,
&[
"issue",
"label",
&number_str,
"--remove",
label,
"-R",
&self.repo,
],
)
.await
.map_err(|e| CoreError::Platform(format!("Failed to spawn gitcode: {e}")))?;
if !output.status.success() {
return Err(parse_gitcode_error(&output.stderr).into());
}
Ok(())
}
}
fn extract_missing_labels_from_error(stderr: &[u8]) -> Vec<String> {
let text = String::from_utf8_lossy(stderr);
let mut labels = Vec::new();
let mut search_from: usize = 0;
while let Some(rel_open) = text[search_from..].find('\'') {
let open_pos = search_from + rel_open + 1;
let Some(rel_close) = text[open_pos..].find('\'') else {
break;
};
let close_pos = open_pos + rel_close;
let after_close = &text[close_pos + 1..];
if after_close.starts_with(" not found") {
let label = text[open_pos..close_pos].to_string();
if !label.is_empty() {
labels.push(label);
}
}
search_from = close_pos + 1;
}
labels
}
#[cfg(test)]
mod tests {
use gitflow_core::types::UserSummary;
use super::*;
use crate::runner::{MockCommandRunner, RecordingMockRunner, SequencedMockCommandRunner};
#[test]
fn test_should_construct_gitcode_issue_provider() {
let provider = GitCodeIssueProvider::new("octocat/hello-world");
assert_eq!(provider.repo, "octocat/hello-world");
}
#[test]
fn test_should_construct_gitcode_issue_provider_from_string() {
let repo = String::from("octocat/hello-world");
let provider = GitCodeIssueProvider::new(repo);
assert_eq!(provider.repo, "octocat/hello-world");
}
#[test]
fn test_should_deserialize_issue_data_from_gc_output() {
let gc_json = br#"{
"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://gitcode.com/octocat/hello-world/issues/42"
}"#;
let issue: IssueData = serde_json::from_slice(gc_json).expect("valid IssueData JSON");
assert_eq!(issue.number, 42);
assert_eq!(issue.title, "Fix login bug");
assert_eq!(issue.state, State::Open);
assert_eq!(issue.labels.len(), 1);
assert_eq!(issue.author.login, "octocat");
assert_eq!(issue.assignees.len(), 1);
assert_eq!(
issue.url,
"https://gitcode.com/octocat/hello-world/issues/42"
);
}
#[test]
fn test_should_deserialize_empty_issue_list_from_gc_output() {
let gc_json = b"[]";
let issues: Vec<IssueData> = serde_json::from_slice(gc_json).expect("valid IssueData list");
assert!(issues.is_empty());
}
#[test]
fn test_should_debug_format_provider() {
let provider = GitCodeIssueProvider::new("octocat/hello-world");
let debug = format!("{provider:?}");
assert!(debug.contains("GitCodeIssueProvider"));
assert!(debug.contains("octocat/hello-world"));
}
#[test]
fn test_should_deserialize_closed_issue_from_gc_close_output() {
let gc_json = br#"{
"number": 10,
"title": "Fixed typo",
"body": null,
"state": "closed",
"labels": [],
"author": {"login": "dev", "id": "5"},
"assignees": [],
"createdAt": "2026-06-01T08:00:00Z",
"updatedAt": "2026-06-02T12:00:00Z",
"url": "https://gitcode.com/octocat/hello-world/issues/10"
}"#;
let issue: IssueData = serde_json::from_slice(gc_json).expect("valid closed IssueData");
assert_eq!(issue.number, 10);
assert_eq!(issue.state, State::Closed);
}
#[test]
fn test_should_deserialize_reopened_issue_from_gc_reopen_output() {
let gc_json = br#"{
"number": 10,
"title": "Fixed typo",
"body": null,
"state": "open",
"labels": [],
"author": {"login": "dev", "id": "5"},
"assignees": [],
"createdAt": "2026-06-01T08:00:00Z",
"updatedAt": "2026-06-03T09:00:00Z",
"url": "https://gitcode.com/octocat/hello-world/issues/10"
}"#;
let issue: IssueData = serde_json::from_slice(gc_json).expect("valid reopened IssueData");
assert_eq!(issue.number, 10);
assert_eq!(issue.state, State::Open);
}
#[test]
fn test_should_deserialize_comment_data_from_gc_comment_output() {
let gc_json = br#"{
"id": "1001",
"body": "Thanks for reporting, looking into it.",
"author": "maintainer",
"created_at": "2026-06-15 14:00:00"
}"#;
let api: CommentApiResponse =
serde_json::from_slice(gc_json).expect("valid CommentApiResponse");
let comment = CommentData::from(api);
assert_eq!(comment.id, 1001);
assert_eq!(comment.body, "Thanks for reporting, looking into it.");
assert_eq!(comment.author.login, "maintainer");
}
#[test]
fn test_should_roundtrip_comment_data_via_serde() {
let comment = CommentData {
id: 77,
body: "reviewed".into(),
author: UserSummary {
login: "alice".into(),
id: "3".to_string(),
},
created_at: "2026-05-01T00:00:00Z".parse().expect("valid date"),
};
let json = serde_json::to_string(&comment).expect("serialize");
let round_tripped: CommentData = serde_json::from_str(&json).expect("deserialize");
assert_eq!(round_tripped.id, comment.id);
assert_eq!(round_tripped.body, comment.body);
assert_eq!(round_tripped.author.login, comment.author.login);
}
#[test]
fn test_should_create_provider_with_different_repos() {
let r1 = GitCodeIssueProvider::new("org/repo-a");
let r2 = GitCodeIssueProvider::new("org/repo-b");
assert_eq!(r1.repo, "org/repo-a");
assert_eq!(r2.repo, "org/repo-b");
}
#[test]
fn test_should_clone_gitcode_issue_provider() {
let original = GitCodeIssueProvider::new("owner/repo");
let cloned = original.clone();
assert_eq!(original.repo, cloned.repo);
}
fn sample_create_args() -> CreateIssueArgs {
CreateIssueArgs {
title: "Bug report".to_string(),
body: Some("Steps to reproduce".to_string()),
labels: vec!["bug".to_string()],
assignees: vec!["alice".to_string()],
}
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_view() {
let runner = MockCommandRunner::failure("issue not found", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.view(999).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[tokio::test]
async fn test_should_return_serialization_error_on_invalid_json_for_view() {
let runner = MockCommandRunner::success("not valid json");
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.view(1).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Serialization(_)
));
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_list() {
let runner = MockCommandRunner::failure("forbidden", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.list(ListIssueArgs::default()).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[tokio::test]
async fn test_should_return_serialization_error_on_invalid_json_for_list() {
let runner = MockCommandRunner::success("invalid");
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.list(ListIssueArgs::default()).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Serialization(_)
));
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_create() {
let runner = MockCommandRunner::failure("validation failed", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.create(sample_create_args()).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[tokio::test]
async fn test_should_return_serialization_error_on_invalid_json_for_create() {
let runner = MockCommandRunner::success("not valid json");
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.create(sample_create_args()).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Serialization(_)
));
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_close() {
let runner = MockCommandRunner::failure("not found", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.close(42).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_reopen() {
let runner = MockCommandRunner::failure("not found", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.reopen(42).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_comment() {
let runner = MockCommandRunner::failure("not found", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.comment(42, "a comment").await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_add_labels() {
let runner = MockCommandRunner::failure("not found", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.add_labels(42, &["bug".to_string()]).await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[tokio::test]
async fn test_should_return_platform_error_when_gc_fails_for_remove_label() {
let runner = MockCommandRunner::failure("not found", 256);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider.remove_label(42, "bug").await;
assert!(matches!(
result.unwrap_err(),
gitflow_core::CoreError::Cli(_)
));
}
#[test]
fn test_should_extract_single_missing_label_from_gc_stderr() {
let stderr =
b"failed to update https://gitcode.com/owner/repo/issues/18: 'type:enhancement' not found";
let missing = extract_missing_labels_from_error(stderr);
assert_eq!(missing, vec!["type:enhancement".to_string()]);
}
#[test]
fn test_should_return_empty_when_no_label_not_found_in_gc_stderr() {
let stderr = b"gitcode: Not logged in";
let missing = extract_missing_labels_from_error(stderr);
assert!(missing.is_empty());
}
#[tokio::test]
async fn test_should_auto_create_label_and_retry_on_add_labels() {
let runner = SequencedMockCommandRunner::from_results(&[
(
false,
"failed to update https://gitcode.com/owner/repo/issues/18: 'type:enhancement' \
not found",
),
(true, ""),
(true, ""),
]);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let result = provider
.add_labels(18, &["type:enhancement".to_string()])
.await;
assert!(result.is_ok(), "expected Ok, got {result:?}");
}
#[tokio::test]
async fn test_should_propagate_error_when_gc_label_create_fails() {
let runner = SequencedMockCommandRunner::from_results(&[
(false, "failed to update ...: 'ghost' not found"),
(false, "gitcode: 403 Forbidden"),
]);
let provider = GitCodeIssueProvider::with_runner("o/r", runner);
let result = provider.add_labels(1, &["ghost".to_string()]).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_should_not_retry_on_non_label_error_gc() {
let runner = SequencedMockCommandRunner::from_results(&[(false, "gitcode: Not logged in")]);
let provider = GitCodeIssueProvider::with_runner("o/r", runner);
let result = provider.add_labels(1, &["bug".to_string()]).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_should_invoke_issue_label_subcommand_for_add_labels() {
let runner = RecordingMockRunner::success("");
let provider = GitCodeIssueProvider::with_runner("o/r", runner.clone());
provider
.add_labels(54, &["type:bug".to_string(), "priority:high".to_string()])
.await
.expect("add_labels should succeed");
let calls = runner.calls();
assert_eq!(calls.len(), 1);
assert_eq!(
calls[0],
vec![
"issue",
"label",
"54",
"--add",
"type:bug,priority:high",
"-R",
"o/r"
],
r"gitcode v0.6.1 的 issue edit 没有 --add-label flag(Issue #90)"
);
}
#[tokio::test]
async fn test_should_return_ok_without_any_call_for_empty_labels() {
let runner = RecordingMockRunner::success("");
let provider = GitCodeIssueProvider::with_runner("o/r", runner.clone());
provider
.add_labels(1, &[])
.await
.expect("empty labels is a no-op");
assert!(runner.calls().is_empty());
}
#[tokio::test]
async fn test_should_invoke_issue_label_subcommand_for_remove_label() {
let runner = RecordingMockRunner::success("");
let provider = GitCodeIssueProvider::with_runner("o/r", runner.clone());
provider
.remove_label(54, "triage:done")
.await
.expect("remove should succeed");
assert_eq!(
runner.calls()[0],
vec![
"issue",
"label",
"54",
"--remove",
"triage:done",
"-R",
"o/r"
]
);
}
#[tokio::test]
async fn test_should_auto_create_missing_label_and_retry_via_issue_label() {
let runner = SequencedMockCommandRunner::from_results(&[
(false, "HTTP 404: 'type:new' not found"),
(true, r#"{"name": "type:new", "color": "ededed"}"#),
(true, ""),
]);
let provider = GitCodeIssueProvider::with_runner("o/r", runner);
provider
.add_labels(18, &["type:new".to_string()])
.await
.expect("should recover by auto-creating the label");
}
#[test]
fn test_should_parse_issue_comment_with_user_object() {
let json = r#"{"id": 12, "body": "hi", "user": {"login": "bob", "id": "u2"}, "created_at": "2026-07-30T12:00:00+08:00"}"#;
let api: CommentApiResponse = serde_json::from_str(json).expect("user-object shape");
let comment: CommentData = api.into();
assert_eq!(comment.id, 12);
assert_eq!(comment.author.login, "bob");
assert_eq!(comment.author.id, "u2");
}
}
#[cfg(test)]
mod contract_tests {
use super::*;
use crate::runner::MockCommandRunner;
#[tokio::test]
async fn test_contract_issue_list_gitcode_v0_6() {
let fixture = include_str!("../tests/fixtures/issue_list_gitcode_v0.6.json");
let runner = MockCommandRunner::success(fixture);
let provider = GitCodeIssueProvider::with_runner("owner/repo", runner);
let issues = provider
.list(ListIssueArgs::default())
.await
.expect("contract fixture must parse");
assert_eq!(issues.len(), 1);
let issue = &issues[0];
assert_eq!(issue.number, 15);
assert!(!issue.title.is_empty());
assert_eq!(issue.state, gitflow_core::types::State::Open);
}
}