use crate::cli::common::RepoContext;
use crate::cli::issue_like::latest_comment_by_current_user;
use crate::error::Result;
use crate::models::comment::{Comment, CreateComment};
use crate::output;
pub(in crate::cli::pr) async fn comment(
hostname: &Option<String>,
cli_repo: &Option<String>,
cli_profile: &Option<String>,
number: u64,
body: crate::cli::input::BodyInput,
edit_last: bool,
json: bool,
) -> Result<()> {
let ctx = RepoContext::resolve(hostname, cli_repo, cli_profile)?;
let editing = edit_last && body.is_editor();
let existing = if editing {
super::super::target::require_pull_request(&ctx, number).await?;
Some(
latest_comment_by_current_user(&ctx.client, &ctx.owner, &ctx.repo, number, "PR")
.await?,
)
} else {
None
};
let seed = existing
.as_ref()
.and_then(|comment| comment.body.as_deref())
.unwrap_or_default();
let body_text = crate::cli::input::comment_seed(body, seed).await?;
if !editing {
super::super::target::require_pull_request(&ctx, number).await?;
}
let comment_body = CreateComment { body: body_text };
if edit_last {
let comment = match existing {
Some(comment) => comment,
None => {
latest_comment_by_current_user(&ctx.client, &ctx.owner, &ctx.repo, number, "PR")
.await?
}
};
let comment = ctx
.client
.update_issue_comment(&ctx.owner, &ctx.repo, comment.id, &comment_body)
.await?;
print_comment_result(&comment, number, true, json)?;
} else {
let comment = ctx
.client
.create_pr_comment(&ctx.owner, &ctx.repo, number, &comment_body)
.await?;
print_comment_result(&comment, number, false, json)?;
}
Ok(())
}
fn print_comment_result(comment: &Comment, pr_number: u64, edited: bool, json: bool) -> Result<()> {
if json {
return output::print_json(comment);
}
let action = if edited { "Edited" } else { "Added" };
println!("✓ {} comment {} on PR #{}", action, comment.id, pr_number);
if let Some(url) = comment.html_url.as_deref().filter(|url| !url.is_empty()) {
println!("URL: {}", url);
}
Ok(())
}