Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub mod disk_usage;
pub mod issue;
pub mod licence;
pub mod notification;
pub mod priority;
pub mod project;
pub mod resolution;
pub mod space;
pub mod space_notification;
pub mod team;
Expand All @@ -27,10 +29,12 @@ use issue::{
};
use licence::Licence;
use notification::{Notification, NotificationCount};
use priority::Priority;
use project::{
Project, ProjectCategory, ProjectDiskUsage, ProjectIssueType, ProjectStatus, ProjectUser,
ProjectVersion,
};
use resolution::Resolution;
use space::Space;
use space_notification::SpaceNotification;
use team::Team;
Expand Down Expand Up @@ -289,6 +293,12 @@ pub trait BacklogApi {
fn put_space_notification(&self, _content: &str) -> Result<SpaceNotification> {
unimplemented!()
}
fn get_priorities(&self) -> Result<Vec<Priority>> {
unimplemented!()
}
fn get_resolutions(&self) -> Result<Vec<Resolution>> {
unimplemented!()
}
fn get_watchings(&self, _user_id: u64, _params: &[(String, String)]) -> Result<Vec<Watching>> {
unimplemented!()
}
Expand Down Expand Up @@ -611,6 +621,12 @@ impl BacklogApi for BacklogClient {
fn put_space_notification(&self, content: &str) -> Result<SpaceNotification> {
self.put_space_notification(content)
}
fn get_priorities(&self) -> Result<Vec<Priority>> {
self.get_priorities()
}
fn get_resolutions(&self) -> Result<Vec<Resolution>> {
self.get_resolutions()
}
fn get_watchings(&self, user_id: u64, params: &[(String, String)]) -> Result<Vec<Watching>> {
self.get_watchings(user_id, params)
}
Expand Down
49 changes: 49 additions & 0 deletions src/api/priority.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::BacklogClient;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Priority {
pub id: u64,
pub name: String,
}

impl BacklogClient {
pub fn get_priorities(&self) -> Result<Vec<Priority>> {
let value = self.get_with_query("/priorities", &[])?;
serde_json::from_value(value.clone()).map_err(|e| {
anyhow::anyhow!(
"Failed to deserialize response: {}\nRaw JSON:\n{}",
e,
serde_json::to_string_pretty(&value).unwrap_or_else(|_| value.to_string())
)
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use httpmock::prelude::*;

#[test]
fn get_priorities_parses_response() {
let server = MockServer::start();
server.mock(|when, then| {
when.method(GET).path("/priorities");
then.status(200)
.header("content-type", "application/json")
.json_body(serde_json::json!([
{"id": 2, "name": "High"},
{"id": 3, "name": "Normal"},
{"id": 4, "name": "Low"}
]));
});
let client = BacklogClient::new_with(&server.base_url(), "test-key").unwrap();
let result = client.get_priorities().unwrap();
assert_eq!(result.len(), 3);
assert_eq!(result[0].id, 2);
assert_eq!(result[0].name, "High");
}
}
49 changes: 49 additions & 0 deletions src/api/resolution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::BacklogClient;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Resolution {
pub id: u64,
pub name: String,
}

impl BacklogClient {
pub fn get_resolutions(&self) -> Result<Vec<Resolution>> {
let value = self.get_with_query("/resolutions", &[])?;
serde_json::from_value(value.clone()).map_err(|e| {
anyhow::anyhow!(
"Failed to deserialize response: {}\nRaw JSON:\n{}",
e,
serde_json::to_string_pretty(&value).unwrap_or_else(|_| value.to_string())
)
})
}
}

#[cfg(test)]
mod tests {
use super::*;
use httpmock::prelude::*;

#[test]
fn get_resolutions_parses_response() {
let server = MockServer::start();
server.mock(|when, then| {
when.method(GET).path("/resolutions");
then.status(200)
.header("content-type", "application/json")
.json_body(serde_json::json!([
{"id": 0, "name": "Fixed"},
{"id": 1, "name": "Won't Fix"},
{"id": 2, "name": "Invalid"}
]));
});
let client = BacklogClient::new_with(&server.base_url(), "test-key").unwrap();
let result = client.get_resolutions().unwrap();
assert_eq!(result.len(), 3);
assert_eq!(result[0].id, 0);
assert_eq!(result[0].name, "Fixed");
}
}
2 changes: 2 additions & 0 deletions src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ pub mod auth;
pub mod banner;
pub mod issue;
pub mod notification;
pub mod priority;
pub mod project;
pub mod resolution;
pub mod space;
pub mod team;
pub mod user;
Expand Down
97 changes: 97 additions & 0 deletions src/cmd/priority/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use anstream::println;
use anyhow::{Context, Result};

use crate::api::{BacklogApi, BacklogClient};

pub struct PriorityListArgs {
json: bool,
}

impl PriorityListArgs {
pub fn new(json: bool) -> Self {
Self { json }
}
}

pub fn list(args: &PriorityListArgs) -> Result<()> {
let client = BacklogClient::from_config()?;
list_with(args, &client)
}

pub fn list_with(args: &PriorityListArgs, api: &dyn BacklogApi) -> Result<()> {
let priorities = api.get_priorities()?;
if args.json {
println!(
"{}",
serde_json::to_string_pretty(&priorities).context("Failed to serialize JSON")?
);
} else {
for p in &priorities {
println!("[{}] {}", p.id, p.name);
}
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::api::priority::Priority;
use anyhow::anyhow;

struct MockApi {
priorities: Option<Vec<Priority>>,
}

impl crate::api::BacklogApi for MockApi {
fn get_priorities(&self) -> anyhow::Result<Vec<Priority>> {
self.priorities
.clone()
.ok_or_else(|| anyhow!("no priorities"))
}
}

fn sample_priorities() -> Vec<Priority> {
vec![
Priority {
id: 2,
name: "High".to_string(),
},
Priority {
id: 3,
name: "Normal".to_string(),
},
Priority {
id: 4,
name: "Low".to_string(),
},
]
}

fn args(json: bool) -> PriorityListArgs {
PriorityListArgs::new(json)
}

#[test]
fn list_with_text_output_succeeds() {
let api = MockApi {
priorities: Some(sample_priorities()),
};
assert!(list_with(&args(false), &api).is_ok());
}

#[test]
fn list_with_json_output_succeeds() {
let api = MockApi {
priorities: Some(sample_priorities()),
};
assert!(list_with(&args(true), &api).is_ok());
}

#[test]
fn list_with_propagates_api_error() {
let api = MockApi { priorities: None };
let err = list_with(&args(false), &api).unwrap_err();
assert!(err.to_string().contains("no priorities"));
}
}
3 changes: 3 additions & 0 deletions src/cmd/priority/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod list;

pub use list::{PriorityListArgs, list};
97 changes: 97 additions & 0 deletions src/cmd/resolution/list.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use anstream::println;
use anyhow::{Context, Result};

use crate::api::{BacklogApi, BacklogClient};

pub struct ResolutionListArgs {
json: bool,
}

impl ResolutionListArgs {
pub fn new(json: bool) -> Self {
Self { json }
}
}

pub fn list(args: &ResolutionListArgs) -> Result<()> {
let client = BacklogClient::from_config()?;
list_with(args, &client)
}

pub fn list_with(args: &ResolutionListArgs, api: &dyn BacklogApi) -> Result<()> {
let resolutions = api.get_resolutions()?;
if args.json {
println!(
"{}",
serde_json::to_string_pretty(&resolutions).context("Failed to serialize JSON")?
);
} else {
for r in &resolutions {
println!("[{}] {}", r.id, r.name);
}
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::api::resolution::Resolution;
use anyhow::anyhow;

struct MockApi {
resolutions: Option<Vec<Resolution>>,
}

impl crate::api::BacklogApi for MockApi {
fn get_resolutions(&self) -> anyhow::Result<Vec<Resolution>> {
self.resolutions
.clone()
.ok_or_else(|| anyhow!("no resolutions"))
}
}

fn sample_resolutions() -> Vec<Resolution> {
vec![
Resolution {
id: 0,
name: "Fixed".to_string(),
},
Resolution {
id: 1,
name: "Won't Fix".to_string(),
},
Resolution {
id: 2,
name: "Invalid".to_string(),
},
]
}

fn args(json: bool) -> ResolutionListArgs {
ResolutionListArgs::new(json)
}

#[test]
fn list_with_text_output_succeeds() {
let api = MockApi {
resolutions: Some(sample_resolutions()),
};
assert!(list_with(&args(false), &api).is_ok());
}

#[test]
fn list_with_json_output_succeeds() {
let api = MockApi {
resolutions: Some(sample_resolutions()),
};
assert!(list_with(&args(true), &api).is_ok());
}

#[test]
fn list_with_propagates_api_error() {
let api = MockApi { resolutions: None };
let err = list_with(&args(false), &api).unwrap_err();
assert!(err.to_string().contains("no resolutions"));
}
}
3 changes: 3 additions & 0 deletions src/cmd/resolution/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod list;

pub use list::{ResolutionListArgs, list};
Loading
Loading