From ccd4efa10b16f29207d7f5f854bc8a1b55439f76 Mon Sep 17 00:00:00 2001 From: Gene Pasquet Date: Fri, 3 Oct 2025 10:32:08 +0100 Subject: [PATCH] Add git command to create branch when creating a issue --- src/git.rs | 17 +++++++++++++++++ src/issue.rs | 9 ++++++--- src/main.rs | 13 +++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/git.rs b/src/git.rs index 1c7aca9..7b480fd 100644 --- a/src/git.rs +++ b/src/git.rs @@ -13,3 +13,20 @@ pub fn get_branch() -> Result { Err(String::from_utf8(output.stderr).unwrap()) } } + +pub fn create_branch(branch_name: String) -> Result { + let output = std::process::Command::new("git") + .arg("checkout") + .arg("-b") + .arg(branch_name) + .output() + .map_err(|e| e.to_string())?; + + if output.status.success() { + String::from_utf8(output.stdout) + .map(|s| s.trim().to_string()) + .map_err(|e| e.to_string()) + } else { + Err(String::from_utf8(output.stderr).unwrap()) + } +} diff --git a/src/issue.rs b/src/issue.rs index 5358513..d62cacd 100644 --- a/src/issue.rs +++ b/src/issue.rs @@ -433,7 +433,7 @@ pub fn create( state: State, assignee_id: String, priority: Priority, -) -> Result { +) -> Result<(String, String), String> { let priority = priority::priority_to_int(&priority); let response = request::Gql::new(config, token, ISSUE_CREATE_DOC) .put_string("title", title) @@ -449,7 +449,7 @@ pub fn create( url, branch_name, .. } = issue_create_response(response)?; - Ok(format!("{url}\n{branch_name}")) + Ok((branch_name.clone(), format!("{url}\n{branch_name}"))) } pub fn list( @@ -702,7 +702,10 @@ mod tests { ); assert_eq!( result, - Ok("https://linear.app/vardy/issue/BE-3354/test\nbe-3354-test".to_string()) + Ok(( + "be-3354-test".to_string(), + "https://linear.app/vardy/issue/BE-3354/test\nbe-3354-test".to_string() + )) ); mock.assert(); } diff --git a/src/main.rs b/src/main.rs index 0f6747e..d12a970 100644 --- a/src/main.rs +++ b/src/main.rs @@ -251,6 +251,19 @@ fn issue_create(cli: Cli, args: &IssueCreate) -> Result { viewer.id, priority, ) + .map(|(branch_name, url)| { + match git::create_branch(branch_name.clone()) { + Ok(_) => { + let success_msg = format!("✓ Created git branch: {}", branch_name); + eprintln!("{}", success_msg.green()); + } + Err(e) => { + let warning_msg = format!("⚠ Could not create git branch '{}': {}", branch_name, e); + eprintln!("{}", warning_msg.yellow()); + } + } + url + }) } fn issue_view(cli: Cli, args: &IssueView) -> Result {