|
1 | | -use crate::cli::{GlobalArgs, JiraCommand, JiraResource}; |
| 1 | +use anyhow::Context; |
| 2 | +use atla_core::auth::{CredentialStore, KeyringCredentialStore}; |
| 3 | +use atla_core::{ |
| 4 | + AtlaConfig, AtlassianClient, ConfigStore, JiraClient, JiraProject, JiraProjectSearch, Profile, |
| 5 | +}; |
2 | 6 |
|
3 | | -pub async fn run(command: JiraCommand, _global: &GlobalArgs) -> anyhow::Result<()> { |
| 7 | +use crate::cli::{ |
| 8 | + GlobalArgs, JiraCommand, JiraResource, OutputFormat, ProjectAction, ProjectCommand, |
| 9 | +}; |
| 10 | +use crate::config; |
| 11 | +use crate::output; |
| 12 | + |
| 13 | +pub async fn run(command: JiraCommand, global: &GlobalArgs) -> anyhow::Result<()> { |
4 | 14 | match command.resource { |
5 | 15 | JiraResource::Issue => println!("jira issue commands are planned"), |
6 | | - JiraResource::Project => println!("jira project commands are planned"), |
| 16 | + JiraResource::Project(command) => run_project(command, global).await?, |
7 | 17 | JiraResource::Sprint => println!("jira sprint commands are planned"), |
8 | 18 | JiraResource::Board => println!("jira board commands are planned"), |
9 | 19 | JiraResource::Search { jql } => println!("jira search is planned: {jql}"), |
10 | 20 | } |
11 | 21 |
|
12 | 22 | Ok(()) |
13 | 23 | } |
| 24 | + |
| 25 | +async fn run_project(command: ProjectCommand, global: &GlobalArgs) -> anyhow::Result<()> { |
| 26 | + match command.action { |
| 27 | + ProjectAction::List { query, limit } => { |
| 28 | + let store = ConfigStore::default_store().context("failed to find config location")?; |
| 29 | + let atla_config = store.load().context("failed to load config")?; |
| 30 | + let (profile_name, profile) = active_profile(&atla_config, global)?; |
| 31 | + let search = JiraProjectSearch { |
| 32 | + start_at: 0, |
| 33 | + max_results: limit.clamp(1, 100), |
| 34 | + query, |
| 35 | + }; |
| 36 | + |
| 37 | + if global.dry_run { |
| 38 | + let url = format!( |
| 39 | + "{}/rest/api/3/project/search?startAt={}&maxResults={}", |
| 40 | + profile.instance.trim_end_matches('/'), |
| 41 | + search.start_at, |
| 42 | + search.max_results |
| 43 | + ); |
| 44 | + if let Some(query) = &search.query { |
| 45 | + println!("Would GET {url} with query `{query}` using profile `{profile_name}`"); |
| 46 | + } else { |
| 47 | + println!("Would GET {url} using profile `{profile_name}`"); |
| 48 | + } |
| 49 | + return Ok(()); |
| 50 | + } |
| 51 | + |
| 52 | + let token = token_for_profile(profile_name, profile)?; |
| 53 | + let client = JiraClient::new(AtlassianClient::from_profile(profile, token)); |
| 54 | + let page = client.search_projects(&search).await.with_context(|| { |
| 55 | + format!( |
| 56 | + "failed to list Jira projects from {}", |
| 57 | + client.instance_url() |
| 58 | + ) |
| 59 | + })?; |
| 60 | + |
| 61 | + print_projects(&page.values, page.total, global)?; |
| 62 | + } |
| 63 | + ProjectAction::View { key } => { |
| 64 | + println!("jira project view is planned: {key}"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + Ok(()) |
| 69 | +} |
| 70 | + |
| 71 | +fn active_profile<'a>( |
| 72 | + atla_config: &'a AtlaConfig, |
| 73 | + global: &GlobalArgs, |
| 74 | +) -> anyhow::Result<(&'a str, &'a Profile)> { |
| 75 | + atla_config |
| 76 | + .active_profile(config::active_profile(global)) |
| 77 | + .ok_or_else(|| anyhow::anyhow!("no active profile; run `atla auth login` first")) |
| 78 | +} |
| 79 | + |
| 80 | +fn token_for_profile(profile_name: &str, profile: &Profile) -> anyhow::Result<String> { |
| 81 | + let credential = profile.credential_ref(profile_name); |
| 82 | + let token = KeyringCredentialStore::default() |
| 83 | + .get_token(&credential) |
| 84 | + .context("failed to read API token from keyring")?; |
| 85 | + |
| 86 | + token.ok_or_else(|| { |
| 87 | + anyhow::anyhow!("missing API token; run `atla auth login --profile {profile_name}`") |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +fn print_projects( |
| 92 | + projects: &[JiraProject], |
| 93 | + total: Option<u64>, |
| 94 | + global: &GlobalArgs, |
| 95 | +) -> anyhow::Result<()> { |
| 96 | + match global.output.unwrap_or(OutputFormat::Table) { |
| 97 | + OutputFormat::Json => output::print_json(projects), |
| 98 | + OutputFormat::Keys => { |
| 99 | + for project in projects { |
| 100 | + if let Some(key) = &project.key { |
| 101 | + println!("{key}"); |
| 102 | + } |
| 103 | + } |
| 104 | + Ok(()) |
| 105 | + } |
| 106 | + OutputFormat::Csv => { |
| 107 | + println!("key,name,type,style,archived"); |
| 108 | + for project in projects { |
| 109 | + println!( |
| 110 | + "{},{},{},{},{}", |
| 111 | + csv_cell(project.key.as_deref().unwrap_or_default()), |
| 112 | + csv_cell(project.name.as_deref().unwrap_or_default()), |
| 113 | + csv_cell(project.project_type_key.as_deref().unwrap_or_default()), |
| 114 | + csv_cell(project.style.as_deref().unwrap_or_default()), |
| 115 | + csv_cell(&project.archived.unwrap_or(false).to_string()) |
| 116 | + ); |
| 117 | + } |
| 118 | + Ok(()) |
| 119 | + } |
| 120 | + OutputFormat::Table => { |
| 121 | + println!("{:<12} {:<16} {:<12} NAME", "KEY", "TYPE", "STYLE"); |
| 122 | + for project in projects { |
| 123 | + println!( |
| 124 | + "{:<12} {:<16} {:<12} {}", |
| 125 | + project.key.as_deref().unwrap_or("-"), |
| 126 | + project.project_type_key.as_deref().unwrap_or("-"), |
| 127 | + project.style.as_deref().unwrap_or("-"), |
| 128 | + project.name.as_deref().unwrap_or("-") |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + if let Some(total) = total { |
| 133 | + println!(); |
| 134 | + println!("Showing {} of {total} projects.", projects.len()); |
| 135 | + } |
| 136 | + Ok(()) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +fn csv_cell(value: &str) -> String { |
| 142 | + if value.contains(',') || value.contains('"') || value.contains('\n') { |
| 143 | + format!("\"{}\"", value.replace('"', "\"\"")) |
| 144 | + } else { |
| 145 | + value.to_owned() |
| 146 | + } |
| 147 | +} |
0 commit comments