codeberg_cli/actions/milestone/
edit.rs

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use crate::actions::GeneralArgs;
use crate::render::spinner::spin_until_ready;
use crate::render::ui::fuzzy_select_with_key;
use crate::render::ui::multi_fuzzy_select_with_key;
use crate::types::context::BergContext;
use crate::types::git::OwnerRepo;
use anyhow::Context;
use forgejo_api::structs::EditMilestoneOption;
use forgejo_api::structs::IssueGetMilestonesListQuery;
use forgejo_api::structs::Milestone;
use forgejo_api::structs::StateType;
use strum::Display;
use strum::VariantArray;

use crate::actions::text_manipulation::edit_prompt_for;
use crate::actions::text_manipulation::input_prompt_for;
use crate::actions::text_manipulation::select_prompt_for;

use super::display_milestone;

use clap::Parser;

/// Edit selected issue
#[derive(Parser, Debug)]
pub struct EditMilestoneArgs {}

#[derive(Display, PartialEq, Eq, VariantArray)]
enum EditableFields {
    Title,
    State,
    Description,
}

impl EditMilestoneArgs {
    pub async fn run(self, general_args: GeneralArgs) -> anyhow::Result<()> {
        let _ = general_args;
        let ctx = BergContext::new(self).await?;

        let OwnerRepo { owner, repo } = ctx.owner_repo()?;
        let milestone = select_milestone(&ctx).await?;
        let milestone_id_or_name = milestone
            .id
            .as_ref()
            .map(|id| id.to_string())
            .or_else(|| milestone.title.clone())
            .context("Selected milestone doesn't have an ID or Name")?;

        let options = create_options(&ctx, &milestone).await?;

        let updated_milestone = ctx
            .client
            .issue_edit_milestone(
                owner.as_str(),
                repo.as_str(),
                milestone_id_or_name.as_str(),
                options,
            )
            .await?;

        tracing::debug!("{updated_milestone:?}");

        Ok(())
    }
}

async fn select_milestone(ctx: &BergContext<EditMilestoneArgs>) -> anyhow::Result<Milestone> {
    let OwnerRepo { owner, repo } = ctx.owner_repo()?;
    let milestones_list = spin_until_ready(ctx.client.issue_get_milestones_list(
        owner.as_str(),
        repo.as_str(),
        IssueGetMilestonesListQuery::default(),
    ))
    .await?;

    if milestones_list.is_empty() {
        anyhow::bail!("No milestones found in this repository");
    }

    fuzzy_select_with_key(
        &milestones_list,
        select_prompt_for("milestone"),
        display_milestone,
    )
    .cloned()
}

async fn create_options(
    ctx: &BergContext<EditMilestoneArgs>,
    milestone: &Milestone,
) -> anyhow::Result<EditMilestoneOption> {
    let selected_update_fields = multi_fuzzy_select_with_key(
        EditableFields::VARIANTS,
        select_prompt_for("options"),
        |_| false,
        |f| f.to_string(),
    )?;

    let mut options = EditMilestoneOption {
        description: None,
        due_on: None,
        state: None,
        title: None,
    };

    if selected_update_fields.contains(&&EditableFields::Title) {
        let current_title = milestone.title.as_ref().cloned();
        options
            .title
            .replace(milestone_title(ctx, current_title).await?);
    }
    if selected_update_fields.contains(&&EditableFields::State) {
        let current_state = milestone.state.as_ref().cloned();
        options
            .state
            .replace(milestone_state(ctx, current_state).await?);
    }
    if selected_update_fields.contains(&&EditableFields::Description) {
        let current_description = milestone.description.as_ref().cloned();
        options
            .description
            .replace(milestone_description(ctx, current_description).await?);
    }

    Ok(options)
}

async fn milestone_title(
    _ctx: &BergContext<EditMilestoneArgs>,
    current_title: Option<String>,
) -> anyhow::Result<String> {
    inquire::Text::new(input_prompt_for("Choose a new milestone title").as_str())
        .with_default(
            current_title
                .as_deref()
                .unwrap_or("Enter a milestone title"),
        )
        .prompt()
        .map_err(anyhow::Error::from)
}

async fn milestone_state(
    _ctx: &BergContext<EditMilestoneArgs>,
    _current_state: Option<StateType>,
) -> anyhow::Result<String> {
    let selected_state = fuzzy_select_with_key(
        &[StateType::Open, StateType::Closed],
        select_prompt_for("states"),
        |f| format!("{f:?}"),
    )
    .cloned()?;
    Ok(format!("{selected_state:?}"))
}

async fn milestone_description(
    _ctx: &BergContext<EditMilestoneArgs>,
    current_description: Option<String>,
) -> anyhow::Result<String> {
    inquire::Editor::new(edit_prompt_for("a description").as_str())
        .with_predefined_text(
            current_description
                .as_deref()
                .unwrap_or("Enter a milestone description"),
        )
        .prompt()
        .map_err(anyhow::Error::from)
}