diff options
author | Teddy Wing | 2017-11-08 22:18:13 +0100 |
---|---|---|
committer | Teddy Wing | 2017-11-08 22:18:13 +0100 |
commit | 2ccb61d165a7a638f5525639e2bc6906984e9140 (patch) | |
tree | efa51c77d43d9fc18c7d60edb15d884016421d41 | |
parent | 13e82e9dad7f18cf7371b4aab1ade63ef651313e (diff) | |
download | kipper-2ccb61d165a7a638f5525639e2bc6906984e9140.tar.bz2 |
jenkins.rs: Make `get_jobs` real
Instead of just making a request to test out 'reqwest' and how to use
it, make this function actually do something useful.
It now requests a URL dynamically based on the `repo_name` passed in,
and returns a list of build job URLs from the JSON resulting from the
API response.
-rw-r--r-- | src/jenkins.rs | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/src/jenkins.rs b/src/jenkins.rs index a82084e..f8bd3bb 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -21,6 +21,7 @@ // fn result_from_job(payload) extern crate json; +extern crate mockito; extern crate reqwest; use self::reqwest::header::{Authorization, Basic}; @@ -28,6 +29,12 @@ use self::reqwest::header::{Authorization, Basic}; use af83; use pull_request::CommitRef; +#[cfg(not(test))] +const API_URL: &'static str = "http://jenkins.example.com"; + +#[cfg(test)] +const API_URL: &'static str = mockito::SERVER_URL; + #[derive(Debug, PartialEq, Eq)] pub enum JobStatus { Success, @@ -73,17 +80,25 @@ pub fn auth_credentials() -> Basic { } } -pub fn get_jobs(repo_name: String) {//-> Vec<String> { +pub fn get_jobs(repo_name: String) -> Vec<String> { let client = reqwest::Client::new(); let credentials = auth_credentials(); - let mut res = client.get("http://jenkins.example.com/job/changes-branches/18/api/json") + let mut response = client.get(&format!("{}/job/{}-branches/api/json", API_URL, repo_name)) .header(Authorization(credentials)) .send() .unwrap(); - println!("{}", res.status()); + let body = response.text().unwrap(); + + let jobs = json::parse(body.as_ref()).unwrap(); + + jobs["builds"].members() + .map(|job| { + job["url"].clone().take_string().unwrap() + }) + .collect::<Vec<String>>() } // Does the `commit_ref` correspond to the job? @@ -107,6 +122,8 @@ pub fn result_from_job(status: Option<String>) -> JobStatus { #[cfg(test)] mod tests { + use self::mockito::mock; + use super::*; #[test] |