From 1b2f71944f50ae3032737c0566c20ad046789b37 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 12 Nov 2017 18:52:21 +0100 Subject: Take Jenkins URL from command line options Get rid of the static, global `API_URL`, and replace it with the Jenkins URL that gets passed into `find_and_track_build_and_update_status()`. This necessitated adding a new URL argument to `get_jobs()` and `request_job()`. Starting to wonder if I should be creating a struct to hold the URL and client that I can attach these functions to as methods, but for now I'll leave them alone. --- src/jenkins.rs | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/jenkins.rs b/src/jenkins.rs index 7402909..fd72e73 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -37,12 +37,6 @@ use af83; use github; 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, @@ -91,11 +85,26 @@ pub fn find_and_track_build_and_update_status( &jenkins_user_id, &jenkins_token )?; - let jobs = get_jobs(&jenkins_client, commit_ref.repo.as_ref())?; + + #[cfg(not(test))] + let jenkins_url = jenkins_url.to_owned(); + + #[cfg(test)] + let jenkins_url = mockito::SERVER_URL; + + let jobs = get_jobs( + &jenkins_url, + &jenkins_client, + commit_ref.repo.as_ref() + )?; let t20_minutes = 60 * 20; for job_url in jobs { - let mut job = request_job(&jenkins_client, job_url.as_ref())?; + let mut job = request_job( + &jenkins_url, + &jenkins_client, + job_url.as_ref() + )?; // Does `displayName` match if job_for_commit(&job, &commit_ref) { @@ -153,6 +162,7 @@ pub fn find_and_track_build_and_update_status( sleep(Duration::from_secs(30)); let updated_job = request_job( + &jenkins_url, &jenkins_client, job_url.as_ref() ).expect( @@ -196,9 +206,14 @@ pub fn auth_credentials(user_id: String, token: String) -> header::Basic { } } -pub fn get_jobs(client: &reqwest::Client, repo_name: &str) -> Result, Box> { - let mut response = client.get(&format!("{}/job/{}-branches/api/json", API_URL, repo_name)) - .send()?; +pub fn get_jobs( + jenkins_url: &String, + client: &reqwest::Client, + repo_name: &str +) -> Result, Box> { + let mut response = client.get( + &format!("{}/job/{}-branches/api/json", jenkins_url, repo_name) + ).send()?; let body = response.text()?; @@ -213,11 +228,16 @@ pub fn get_jobs(client: &reqwest::Client, repo_name: &str) -> Result ) } -pub fn request_job(client: &reqwest::Client, url: &str) -> Result> { +pub fn request_job( + jenkins_url: &String, + client: &reqwest::Client, + url: &str +) -> Result> { let url = Url::parse(url.as_ref())?; - let mut response = client.get(&format!("{}{}/api/json", API_URL, url.path())) - .send()?; + let mut response = client.get( + &format!("{}{}/api/json", jenkins_url, url.path()) + ).send()?; let body = response.text()?; -- cgit v1.2.3