Skip to main content

ghl/cmds/
branch.rs

1//! Issue branch creation command.
2
3use std::fmt::Display;
4use std::fmt::Formatter;
5use std::ops::Deref;
6
7use owo_colors::OwoColorize;
8use ytil_gh::issue::ListedIssue;
9
10struct RenderableListedIssue(pub ListedIssue);
11
12impl Deref for RenderableListedIssue {
13    type Target = ListedIssue;
14
15    fn deref(&self) -> &Self::Target {
16        &self.0
17    }
18}
19
20impl Display for RenderableListedIssue {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        write!(
23            f,
24            // The spacing before the title is required to align it with the first line.
25            "{} {} {}",
26            self.author.login.blue().bold(),
27            self.updated_at.strftime("%d-%m-%Y %H:%M UTC"),
28            self.title
29        )
30    }
31}
32
33/// Interactively create a GitHub branch from a selected issue.
34pub fn run() -> rootcause::Result<()> {
35    let issues = ytil_gh::issue::list()?;
36
37    let Some(issue) = ytil_tui::minimal_select(issues.into_iter().map(RenderableListedIssue).collect())? else {
38        return Ok(());
39    };
40
41    let Some(checkout_branch) = ytil_tui::yes_no_select("Checkout branch?")? else {
42        return Ok(());
43    };
44
45    let develop_output = ytil_gh::issue::develop(&issue.number.to_string(), checkout_branch)?;
46    println!(
47        "{} with name={:?}",
48        "Branch created".green().bold(),
49        develop_output.branch_name
50    );
51
52    Ok(())
53}