1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use cod_cli::pull_request::comment::CommentPullRequestArgs;
use cod_client::CodebergClient;
use cod_render::spinner::spin_until_ready;
use cod_render::ui::fuzzy_select_with_key;
use cod_types::api::create_options::create_comment_option::CreateCommentOption;

use crate::text_manipulation::{edit_prompt_for, select_prompt_for};

pub async fn comment_pull(
    _args: CommentPullRequestArgs,
    client: &CodebergClient,
) -> anyhow::Result<()> {
    let pull_requests_list = spin_until_ready(client.get_repo_prs(None, None)).await?;

    let selected_pull_request =
        fuzzy_select_with_key(pull_requests_list, select_prompt_for("pull request"))?;

    if let Some(pull_request) = selected_pull_request {
        let body = get_comment_input(pull_request.title.as_str())?;

        let comment = client
            .post_comment_for_id(pull_request.number, body)
            .await?;

        println!("Posted comment: {comment:?}");
    }
    Ok(())
}

fn get_comment_input(pull_request_title: &str) -> anyhow::Result<CreateCommentOption> {
    let comment = inquire::Editor::new(edit_prompt_for("a comment").as_str())
        .with_predefined_text(
            format!(
                "Write a comment for pull_request \"{}\"",
                pull_request_title
            )
            .as_str(),
        )
        .prompt()?;
    Ok(CreateCommentOption::new(comment))
}