Skip to content
This repository has been archived by the owner on Jan 29, 2023. It is now read-only.

Commit

Permalink
✨ can query job with depth or tree parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed Jun 18, 2018
1 parent 0155e4a commit bafb9b8
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 7 deletions.
87 changes: 83 additions & 4 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
//! Jenkins Client

use failure;
use regex::Regex;
use reqwest::header::ContentType;
use reqwest::{Body, Client, RequestBuilder, Response, StatusCode};
use serde::{Serialize, Serializer};
use std::fmt::Debug;
use std::string::ToString;

mod errors;
pub use self::errors::Error;
Expand Down Expand Up @@ -31,7 +35,82 @@ pub struct Jenkins {
client: Client,
user: Option<User>,
csrf_enabled: bool,
depth: u8,
pub(crate) depth: u8,
}

/// Advanced query parameters supported by Jenkins
/// See https://www.cloudbees.com/blog/taming-jenkins-json-api-depth-and-tree
#[derive(Debug)]
pub enum AdvancedQuery {
/// depth query parameter
Depth(u8),
/// tree query parameter
Tree(TreeQueryParam),
}

#[derive(Debug, Serialize)]
pub(crate) struct InternalAdvancedQueryParams {
depth: Option<u8>,
tree: Option<TreeQueryParam>,
}
impl From<AdvancedQuery> for InternalAdvancedQueryParams {
fn from(query: AdvancedQuery) -> Self {
match query {
AdvancedQuery::Depth(depth) => InternalAdvancedQueryParams {
depth: Some(depth),
tree: None,
},
AdvancedQuery::Tree(tree) => InternalAdvancedQueryParams {
depth: None,
tree: Some(tree),
},
}
}
}
/// Jenkins tree query parameter
#[derive(Debug)]
pub struct TreeQueryParam {
/// Name of the key at the root of this tree
pub keyname: String,
/// fields of this object
pub fields: Vec<String>,
/// keys leading to child objects
pub subkeys: Vec<TreeQueryParam>,
}
impl Serialize for TreeQueryParam {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&self.to_string())
}
}
impl ToString for TreeQueryParam {
fn to_string(&self) -> String {
match (self.fields.len(), self.subkeys.len()) {
(0, 0) => format!("{}", self.keyname),
(_, 0) => format!("{}[{}]", self.keyname, self.fields.join(",")),
(0, _) => format!(
"{}[{}]",
self.keyname,
self.subkeys
.iter()
.map(|t| t.to_string())
.collect::<Vec<_>>()
.join(",")
),
(_, _) => format!(
"{}[{},{}]",
self.keyname,
self.fields.join(","),
self.subkeys
.iter()
.map(|t| t.to_string())
.collect::<Vec<_>>()
.join(",")
),
}
}
}

impl Jenkins {
Expand Down Expand Up @@ -61,13 +140,13 @@ impl Jenkins {
self.get_with_params(path, &[("depth", &self.depth.to_string())])
}

pub(crate) fn get_with_params(
pub(crate) fn get_with_params<T: Serialize>(
&self,
path: &Path,
qps: &[(&str, &str)],
qps: T,
) -> Result<Response, failure::Error> {
let mut query = self.client.get(&self.url_api_json(&path.to_string()));
query.query(qps);
query.query(&qps);
Ok(Self::error_for_status(self.send(query)?)?)
}

Expand Down
24 changes: 23 additions & 1 deletion src/job/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Jenkins Jobs

use failure::Error;
use serde;

use client::{Name, Path};
use client::{AdvancedQuery, InternalAdvancedQueryParams, Name, Path};
use queue::ShortQueueItem;
use Jenkins;

Expand Down Expand Up @@ -39,6 +40,27 @@ impl Jenkins {
.json()?)
}

/// Get a `Job` from it's `job_name`, specifying the depth or tree parameters
/// see https://www.cloudbees.com/blog/taming-jenkins-json-api-depth-and-tree
pub fn get_job_as<'a, J, T>(
&self,
job_name: J,
parameters: Option<AdvancedQuery>,
) -> Result<T, Error>
where
J: Into<JobName<'a>>,
for<'de> T: serde::Deserialize<'de>,
{
Ok(self.get_with_params(
&Path::Job {
name: Name::Name(job_name.into().0),
configuration: None,
},
parameters.map(InternalAdvancedQueryParams::from),
)?
.json()?)
}

/// Build a `Job` from it's `job_name`
pub fn build_job<'a, J>(&self, job_name: J) -> Result<ShortQueueItem, Error>
where
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ extern crate regex;
#[macro_use]
extern crate log;

mod client;
pub use client::{error, Error, Jenkins, JenkinsBuilder};
pub mod client;
pub use client::{Error, Jenkins, JenkinsBuilder};

#[macro_use]
pub mod helpers;
Expand Down

0 comments on commit bafb9b8

Please sign in to comment.