From 42b3a7089ac99afe399663ab0ee1686b7c27471a Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 8 Nov 2017 02:16:39 +0100 Subject: jenkins.rs: Add `Job::new` function to create Job from JSON payload Now we can parse the JSON payload in a single place and extract the values from it into a new `Job`. Change `Job.result`'s type to `JobStatus` because that makes more sense. Now, `result_from_job` gets called in `Job::new` to give us the `JobStatus` directly on the `Job`. Modify `result_from_job` to fit into its new responsibility, working for `Job::new`. Update the tests accordingly. --- src/jenkins.rs | 68 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/jenkins.rs b/src/jenkins.rs index 6c34f6c..59f0ddd 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -35,7 +35,18 @@ pub enum JobStatus { struct Job { display_name: String, - result: String, + result: JobStatus, +} + +impl Job { + fn new(payload: String) -> Job { + let mut job = json::parse(payload.as_ref()).unwrap(); + + Job { + display_name: job["displayName"].take_string().unwrap(), + result: result_from_job(job["result"].take_string()), + } + } } pub fn update_commit_status(commit_ref) { @@ -76,21 +87,16 @@ pub fn get_jobs(repo_name: String) {//-> Vec { pub fn job_for_commit(payload: String, commit_ref: CommitRef) -> bool { } -pub fn result_from_job(payload: String) -> JobStatus { - let mut job = json::parse(payload.as_ref()).unwrap(); - - if job["result"].is_null() { - return JobStatus::Pending - } - - let status = job["result"].take_string().unwrap(); - - if status == "SUCCESS" { - JobStatus::Success - } else if status == "FAILURE" { - JobStatus::Failure - } else { - JobStatus::Unknown +pub fn result_from_job(status: Option) -> JobStatus { + match status { + None => JobStatus::Pending, + Some(s) => { + match s.as_ref() { + "SUCCESS" => JobStatus::Success, + "FAILURE" => JobStatus::Failure, + _ => JobStatus::Unknown, + } + } } } @@ -99,6 +105,19 @@ pub fn result_from_job(payload: String) -> JobStatus { mod tests { use super::*; + #[test] + fn job_new_creates_a_job_from_payload() { + let payload = r#"{ + "displayName": "3296-fix-typo-700d0", + "result": "SUCCESS" + }"#.to_string(); + + let job = Job::new(payload); + + assert_eq!(job.display_name, "3296-fix-typo-700d0"); + assert_eq!(job.result, JobStatus::Success); + } + #[test] fn get_jobs_queries_jobs_from_jenkins_api() { get_jobs("changes".to_string()); @@ -106,35 +125,24 @@ mod tests { #[test] fn result_from_job_is_success() { - let payload = r#"{ - "result": "SUCCESS" - }"#; - assert_eq!( - result_from_job(payload.to_owned()), + result_from_job(Some("SUCCESS".to_string())), JobStatus::Success ); } #[test] fn result_from_job_is_failure() { - let payload = r#"{ - "result": "FAILURE" - }"#; - assert_eq!( - result_from_job(payload.to_owned()), + result_from_job(Some("FAILURE".to_string())), JobStatus::Failure ); } #[test] fn result_from_job_is_pending() { - let payload = r#"{ - }"#; - assert_eq!( - result_from_job(payload.to_owned()), + result_from_job(None), JobStatus::Pending ); } -- cgit v1.2.3