summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCyborus <cyborus@noreply.codeberg.org>2024-10-25 18:36:57 +0200
committerCyborus <cyborus@noreply.codeberg.org>2024-10-25 18:36:57 +0200
commitbbcc33dad3af7db703728c175662247518bf3c42 (patch)
treeab32de48454947ddf9a050205e80d7311a7ed76f
parentMerge pull request 'add user agent' (#135) from user-agent into main (diff)
parentfeat: add `--web` flag to `issue create` (diff)
downloadforgejo-cli-bbcc33dad3af7db703728c175662247518bf3c42.tar.xz
forgejo-cli-bbcc33dad3af7db703728c175662247518bf3c42.zip
Merge pull request 'add `--web` flag to `issue create`' (#136) from issue-create-web into main
Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/136
-rw-r--r--src/issues.rs96
1 files 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<String>,
#[clap(long)]
body: Option<String>,
#[clap(long, short, id = "[HOST/]OWNER/REPO")]
repo: Option<RepoArg>,
+ #[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<String>,
body: Option<String>,
+ 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(())
}