Add a search command

This commit is contained in:
Daan Boerlage 2025-01-21 20:44:38 +01:00
parent 8423cc1240
commit 0c459e2770
Signed by: daan
GPG key ID: FCE070E1E4956606
5 changed files with 80 additions and 12 deletions

View file

@ -24,8 +24,9 @@ cargo install jirac
Usage: jirac <COMMAND> Usage: jirac <COMMAND>
Commands: Commands:
create Create a ticket create Create an issue
list Find tickets currently assigned to you list Find issues currently assigned to you
search Search for issues
init Set up the configuration init Set up the configuration
help Print this message or the help of the given subcommand(s) help Print this message or the help of the given subcommand(s)
@ -68,7 +69,7 @@ jirac create ticket.md
jirac create --project KEY jirac create --project KEY
``` ```
## Listing tickets ### Listing tickets
``` ```
Find tickets currently assigned to you Find tickets currently assigned to you
@ -80,6 +81,29 @@ Options:
-h, --help Print help -h, --help Print help
``` ```
### Search for tickets
```
Search for issues
Usage: jirac search [OPTIONS] <JQL>
Arguments:
<JQL> 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 ## Configuration
Get the following information: Get the following information:
@ -88,7 +112,7 @@ Get the following information:
* You email * You email
* [An API key](https://id.atlassian.com/manage-profile/security/api-tokens) * [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 Setup the configuration

View file

@ -10,26 +10,36 @@ pub struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
pub enum Commands { pub enum Commands {
/// Create a ticket /// Create an issue
Create { Create {
/// The project key in which to create the ticket /// The project key in which to create the issue
#[arg(long)] #[arg(long)]
project: Option<String>, project: Option<String>,
/// Open the new ticket in a browser /// Open the new issue in a browser
#[arg(long)] #[arg(long)]
open: bool, open: bool,
/// A markdown file /// A Markdown file
#[arg(value_name = "MARKDOWN_FILE")] #[arg(value_name = "MARKDOWN_FILE")]
markdown_file: Option<PathBuf>, markdown_file: Option<PathBuf>,
}, },
/// Find tickets currently assigned to you /// Find issues currently assigned to you
List { List {
/// Print json rather than pretty print /// Print JSON rather than pretty print
#[arg(long)] #[arg(long)]
json: bool, json: bool,
}, },
/// 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 /// Set up the configuration
Init { Init {
/// Jira instance URL /// Jira instance URL

View file

@ -1,2 +1,3 @@
pub mod create; pub mod create;
pub mod list; pub mod list;
pub mod search;

32
src/cmd/search.rs Normal file
View file

@ -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<dyn std::error::Error>> {
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(())
}

View file

@ -19,6 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
markdown_file, markdown_file,
} => cmd::create::create(project, open, markdown_file).await?, } => cmd::create::create(project, open, markdown_file).await?,
Commands::List { json } => cmd::list::list(json).await?, Commands::List { json } => cmd::list::list(json).await?,
Commands::Search { json, jql } => cmd::search::exec(json, jql).await?,
Commands::Init { url, email, token } => { Commands::Init { url, email, token } => {
JiraConfig::init(url, email, token).await?; JiraConfig::init(url, email, token).await?;
println!("Configuration initialized successfully!"); println!("Configuration initialized successfully!");