codeberg_cli/actions/issue/
comment.rs

1use anyhow::Context;
2use forgejo_api::structs::{CreateIssueCommentOption, IssueListIssuesQuery};
3
4use crate::actions::issue::display_issue;
5
6use crate::actions::text_manipulation::select_prompt_for;
7use crate::actions::GeneralArgs;
8use crate::render::spinner::spin_until_ready;
9use crate::render::ui::fuzzy_select_with_key;
10use crate::types::context::BergContext;
11use crate::types::git::OwnerRepo;
12
13use clap::Parser;
14
15/// Add a comment to selected issue
16#[derive(Parser, Debug)]
17pub struct CommentIssueArgs {}
18
19impl CommentIssueArgs {
20    pub async fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
21        let _ = general_args;
22        let ctx = BergContext::new(self, general_args).await?;
23        let OwnerRepo { owner, repo } = ctx.owner_repo()?;
24        let issues_list = spin_until_ready(ctx.client.issue_list_issues(
25            owner.as_str(),
26            repo.as_str(),
27            IssueListIssuesQuery::default(),
28        ))
29        .await
30        .map_err(anyhow::Error::from)?;
31
32        let selected_issue =
33            fuzzy_select_with_key(&issues_list, select_prompt_for("issue"), display_issue)?;
34
35        let (nr, title) = {
36            (
37                selected_issue.id.context("Selected issue had no id")?,
38                selected_issue
39                    .title
40                    .as_ref()
41                    .cloned()
42                    .context("Selected issue had no title")?,
43            )
44        };
45
46        let body = get_comment_input(&ctx, title)?;
47        let comment = ctx
48            .client
49            .issue_create_comment(
50                owner.as_str(),
51                repo.as_str(),
52                nr as u64,
53                CreateIssueCommentOption {
54                    body,
55                    updated_at: None,
56                },
57            )
58            .await?;
59
60        tracing::debug!("{comment:?}");
61
62        Ok(())
63    }
64}
65
66fn get_comment_input(
67    ctx: &BergContext<CommentIssueArgs>,
68    issue_title: impl AsRef<str>,
69) -> anyhow::Result<String> {
70    ctx.editor_for(
71        "a comment",
72        format!("Write a comment for issue \"{}\"", issue_title.as_ref()).as_str(),
73    )
74}