From 0c459e2770ae299ab34ad2302f3ab5b1bbd88515 Mon Sep 17 00:00:00 2001 From: Daan Boerlage Date: Tue, 21 Jan 2025 20:44:38 +0100 Subject: [PATCH] Add a search command --- readme.md | 34 +++++++++++++++++++++++++++++----- src/cli.rs | 24 +++++++++++++++++------- src/cmd.rs | 1 + src/cmd/search.rs | 32 ++++++++++++++++++++++++++++++++ src/main.rs | 1 + 5 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 src/cmd/search.rs diff --git a/readme.md b/readme.md index 9481120..e957457 100644 --- a/readme.md +++ b/readme.md @@ -24,9 +24,10 @@ cargo install jirac Usage: jirac Commands: - create Create a ticket - list Find tickets currently assigned to you - init Setup the configuration + create Create an issue + list Find issues currently assigned to you + search Search for issues + init Set up the configuration help Print this message or the help of the given subcommand(s) Options: @@ -68,7 +69,7 @@ jirac create ticket.md jirac create --project KEY ``` -## Listing tickets +### Listing tickets ``` Find tickets currently assigned to you @@ -80,6 +81,29 @@ Options: -h, --help Print help ``` +### Search for tickets + +``` +Search for issues + +Usage: jirac search [OPTIONS] + +Arguments: + A JQL string + +Options: + --json Print JSON rather than pretty print + -h, --help Print help +``` + +Use [JQL](https://support.atlassian.com/jira-software-cloud/docs/use-advanced-search-with-jira-query-language-jql/) to search for Issues. + +*Find all in-progress tickets in a project* + +``` +jirac search 'project = KEY AND status = "In Progress" ORDER BY created DESC' +``` + ## Configuration Get the following information: @@ -88,7 +112,7 @@ Get the following information: * You email * [An API key](https://id.atlassian.com/manage-profile/security/api-tokens) -Then run the the `jirac init` command +Then run the `jirac init` command ``` Setup the configuration diff --git a/src/cli.rs b/src/cli.rs index 136e472..818a9fb 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -10,27 +10,37 @@ pub struct Cli { #[derive(Subcommand)] pub enum Commands { - /// Create a ticket + /// Create an issue Create { - /// The project key in which to create the ticket + /// The project key in which to create the issue #[arg(long)] project: Option, - /// Open the new ticket in a browser + /// Open the new issue in a browser #[arg(long)] open: bool, - /// A markdown file + /// A Markdown file #[arg(value_name = "MARKDOWN_FILE")] markdown_file: Option, }, - /// Find tickets currently assigned to you + /// Find issues currently assigned to you List { - /// Print json rather than pretty print + /// Print JSON rather than pretty print #[arg(long)] json: bool, }, - /// Setup the configuration + /// Search for issues + Search { + /// Print JSON rather than pretty print + #[arg(long)] + json: bool, + + /// A JQL string + #[arg(value_name = "JQL")] + jql: String, + }, + /// Set up the configuration Init { /// Jira instance URL #[arg(long)] diff --git a/src/cmd.rs b/src/cmd.rs index fa6613e..ab17459 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -1,2 +1,3 @@ pub mod create; pub mod list; +pub mod search; diff --git a/src/cmd/search.rs b/src/cmd/search.rs new file mode 100644 index 0000000..1a4bef1 --- /dev/null +++ b/src/cmd/search.rs @@ -0,0 +1,32 @@ +use crate::jira_config::JiraConfig; +use crate::types::issue::{display_issues_json, display_issues_pretty}; + +pub async fn exec(json: bool, jql: String) -> Result<(), Box> { + let config = JiraConfig::load().map_err(|e| format!("Configuration error: {}", e))?; + if !json { + println!("Searching for issues..."); + } + + match crate::jql::run(&config, &jql).await { + Ok(response) => { + if json { + if response.issues.is_empty() { + println!("[]"); + } else { + display_issues_json(&response.issues)?; + } + } else if response.issues.is_empty() { + println!("No results found for query."); + } else { + display_issues_pretty(&response.issues)?; + println!("Total issues: {}", response.total); + } + } + Err(e) => { + eprintln!("Error fetching issues: {}", e); + std::process::exit(1); + } + } + + Ok(()) +} diff --git a/src/main.rs b/src/main.rs index 57173bc..bedd888 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ async fn main() -> Result<(), Box> { markdown_file, } => cmd::create::create(project, open, markdown_file).await?, Commands::List { json } => cmd::list::list(json).await?, + Commands::Search { json, jql } => cmd::search::exec(json, jql).await?, Commands::Init { url, email, token } => { JiraConfig::init(url, email, token).await?; println!("Configuration initialized successfully!");