From f3778725c8d56ebab62939adca05bb12642ab04f Mon Sep 17 00:00:00 2001 From: Cyborus Date: Fri, 25 Oct 2024 12:31:54 -0400 Subject: feat: add `--web` flag to `issue create` --- src/issues.rs | 96 +++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 35 deletions(-) diff --git a/src/issues.rs b/src/issues.rs index 091f221..86785a2 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -22,11 +22,13 @@ pub struct IssueCommand { pub enum IssueSubcommand { /// Create a new issue on a repo Create { - title: String, + title: Option, #[clap(long)] body: Option, #[clap(long, short, id = "[HOST/]OWNER/REPO")] repo: Option, + #[clap(long)] + web: bool, }, /// Edit an issue Edit { @@ -176,7 +178,8 @@ impl IssueCommand { repo: _, title, body, - } => create_issue(repo, &api, title, body).await?, + web, + } => create_issue(repo, &api, title, body, web).await?, View { id, command } => match command.unwrap_or(ViewCommand::Body) { ViewCommand::Body => view_issue(repo, &api, id.number).await?, ViewCommand::Comment { idx } => view_comment(repo, &api, id.number, idx).await?, @@ -241,42 +244,65 @@ impl IssueCommand { async fn create_issue( repo: &RepoName, api: &Forgejo, - title: String, + title: Option, body: Option, + web: bool, ) -> eyre::Result<()> { - let body = match body { - Some(body) => body, - None => { - let mut body = String::new(); - crate::editor(&mut body, Some("md")).await?; - body + match (title, web) { + (Some(title), false) => { + let body = match body { + Some(body) => body, + None => { + let mut body = String::new(); + crate::editor(&mut body, Some("md")).await?; + body + } + }; + let issue = api + .issue_create_issue( + repo.owner(), + repo.name(), + CreateIssueOption { + body: Some(body), + title, + assignee: None, + assignees: None, + closed: None, + due_date: None, + labels: None, + milestone: None, + r#ref: None, + }, + ) + .await?; + let number = issue + .number + .ok_or_else(|| eyre::eyre!("issue does not have number"))?; + let title = issue + .title + .as_ref() + .ok_or_else(|| eyre::eyre!("issue does not have title"))?; + eprintln!("created issue #{}: {}", number, title); } - }; - let issue = api - .issue_create_issue( - repo.owner(), - repo.name(), - CreateIssueOption { - body: Some(body), - title, - assignee: None, - assignees: None, - closed: None, - due_date: None, - labels: None, - milestone: None, - r#ref: None, - }, - ) - .await?; - let number = issue - .number - .ok_or_else(|| eyre::eyre!("issue does not have number"))?; - let title = issue - .title - .as_ref() - .ok_or_else(|| eyre::eyre!("issue does not have title"))?; - eprintln!("created issue #{}: {}", number, title); + (None, true) => { + let base_repo = api.repo_get(repo.owner(), repo.name()).await?; + let mut issue_create_url = base_repo + .html_url + .clone() + .ok_or_eyre("repo does not have html url")?; + issue_create_url + .path_segments_mut() + .expect("invalid url") + .extend(["issues", "new"]); + open::that(issue_create_url.as_str())?; + } + (None, false) => { + eyre::bail!("requires either issue title or --web flag") + } + (Some(_), true) => { + eyre::bail!("issue title and --web flag are mutually exclusive") + } + } Ok(()) } -- cgit v1.2.3