From 1a9353c9bfcef94a5bfa870c4cee03e5e9df1648 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Tue, 12 Dec 2023 19:50:55 -0500 Subject: [PATCH 1/2] add issue search --- src/issues.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/issues.rs b/src/issues.rs index 56ee300..d4eef16 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -25,6 +25,17 @@ pub enum IssueCommand { #[clap(long, short)] with_msg: Option>, }, + Search { + query: Option, + #[clap(long, short)] + labels: Option, + #[clap(long, short)] + creator: Option, + #[clap(long, short)] + assignee: Option, + #[clap(long, short)] + state: Option, + }, View { id: u64, #[clap(subcommand)] @@ -35,6 +46,21 @@ pub enum IssueCommand { }, } +#[derive(clap::ValueEnum, Clone, Copy, Debug)] +pub enum State { + Open, + Closed, +} + +impl From for forgejo_api::State { + fn from(value: State) -> Self { + match value { + State::Open => forgejo_api::State::Open, + State::Closed => forgejo_api::State::Closed, + } + } +} + #[derive(Subcommand, Clone, Debug)] pub enum EditCommand { Title { @@ -68,6 +94,7 @@ impl IssueCommand { ViewCommand::Comment { idx } => view_comment(&repo, &api, id, idx).await?, ViewCommand::Comments => view_comments(&repo, &api, id).await?, }, + Search { query, labels, creator, assignee, state } => view_issues(&repo, &api, query, labels, creator, assignee, state).await?, Edit { issue, command } => match command { EditCommand::Title { new_title } => { edit_title(&repo, &api, issue, new_title).await? @@ -127,6 +154,37 @@ async fn view_issue(repo: &RepoInfo, api: &Forgejo, id: u64) -> eyre::Result<()> } Ok(()) } +async fn view_issues( + repo: &RepoInfo, + api: &Forgejo, + query_str: Option, + labels: Option, + creator: Option, + assignee: Option, + state: Option +) -> eyre::Result<()> { + let labels = labels.map(|s| s.split(',').map(|s| s.to_string()).collect::>()).unwrap_or_default(); + let query = forgejo_api::IssueQuery { + query: query_str, + labels, + created_by: creator, + assigned_by: assignee, + state: state.map(|s| s.into()), + ..Default::default() + }; + let issues = api + .get_repo_issues(repo.owner(), repo.name(), query) + .await?; + if issues.len() == 1 { + println!("1 issue"); + } else { + println!("{} issues", issues.len()); + } + for issue in issues { + println!("#{}: {} (by {})", issue.number, issue.title, issue.user.login); + } + Ok(()) +} async fn view_comment(repo: &RepoInfo, api: &Forgejo, id: u64, idx: usize) -> eyre::Result<()> { let comments = api From e365cd35de2881ff1c4e1e0ac9e54fba8ae920b1 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Tue, 12 Dec 2023 20:09:43 -0500 Subject: [PATCH 2/2] format --- src/issues.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/issues.rs b/src/issues.rs index d4eef16..13f9590 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -94,7 +94,13 @@ impl IssueCommand { ViewCommand::Comment { idx } => view_comment(&repo, &api, id, idx).await?, ViewCommand::Comments => view_comments(&repo, &api, id).await?, }, - Search { query, labels, creator, assignee, state } => view_issues(&repo, &api, query, labels, creator, assignee, state).await?, + Search { + query, + labels, + creator, + assignee, + state, + } => view_issues(&repo, &api, query, labels, creator, assignee, state).await?, Edit { issue, command } => match command { EditCommand::Title { new_title } => { edit_title(&repo, &api, issue, new_title).await? @@ -161,9 +167,11 @@ async fn view_issues( labels: Option, creator: Option, assignee: Option, - state: Option + state: Option, ) -> eyre::Result<()> { - let labels = labels.map(|s| s.split(',').map(|s| s.to_string()).collect::>()).unwrap_or_default(); + let labels = labels + .map(|s| s.split(',').map(|s| s.to_string()).collect::>()) + .unwrap_or_default(); let query = forgejo_api::IssueQuery { query: query_str, labels, @@ -181,7 +189,10 @@ async fn view_issues( println!("{} issues", issues.len()); } for issue in issues { - println!("#{}: {} (by {})", issue.number, issue.title, issue.user.login); + println!( + "#{}: {} (by {})", + issue.number, issue.title, issue.user.login + ); } Ok(()) }