diff options
author | Teddy Wing | 2017-11-08 02:16:39 +0100 |
---|---|---|
committer | Teddy Wing | 2017-11-08 02:16:39 +0100 |
commit | 42b3a7089ac99afe399663ab0ee1686b7c27471a (patch) | |
tree | b0608efd2e83fccc6d5220697cd1e4a01416ac38 /src | |
parent | 8d3a48ec18b7235c6284d982563822f9df89cb73 (diff) | |
download | kipper-42b3a7089ac99afe399663ab0ee1686b7c27471a.tar.bz2 |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/jenkins.rs | 68 |
1 files changed, 38 insertions, 30 deletions
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<String> { 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<String>) -> JobStatus { + match status { + None => JobStatus::Pending, + Some(s) => { + match s.as_ref() { + "SUCCESS" => JobStatus::Success, + "FAILURE" => JobStatus::Failure, + _ => JobStatus::Unknown, + } + } } } @@ -100,41 +106,43 @@ 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()); } #[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 ); } |