Skip to content
Open
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
18 changes: 17 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod issues;
mod rep;
mod search;
mod transitions;
mod versions;

pub use crate::builder::*;
pub use crate::errors::*;
Expand All @@ -34,6 +35,7 @@ pub mod resolution;
pub use crate::boards::*;
pub mod sprints;
pub use crate::sprints::*;
pub use crate::versions::*;

#[derive(Deserialize, Debug)]
pub struct EmptyResponse;
Expand Down Expand Up @@ -109,16 +111,30 @@ impl Jira {
Sprints::new(self)
}

pub fn versions(&self) -> Versions {
Versions::new(self)
}

fn post<D, S>(&self, api_name: &str, endpoint: &str, body: S) -> Result<D>
where
D: DeserializeOwned,
S: Serialize,
{
let data = serde_json::to_string::<S>(&body)?;
debug!("Json request: {}", data);
debug!("Json POST request: {}", data);
self.request::<D>(Method::POST, api_name, endpoint, Some(data.into_bytes()))
}

fn put<D, S>(&self, api_name: &str, endpoint: &str, body: S) -> Result<D>
where
D: DeserializeOwned,
S: Serialize,
{
let data = serde_json::to_string::<S>(&body)?;
debug!("Json PUT request: {}", data);
self.request::<D>(Method::PUT, api_name, endpoint, Some(data.into_bytes()))
}

fn get<D>(&self, api_name: &str, endpoint: &str) -> Result<D>
where
D: DeserializeOwned,
Expand Down
22 changes: 22 additions & 0 deletions src/rep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,33 @@ pub struct Version {
pub archived: bool,
pub id: String,
pub name: String,
#[serde(rename = "projectId")]
pub project_id: u64,
pub released: bool,
#[serde(rename = "self")]
pub self_link: String,
}

#[derive(Serialize, Debug)]
pub struct VersionCreationBody {
pub name: String,
#[serde(rename = "projectId")]
pub project_id: u64,
}

#[derive(Serialize, Debug)]
pub struct VersionMoveAfterBody {
pub after: String,
}

#[derive(Serialize, Debug)]
pub struct VersionUpdateBody {
pub released: bool,
pub archived: bool,
#[serde(rename = "moveUnfixedIssuesTo")]
pub move_unfixed_issues_to: Option<String>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct User {
pub active: bool,
Expand Down
73 changes: 73 additions & 0 deletions src/versions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use crate::{
Error, Jira, Result, Version, VersionCreationBody, VersionMoveAfterBody, VersionUpdateBody,
};

pub struct Versions {
jira: Jira,
}

impl Versions {
pub fn new(jira: &Jira) -> Self {
Self { jira: jira.clone() }
}

/// Fetch all versions associated to the given project
///
/// See [jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-project-projectIdOrKey-versions-get)
/// for more information
pub fn project_versions(&self, project_id_or_key: &str) -> Result<Vec<Version>> {
self.jira
.get("api", &format!("/project/{}/versions", project_id_or_key))
}

/// Create a new version
///
/// See [jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-version-post)
/// for more information
pub fn create<T: Into<String>>(&self, project_id: u64, name: T) -> Result<Version> {
let name = name.into();
self.jira
.post("api", "/version", VersionCreationBody { project_id, name })
}

/// Move a version after another version
///
/// See [jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-version-id-move-post)
/// for more information
pub fn move_after<T: Into<String>>(&self, version: &Version, after: T) -> Result<Version> {
self.jira.post(
"api",
&format!("/version/{}/move", version.id),
VersionMoveAfterBody {
after: after.into(),
},
)
}

/// Release a new version: modify the version by turning the released boolean to true
///
/// See [jira docs](https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-version-id-put)
/// for more information
pub fn release(
&self,
version: &Version,
move_unfixed_issues_to: Option<&Version>,
) -> Result<()> {
if version.released {
// already released
Ok(())
} else {
self.jira
.put::<Version, _>(
"api",
&format!("/version/{}", version.id),
VersionUpdateBody {
released: true,
archived: false,
move_unfixed_issues_to: move_unfixed_issues_to.map(|v| v.self_link.clone()),
},
)
.map(|_v| ())
}
}
}