@@ -76,6 +76,25 @@
#[arg(long)]
body: Option<String>,
},
/// Add a comment to an issue
Comment {
/// Issue number
number: u32,
/// Comment body
#[arg(long)]
body: String,
/// Repository (org/repo)
#[arg(long)]
repo: Option<String>,
},
/// List comments on an issue
Comments {
/// Issue number
number: u32,
/// Repository (org/repo)
#[arg(long)]
repo: Option<String>,
},
}
#[derive(Debug, Deserialize)]
@@ -119,5 +138,7 @@
title,
body,
} => edit_issue(repo.as_deref(), number, title.as_deref(), body.as_deref()).await,
IssueCommand::Comment { number, body, repo } => add_comment(repo.as_deref(), number, &body).await,
IssueCommand::Comments { number, repo } => list_comments(repo.as_deref(), number).await,
}
}
@@ -323,5 +344,44 @@
.await?;
output::success(&format!("Updated issue #{number}"));
Ok(())
}
async fn add_comment(repo: Option<&str>, number: u32, body: &str) -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_config()?;
let (org, name) = config::resolve_repo(repo)?;
let _resp: serde_json::Value = client
.post(&format!("/{org}/{name}/issues/{number}/comments"), &serde_json::json!({"body": body}))
.await?;
output::success(&format!("Added comment to issue #{number}"));
Ok(())
}
async fn list_comments(repo: Option<&str>, number: u32) -> Result<(), Box<dyn std::error::Error>> {
let client = Client::from_config()?;
let (org, name) = config::resolve_repo(repo)?;
let resp: serde_json::Value = client
.get(&format!("/{org}/{name}/issues/{number}/comments"))
.await?;
let comments: Vec<serde_json::Value> = resp
.get("comments")
.and_then(|v| serde_json::from_value(v.clone()).ok())
.unwrap_or_default();
output::header(&format!("Comments on issue #{number}"));
if comments.is_empty() {
println!(" (no comments)");
return Ok(());
}
for comment in &comments {
let author = comment.pointer("/author/email").and_then(|v| v.as_str()).unwrap_or("unknown");
let time = comment.get("inserted_at").and_then(|v| v.as_str()).map(|t| output::format_time(t)).unwrap_or_default();
let body = comment.get("body").and_then(|v| v.as_str()).unwrap_or("");
println!("\n {} — {}", author, time);
println!(" {body}");
}
Ok(())
}