Merge pull request 'add --web flag to issue create' () from issue-create-web into main

Reviewed-on: https://codeberg.org/Cyborus/forgejo-cli/pulls/136
This commit is contained in:
Cyborus 2024-10-25 16:36:57 +00:00
commit bbcc33dad3

View file

@ -22,11 +22,13 @@ pub struct IssueCommand {
pub enum IssueSubcommand { pub enum IssueSubcommand {
/// Create a new issue on a repo /// Create a new issue on a repo
Create { Create {
title: String, title: Option<String>,
#[clap(long)] #[clap(long)]
body: Option<String>, body: Option<String>,
#[clap(long, short, id = "[HOST/]OWNER/REPO")] #[clap(long, short, id = "[HOST/]OWNER/REPO")]
repo: Option<RepoArg>, repo: Option<RepoArg>,
#[clap(long)]
web: bool,
}, },
/// Edit an issue /// Edit an issue
Edit { Edit {
@ -176,7 +178,8 @@ impl IssueCommand {
repo: _, repo: _,
title, title,
body, 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) { View { id, command } => match command.unwrap_or(ViewCommand::Body) {
ViewCommand::Body => view_issue(repo, &api, id.number).await?, ViewCommand::Body => view_issue(repo, &api, id.number).await?,
ViewCommand::Comment { idx } => view_comment(repo, &api, id.number, idx).await?, ViewCommand::Comment { idx } => view_comment(repo, &api, id.number, idx).await?,
@ -241,9 +244,12 @@ impl IssueCommand {
async fn create_issue( async fn create_issue(
repo: &RepoName, repo: &RepoName,
api: &Forgejo, api: &Forgejo,
title: String, title: Option<String>,
body: Option<String>, body: Option<String>,
web: bool,
) -> eyre::Result<()> { ) -> eyre::Result<()> {
match (title, web) {
(Some(title), false) => {
let body = match body { let body = match body {
Some(body) => body, Some(body) => body,
None => { None => {
@ -277,6 +283,26 @@ async fn create_issue(
.as_ref() .as_ref()
.ok_or_else(|| eyre::eyre!("issue does not have title"))?; .ok_or_else(|| eyre::eyre!("issue does not have title"))?;
eprintln!("created issue #{}: {}", number, 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(()) Ok(())
} }