use anyhow::Result;
use atlassian_cli_api::ApiClient;
use atlassian_cli_output::OutputRenderer;
use clap::{Args, Subcommand};
mod approvals;
mod customers;
mod feedback;
mod knowledgebase;
mod organizations;
mod queues;
mod requests;
mod requesttypes;
mod servicedesk;
mod sla;
pub mod utils;
pub use utils::JsmContext;
#[derive(Args, Debug, Clone)]
pub struct JsmArgs {
#[command(subcommand)]
command: JsmCommands,
}
#[derive(Subcommand, Debug, Clone)]
enum JsmCommands {
#[command(subcommand)]
ServiceDesk(ServiceDeskCommands),
#[command(subcommand)]
Request(RequestCommands),
#[command(subcommand)]
Queue(QueueCommands),
#[command(subcommand)]
Approval(ApprovalCommands),
#[command(subcommand)]
Sla(SlaCommands),
#[command(subcommand)]
Customer(CustomerCommands),
#[command(subcommand)]
Organization(OrganizationCommands),
#[command(subcommand)]
RequestType(RequestTypeCommands),
#[command(subcommand)]
Kb(KbCommands),
#[command(subcommand)]
Feedback(FeedbackCommands),
}
#[derive(Subcommand, Debug, Clone)]
enum ServiceDeskCommands {
List {
#[arg(long, default_value_t = 25)]
limit: usize,
},
Get { id: i64 },
Customers {
id: i64,
#[arg(long, default_value_t = 25)]
limit: usize,
},
AddCustomer {
id: i64,
#[arg(long, required = true)]
account_id: Vec<String>,
},
RemoveCustomer {
id: i64,
#[arg(long, required = true)]
account_id: Vec<String>,
},
Organizations {
id: i64,
#[arg(long, default_value_t = 25)]
limit: usize,
},
AddOrganization {
id: i64,
#[arg(long)]
org_id: i64,
},
RemoveOrganization {
id: i64,
#[arg(long)]
org_id: i64,
},
}
#[derive(Subcommand, Debug, Clone)]
enum RequestCommands {
List {
#[arg(long)]
servicedesk_id: Option<i64>,
#[arg(long, default_value_t = 25)]
limit: usize,
},
Get {
#[arg(value_name = "KEY")]
key: String,
},
Create {
#[arg(long)]
servicedesk_id: i64,
#[arg(long)]
request_type_id: i64,
#[arg(long)]
summary: String,
#[arg(long)]
description: Option<String>,
},
Transitions {
#[arg(value_name = "KEY")]
key: String,
},
Transition {
#[arg(value_name = "KEY")]
key: String,
#[arg(long)]
transition: String,
},
Status {
#[arg(value_name = "KEY")]
key: String,
},
Comments {
#[arg(value_name = "KEY")]
key: String,
#[arg(long, default_value_t = 25)]
limit: usize,
},
AddComment {
#[arg(value_name = "KEY")]
key: String,
#[arg(long)]
body: String,
#[arg(long, default_value_t = false)]
public: bool,
},
Participants {
#[arg(value_name = "KEY")]
key: String,
},
AddParticipant {
#[arg(value_name = "KEY")]
key: String,
#[arg(long, required = true)]
account_id: Vec<String>,
},
RemoveParticipant {
#[arg(value_name = "KEY")]
key: String,
#[arg(long, required = true)]
account_id: Vec<String>,
},
Subscribe {
#[arg(value_name = "KEY")]
key: String,
},
Unsubscribe {
#[arg(value_name = "KEY")]
key: String,
},
}
#[derive(Subcommand, Debug, Clone)]
enum QueueCommands {
List { servicedesk_id: i64 },
Get { servicedesk_id: i64, queue_id: i64 },
Issues {
servicedesk_id: i64,
queue_id: i64,
#[arg(long, default_value_t = 25)]
limit: usize,
},
}
#[derive(Subcommand, Debug, Clone)]
enum ApprovalCommands {
List {
#[arg(value_name = "KEY")]
key: String,
},
Get {
#[arg(value_name = "KEY")]
key: String,
#[arg(long)]
approval_id: i64,
},
Approve {
#[arg(value_name = "KEY")]
key: String,
#[arg(long)]
approval_id: i64,
},
Decline {
#[arg(value_name = "KEY")]
key: String,
#[arg(long)]
approval_id: i64,
},
}
#[derive(Subcommand, Debug, Clone)]
enum SlaCommands {
List {
#[arg(value_name = "KEY")]
key: String,
},
Get {
#[arg(value_name = "KEY")]
key: String,
#[arg(long)]
sla_id: String,
},
}
#[derive(Subcommand, Debug, Clone)]
enum CustomerCommands {
Create {
#[arg(long)]
email: String,
#[arg(long)]
display_name: String,
},
RevokePortalAccess {
#[arg(long)]
account_id: String,
},
}
#[derive(Subcommand, Debug, Clone)]
enum OrganizationCommands {
List {
#[arg(long, default_value_t = 25)]
limit: usize,
},
Get { org_id: i64 },
Create {
#[arg(long)]
name: String,
},
Delete { org_id: i64 },
Users {
org_id: i64,
#[arg(long, default_value_t = 25)]
limit: usize,
},
AddUser {
org_id: i64,
#[arg(long, required = true)]
account_id: Vec<String>,
},
RemoveUser {
org_id: i64,
#[arg(long, required = true)]
account_id: Vec<String>,
},
}
#[derive(Subcommand, Debug, Clone)]
enum RequestTypeCommands {
List {
#[arg(long)]
servicedesk_id: Option<i64>,
#[arg(long, default_value_t = 25)]
limit: usize,
},
Get { servicedesk_id: i64, type_id: i64 },
Fields { servicedesk_id: i64, type_id: i64 },
Groups { servicedesk_id: i64 },
}
#[derive(Subcommand, Debug, Clone)]
enum KbCommands {
Search {
#[arg(long)]
query: String,
#[arg(long)]
servicedesk_id: Option<i64>,
#[arg(long, default_value_t = 25)]
limit: usize,
},
}
#[derive(Subcommand, Debug, Clone)]
enum FeedbackCommands {
Get {
#[arg(value_name = "KEY")]
key: String,
},
Submit {
#[arg(value_name = "KEY")]
key: String,
#[arg(long)]
rating: i32,
#[arg(long)]
comment: Option<String>,
},
Delete {
#[arg(value_name = "KEY")]
key: String,
},
}
pub async fn execute(args: JsmArgs, client: ApiClient, renderer: &OutputRenderer) -> Result<()> {
let ctx = JsmContext { client, renderer };
match args.command {
JsmCommands::ServiceDesk(cmd) => match cmd {
ServiceDeskCommands::List { limit } => {
servicedesk::list_service_desks(&ctx, limit).await
}
ServiceDeskCommands::Get { id } => servicedesk::get_service_desk(&ctx, id).await,
ServiceDeskCommands::Customers { id, limit } => {
servicedesk::list_customers(&ctx, id, limit).await
}
ServiceDeskCommands::AddCustomer { id, account_id } => {
servicedesk::add_customer(&ctx, id, account_id).await
}
ServiceDeskCommands::RemoveCustomer { id, account_id } => {
servicedesk::remove_customer(&ctx, id, account_id).await
}
ServiceDeskCommands::Organizations { id, limit } => {
servicedesk::list_organizations(&ctx, id, limit).await
}
ServiceDeskCommands::AddOrganization { id, org_id } => {
servicedesk::add_organization(&ctx, id, org_id).await
}
ServiceDeskCommands::RemoveOrganization { id, org_id } => {
servicedesk::remove_organization(&ctx, id, org_id).await
}
},
JsmCommands::Request(cmd) => match cmd {
RequestCommands::List {
servicedesk_id,
limit,
} => requests::list_requests(&ctx, servicedesk_id, limit).await,
RequestCommands::Get { key } => requests::get_request(&ctx, &key).await,
RequestCommands::Create {
servicedesk_id,
request_type_id,
summary,
description,
} => {
requests::create_request(
&ctx,
servicedesk_id,
request_type_id,
summary,
description,
)
.await
}
RequestCommands::Transitions { key } => requests::list_transitions(&ctx, &key).await,
RequestCommands::Transition { key, transition } => {
requests::transition_request(&ctx, &key, &transition).await
}
RequestCommands::Status { key } => requests::get_status_history(&ctx, &key).await,
RequestCommands::Comments { key, limit } => {
requests::list_comments(&ctx, &key, limit).await
}
RequestCommands::AddComment { key, body, public } => {
requests::add_comment(&ctx, &key, body, public).await
}
RequestCommands::Participants { key } => requests::list_participants(&ctx, &key).await,
RequestCommands::AddParticipant { key, account_id } => {
requests::add_participant(&ctx, &key, account_id).await
}
RequestCommands::RemoveParticipant { key, account_id } => {
requests::remove_participant(&ctx, &key, account_id).await
}
RequestCommands::Subscribe { key } => requests::subscribe(&ctx, &key).await,
RequestCommands::Unsubscribe { key } => requests::unsubscribe(&ctx, &key).await,
},
JsmCommands::Queue(cmd) => match cmd {
QueueCommands::List { servicedesk_id } => {
queues::list_queues(&ctx, servicedesk_id).await
}
QueueCommands::Get {
servicedesk_id,
queue_id,
} => queues::get_queue(&ctx, servicedesk_id, queue_id).await,
QueueCommands::Issues {
servicedesk_id,
queue_id,
limit,
} => queues::list_queue_issues(&ctx, servicedesk_id, queue_id, limit).await,
},
JsmCommands::Approval(cmd) => match cmd {
ApprovalCommands::List { key } => approvals::list_approvals(&ctx, &key).await,
ApprovalCommands::Get { key, approval_id } => {
approvals::get_approval(&ctx, &key, approval_id).await
}
ApprovalCommands::Approve { key, approval_id } => {
approvals::answer_approval(&ctx, &key, approval_id, true).await
}
ApprovalCommands::Decline { key, approval_id } => {
approvals::answer_approval(&ctx, &key, approval_id, false).await
}
},
JsmCommands::Sla(cmd) => match cmd {
SlaCommands::List { key } => sla::list_slas(&ctx, &key).await,
SlaCommands::Get { key, sla_id } => sla::get_sla(&ctx, &key, &sla_id).await,
},
JsmCommands::Customer(cmd) => match cmd {
CustomerCommands::Create {
email,
display_name,
} => customers::create_customer(&ctx, email, display_name).await,
CustomerCommands::RevokePortalAccess { account_id } => {
customers::revoke_portal_access(&ctx, &account_id).await
}
},
JsmCommands::Organization(cmd) => match cmd {
OrganizationCommands::List { limit } => {
organizations::list_organizations(&ctx, limit).await
}
OrganizationCommands::Get { org_id } => {
organizations::get_organization(&ctx, org_id).await
}
OrganizationCommands::Create { name } => {
organizations::create_organization(&ctx, name).await
}
OrganizationCommands::Delete { org_id } => {
organizations::delete_organization(&ctx, org_id).await
}
OrganizationCommands::Users { org_id, limit } => {
organizations::list_organization_users(&ctx, org_id, limit).await
}
OrganizationCommands::AddUser { org_id, account_id } => {
organizations::add_organization_user(&ctx, org_id, account_id).await
}
OrganizationCommands::RemoveUser { org_id, account_id } => {
organizations::remove_organization_user(&ctx, org_id, account_id).await
}
},
JsmCommands::RequestType(cmd) => match cmd {
RequestTypeCommands::List {
servicedesk_id,
limit,
} => {
if let Some(sd_id) = servicedesk_id {
requesttypes::list_request_types(&ctx, sd_id, limit).await
} else {
requesttypes::list_all_request_types(&ctx, limit).await
}
}
RequestTypeCommands::Get {
servicedesk_id,
type_id,
} => requesttypes::get_request_type(&ctx, servicedesk_id, type_id).await,
RequestTypeCommands::Fields {
servicedesk_id,
type_id,
} => requesttypes::list_request_type_fields(&ctx, servicedesk_id, type_id).await,
RequestTypeCommands::Groups { servicedesk_id } => {
requesttypes::list_request_type_groups(&ctx, servicedesk_id).await
}
},
JsmCommands::Kb(cmd) => match cmd {
KbCommands::Search {
query,
servicedesk_id,
limit,
} => knowledgebase::search_articles(&ctx, &query, servicedesk_id, limit).await,
},
JsmCommands::Feedback(cmd) => match cmd {
FeedbackCommands::Get { key } => feedback::get_feedback(&ctx, &key).await,
FeedbackCommands::Submit {
key,
rating,
comment,
} => feedback::submit_feedback(&ctx, &key, rating, comment).await,
FeedbackCommands::Delete { key } => feedback::delete_feedback(&ctx, &key).await,
},
}
}