From patchwork Fri Feb 1 06:17:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 1034597 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43rRjK5Y6Zz9sDX for ; Fri, 1 Feb 2019 17:17:41 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 43rRjK2CTCzDqfk for ; Fri, 1 Feb 2019 17:17:41 +1100 (AEDT) X-Original-To: snowpatch@lists.ozlabs.org Delivered-To: snowpatch@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=au1.ibm.com (client-ip=148.163.158.5; helo=mx0a-001b2d01.pphosted.com; envelope-from=andrew.donnellan@au1.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 43rRj9346czDqfK for ; Fri, 1 Feb 2019 17:17:32 +1100 (AEDT) Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x1164mkY129374 for ; Fri, 1 Feb 2019 01:17:30 -0500 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0b-001b2d01.pphosted.com with ESMTP id 2qcfqnjask-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 01 Feb 2019 01:17:30 -0500 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 1 Feb 2019 06:17:28 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 1 Feb 2019 06:17:27 -0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x116HQnP65470538 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 1 Feb 2019 06:17:26 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5FBBBA4062 for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0E36CA405B for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from intelligence.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id BF644A0123 for ; Fri, 1 Feb 2019 17:17:24 +1100 (AEDT) From: Andrew Donnellan To: snowpatch@lists.ozlabs.org Date: Fri, 1 Feb 2019 17:17:10 +1100 X-Mailer: git-send-email 2.11.0 X-TM-AS-GCONF: 00 x-cbid: 19020106-0028-0000-0000-00000341AE75 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19020106-0029-0000-0000-000023FFB471 Message-Id: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-02-01_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=654 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902010044 Subject: [snowpatch] [PATCH 1/5] Formatting cleanups X-BeenThere: snowpatch@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Continuous Integration for patch-based workflows List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: snowpatch-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "snowpatch" Signed-off-by: Andrew Donnellan --- src/jenkins.rs | 3 ++- src/patchwork.rs | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/jenkins.rs b/src/jenkins.rs index 593cee56716d..97dfe4f031db 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -101,7 +101,8 @@ impl JenkinsBackend { format!( "Basic {}", base64::encode(&format!("{}:{}", username, token)) - ).parse() + ) + .parse() .unwrap(), ); } diff --git a/src/patchwork.rs b/src/patchwork.rs index 1c2d3821300b..f01779ec5274 100644 --- a/src/patchwork.rs +++ b/src/patchwork.rs @@ -243,7 +243,8 @@ impl PatchworkServer { format!( "Basic {}", base64::encode(&format!("{}:{}", username, password)) - ).parse() + ) + .parse() .unwrap(), ); } @@ -310,15 +311,15 @@ impl PatchworkServer { fn get_next_link(&self, _resp: &Response) -> Option { /* - let next = resp.headers().get(LINK)?; - for val in next.values() { - if let Some(rel) = val.rel() { - if rel.iter().any(|reltype| reltype == &RelationType::Next) { - return Some(val.link().to_string()); + let next = resp.headers().get(LINK)?; + for val in next.values() { + if let Some(rel) = val.rel() { + if rel.iter().any(|reltype| reltype == &RelationType::Next) { + return Some(val.link().to_string()); + } + } } - } - } -*/ + */ None } From patchwork Fri Feb 1 06:17:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 1034600 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43rRjT0mPdz9sDX for ; Fri, 1 Feb 2019 17:17:49 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 43rRjS6pgLzDqfk for ; Fri, 1 Feb 2019 17:17:48 +1100 (AEDT) X-Original-To: snowpatch@lists.ozlabs.org Delivered-To: snowpatch@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=au1.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=andrew.donnellan@au1.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 43rRjC0tzRzDqfL for ; Fri, 1 Feb 2019 17:17:34 +1100 (AEDT) Received: from pps.filterd (m0098396.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x1166Ofx131740 for ; Fri, 1 Feb 2019 01:17:33 -0500 Received: from e06smtp05.uk.ibm.com (e06smtp05.uk.ibm.com [195.75.94.101]) by mx0a-001b2d01.pphosted.com with ESMTP id 2qcgan8wc6-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 01 Feb 2019 01:17:32 -0500 Received: from localhost by e06smtp05.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 1 Feb 2019 06:17:30 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp05.uk.ibm.com (192.168.101.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 1 Feb 2019 06:17:27 -0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x116HQJi34275402 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 1 Feb 2019 06:17:26 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A5379A4060 for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0D8A2A4054 for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from intelligence.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id C1962A025E for ; Fri, 1 Feb 2019 17:17:24 +1100 (AEDT) From: Andrew Donnellan To: snowpatch@lists.ozlabs.org Date: Fri, 1 Feb 2019 17:17:11 +1100 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> References: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19020106-0020-0000-0000-00000310212A X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19020106-0021-0000-0000-000021612255 Message-Id: <20190201061714.7110-2-andrew.donnellan@au1.ibm.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-02-01_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902010044 Subject: [snowpatch] [PATCH 2/5] jenkins: Rename "build_url" to "build_handle" X-BeenThere: snowpatch@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Continuous Integration for patch-based workflows List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: snowpatch-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "snowpatch" In preparation for genericising our CI backend API, change references to "build_url" to the more generic "build_handle", because the fact that it's a URL is really a Jenkins-specific detail. Snowpatch cares about the fact that a results URL is a URL, but not so much the build URL, which could be any arbitrary identifier really. Also some random formatting cleanups. Signed-off-by: Andrew Donnellan --- src/jenkins.rs | 42 ++++++++++++++++++++++-------------------- src/main.rs | 18 +++++++++--------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/src/jenkins.rs b/src/jenkins.rs index 97dfe4f031db..547c396c3513 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -145,7 +145,7 @@ impl JenkinsBackend { .map_err(|e| format!("Couldn't parse JSON from Jenkins: {}", e).into()) } - pub fn get_build_url(&self, build_queue_entry: &str) -> Result> { + pub fn get_build_handle(&self, build_queue_entry: &str) -> Result> { loop { let entry = self.get_api_json_object(build_queue_entry)?; match entry.get("executable") { @@ -166,17 +166,17 @@ impl JenkinsBackend { } } - pub fn get_build_status(&self, build_url: &str) -> Result> { - match self.get_api_json_object(build_url)?["building"].as_bool() { + pub fn get_build_status(&self, build_handle: &str) -> Result> { + match self.get_api_json_object(build_handle)?["building"].as_bool() { Some(true) => Ok(JenkinsBuildStatus::Running), Some(false) => Ok(JenkinsBuildStatus::Done), None => Err("Error getting build status".into()), } } - pub fn get_build_result(&self, build_url: &str) -> Result> { + pub fn get_build_result(&self, build_handle: &str) -> Result> { match self - .get_api_json_object(build_url)? + .get_api_json_object(build_handle)? .get("result") .map(|v| v.as_str().unwrap_or("PENDING")) { @@ -191,11 +191,11 @@ impl JenkinsBackend { } } - pub fn get_results_url(&self, build_url: &str, job: &BTreeMap) -> String { - let default_url = format!("{}/", build_url); + pub fn get_results_url(&self, build_handle: &str, job: &BTreeMap) -> String { + let default_url = format!("{}/", build_handle); match job.get("artifact") { Some(artifact) => { - let artifact_url = format!("{}/artifact/{}", build_url, artifact); + let artifact_url = format!("{}/artifact/{}", build_handle, artifact); match self.get_url(&artifact_url) { Ok(mut resp) => match resp.status().is_success() { true => artifact_url, @@ -210,27 +210,29 @@ impl JenkinsBackend { pub fn get_description( &self, - build_url: &str, + build_handle: &str, job: &BTreeMap, ) -> Option { match job.get("description") { - Some(artifact) => match self.get_url(&format!("{}/artifact/{}", build_url, artifact)) { - Ok(mut resp) => match resp.status().is_success() { - true => match resp.text() { - Ok(text) => Some(text), - Err(_e) => None, + Some(artifact) => { + match self.get_url(&format!("{}/artifact/{}", build_handle, artifact)) { + Ok(mut resp) => match resp.status().is_success() { + true => match resp.text() { + Ok(text) => Some(text), + Err(_e) => None, + }, + false => None, }, - false => None, - }, - Err(_e) => None, - }, + Err(_e) => None, + } + } None => None, } } - pub fn wait_build(&self, build_url: &str) -> Result> { + pub fn wait_build(&self, build_handle: &str) -> Result> { // TODO: Implement a timeout? - while self.get_build_status(build_url)? != JenkinsBuildStatus::Done { + while self.get_build_status(build_handle)? != JenkinsBuildStatus::Done { sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); } Ok(JenkinsBuildStatus::Done) diff --git a/src/main.rs b/src/main.rs index 2ca2b007ecba..19989c87cb01 100644 --- a/src/main.rs +++ b/src/main.rs @@ -134,23 +134,23 @@ fn run_tests( .start_test(&job.job, jenkins_params) .unwrap_or_else(|err| panic!("Starting Jenkins test failed: {}", err)); debug!("{:?}", &res); - let build_url_real; + let build_handle_real; loop { - let build_url = jenkins.get_build_url(&res); - if let Ok(url) = build_url { - build_url_real = url; + let build_handle = jenkins.get_build_handle(&res); + if let Ok(handle) = build_handle { + build_handle_real = handle; break; } // TODO: Handle error } - debug!("Build URL: {}", build_url_real); - jenkins.wait_build(&build_url_real).unwrap(); // TODO: Error correctly - let mut test_result = jenkins.get_build_result(&build_url_real).unwrap(); + debug!("Build URL: {}", build_handle_real); + jenkins.wait_build(&build_handle_real).unwrap(); // TODO: Error correctly + let mut test_result = jenkins.get_build_result(&build_handle_real).unwrap(); info!("Jenkins job for {}/{} complete.", branch_name, job.title); if test_result == TestState::Fail && job.warn_on_fail { test_result = TestState::Warning; } results.push(TestResult { - description: match jenkins.get_description(&build_url_real, &job.parameters) { + description: match jenkins.get_description(&build_handle_real, &job.parameters) { Some(description) => Some(description), None => Some( format!("Test {} on branch {}", job.title, branch_name.to_string()).to_string(), @@ -158,7 +158,7 @@ fn run_tests( }, state: test_result, context: Some(job.title.replace("/", "_")), - target_url: Some(jenkins.get_results_url(&build_url_real, &job.parameters)), + target_url: Some(jenkins.get_results_url(&build_handle_real, &job.parameters)), }); } results From patchwork Fri Feb 1 06:17:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 1034598 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43rRjN59b2z9sDr for ; Fri, 1 Feb 2019 17:17:44 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 43rRjN06vJzDqfK for ; Fri, 1 Feb 2019 17:17:44 +1100 (AEDT) X-Original-To: snowpatch@lists.ozlabs.org Delivered-To: snowpatch@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=au1.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=andrew.donnellan@au1.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 43rRj95gY0zDqfL for ; Fri, 1 Feb 2019 17:17:33 +1100 (AEDT) Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x1165f4t116031 for ; Fri, 1 Feb 2019 01:17:31 -0500 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2qcghtge07-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 01 Feb 2019 01:17:30 -0500 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 1 Feb 2019 06:17:28 -0000 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 1 Feb 2019 06:17:27 -0000 Received: from d06av24.portsmouth.uk.ibm.com (mk.ibm.com [9.149.105.60]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x116HQLk22216826 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 1 Feb 2019 06:17:26 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 62BFF42049 for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0F9934203F for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from intelligence.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id C4444A0268 for ; Fri, 1 Feb 2019 17:17:24 +1100 (AEDT) From: Andrew Donnellan To: snowpatch@lists.ozlabs.org Date: Fri, 1 Feb 2019 17:17:12 +1100 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> References: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19020106-4275-0000-0000-00000308B34C X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19020106-4276-0000-0000-00003816BFEF Message-Id: <20190201061714.7110-3-andrew.donnellan@au1.ibm.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-02-01_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=727 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902010044 Subject: [snowpatch] [PATCH 3/5] jenkins: Rename JenkinsBuildStatus to BuildStatus X-BeenThere: snowpatch@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Continuous Integration for patch-based workflows List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: snowpatch-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "snowpatch" There's no reason JenkinsBuildStatus needs to be Jenkins-specific at the moment. Signed-off-by: Andrew Donnellan --- src/jenkins.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/jenkins.rs b/src/jenkins.rs index 547c396c3513..c76561f1fda2 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -86,7 +86,7 @@ impl CIBackend for JenkinsBackend { } #[derive(Eq, PartialEq)] -pub enum JenkinsBuildStatus { +pub enum BuildStatus { Running, Done, } @@ -166,10 +166,10 @@ impl JenkinsBackend { } } - pub fn get_build_status(&self, build_handle: &str) -> Result> { + pub fn get_build_status(&self, build_handle: &str) -> Result> { match self.get_api_json_object(build_handle)?["building"].as_bool() { - Some(true) => Ok(JenkinsBuildStatus::Running), - Some(false) => Ok(JenkinsBuildStatus::Done), + Some(true) => Ok(BuildStatus::Running), + Some(false) => Ok(BuildStatus::Done), None => Err("Error getting build status".into()), } } @@ -230,11 +230,11 @@ impl JenkinsBackend { } } - pub fn wait_build(&self, build_handle: &str) -> Result> { + pub fn wait_build(&self, build_handle: &str) -> Result> { // TODO: Implement a timeout? - while self.get_build_status(build_handle)? != JenkinsBuildStatus::Done { + while self.get_build_status(build_handle)? != BuildStatus::Done { sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); } - Ok(JenkinsBuildStatus::Done) + Ok(BuildStatus::Done) } } From patchwork Fri Feb 1 06:17:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 1034599 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43rRjQ42YYz9sDX for ; Fri, 1 Feb 2019 17:17:46 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 43rRjQ2pTbzDqg8 for ; Fri, 1 Feb 2019 17:17:46 +1100 (AEDT) X-Original-To: snowpatch@lists.ozlabs.org Delivered-To: snowpatch@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=au1.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=andrew.donnellan@au1.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 43rRjB1MJVzDqfM for ; Fri, 1 Feb 2019 17:17:33 +1100 (AEDT) Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x1163pVr051498 for ; Fri, 1 Feb 2019 01:17:31 -0500 Received: from e06smtp04.uk.ibm.com (e06smtp04.uk.ibm.com [195.75.94.100]) by mx0a-001b2d01.pphosted.com with ESMTP id 2qce52e68t-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 01 Feb 2019 01:17:31 -0500 Received: from localhost by e06smtp04.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 1 Feb 2019 06:17:29 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) by e06smtp04.uk.ibm.com (192.168.101.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 1 Feb 2019 06:17:27 -0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x116HQqj7209438 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 1 Feb 2019 06:17:26 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 555DA52059 for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTP id 035B55204E for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from intelligence.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id D0E37A0272 for ; Fri, 1 Feb 2019 17:17:24 +1100 (AEDT) From: Andrew Donnellan To: snowpatch@lists.ozlabs.org Date: Fri, 1 Feb 2019 17:17:13 +1100 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> References: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19020106-0016-0000-0000-0000024F2172 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19020106-0017-0000-0000-000032A9229D Message-Id: <20190201061714.7110-4-andrew.donnellan@au1.ibm.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-02-01_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902010044 Subject: [snowpatch] [PATCH 4/5] ci: Move CIBackend and BuildStatus to separate module X-BeenThere: snowpatch@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Continuous Integration for patch-based workflows List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: snowpatch-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "snowpatch" Move CIBackend and BuildStatus to a separate module as we're eventually going to use it for non-Jenkins things. Signed-off-by: Andrew Donnellan --- src/ci.rs | 26 ++++++++++++++++++++++++++ src/jenkins.rs | 14 ++------------ src/main.rs | 5 ++++- 3 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 src/ci.rs diff --git a/src/ci.rs b/src/ci.rs new file mode 100644 index 000000000000..7d21edb7662f --- /dev/null +++ b/src/ci.rs @@ -0,0 +1,26 @@ +// +// snowpatch - continuous integration for patch-based workflows +// +// Copyright (C) 2016-2019 IBM Corporation +// Authors: +// Russell Currey +// Andrew Donnellan +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 2 of the License, or (at your option) +// any later version. +// +// ci.rs - CI backend interface definitions +// + +#[derive(Eq, PartialEq)] +pub enum BuildStatus { + Running, + Done, +} + +pub trait CIBackend { + fn start_test(&self, job_name: &str, params: Vec<(&str, &str)>) + -> Result; +} diff --git a/src/jenkins.rs b/src/jenkins.rs index c76561f1fda2..4738fca7a069 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -35,6 +35,8 @@ use reqwest::header::{HeaderMap, AUTHORIZATION, LOCATION}; use reqwest::{Client, IntoUrl, Response}; use serde_json::{self, Value}; +use ci::{BuildStatus, CIBackend}; + use patchwork::TestState; // Constants @@ -42,12 +44,6 @@ const JENKINS_POLLING_INTERVAL: u64 = 5000; // Polling interval in milliseconds // Jenkins API definitions -pub trait CIBackend { - // TODO: Separate out - fn start_test(&self, job_name: &str, params: Vec<(&str, &str)>) - -> Result; -} - pub struct JenkinsBackend { pub base_url: String, pub reqwest_client: Arc, @@ -85,12 +81,6 @@ impl CIBackend for JenkinsBackend { } } -#[derive(Eq, PartialEq)] -pub enum BuildStatus { - Running, - Done, -} - impl JenkinsBackend { fn headers(&self) -> HeaderMap { let mut headers = HeaderMap::new(); diff --git a/src/main.rs b/src/main.rs index 19989c87cb01..b6c756815595 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,8 +58,11 @@ use std::time::Duration; mod patchwork; use patchwork::{PatchworkServer, TestResult, TestState}; +mod ci; +use ci::CIBackend; + mod jenkins; -use jenkins::{CIBackend, JenkinsBackend}; +use jenkins::JenkinsBackend; mod settings; use settings::{Config, Project}; From patchwork Fri Feb 1 06:17:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 1034601 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43rRjW3pd1z9sDX for ; Fri, 1 Feb 2019 17:17:51 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 43rRjW2nDyzDqg9 for ; Fri, 1 Feb 2019 17:17:51 +1100 (AEDT) X-Original-To: snowpatch@lists.ozlabs.org Delivered-To: snowpatch@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=au1.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=andrew.donnellan@au1.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 43rRjB4jzYzDqfK for ; Fri, 1 Feb 2019 17:17:34 +1100 (AEDT) Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x1166eti125001 for ; Fri, 1 Feb 2019 01:17:32 -0500 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2qcgangwe3-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 01 Feb 2019 01:17:32 -0500 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 1 Feb 2019 06:17:30 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 1 Feb 2019 06:17:27 -0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x116HQUq4325722 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 1 Feb 2019 06:17:26 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AB318A4065 for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0FAEFA405F for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from intelligence.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id E0165A0274 for ; Fri, 1 Feb 2019 17:17:24 +1100 (AEDT) From: Andrew Donnellan To: snowpatch@lists.ozlabs.org Date: Fri, 1 Feb 2019 17:17:14 +1100 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> References: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19020106-4275-0000-0000-00000308B34D X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19020106-4276-0000-0000-00003816BFF0 Message-Id: <20190201061714.7110-5-andrew.donnellan@au1.ibm.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-02-01_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902010044 Subject: [snowpatch] [PATCH 5/5] ci: Move externally-visible prototypes from JenkinsBackend to CIBackend X-BeenThere: snowpatch@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Continuous Integration for patch-based workflows List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: snowpatch-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "snowpatch" Move the functions we actually use from the main code from the JenkinsBackend impl into the CIBackend trait. Signed-off-by: Andrew Donnellan --- src/ci.rs | 11 +++++ src/jenkins.rs | 136 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 79 insertions(+), 68 deletions(-) diff --git a/src/ci.rs b/src/ci.rs index 7d21edb7662f..82af1594f2c7 100644 --- a/src/ci.rs +++ b/src/ci.rs @@ -14,6 +14,11 @@ // ci.rs - CI backend interface definitions // +use std::collections::BTreeMap; +use std::error::Error; + +use patchwork::TestState; + #[derive(Eq, PartialEq)] pub enum BuildStatus { Running, @@ -23,4 +28,10 @@ pub enum BuildStatus { pub trait CIBackend { fn start_test(&self, job_name: &str, params: Vec<(&str, &str)>) -> Result; + fn get_build_handle(&self, build_queue_entry: &str) -> Result>; + fn get_build_result(&self, build_handle: &str) -> Result>; + fn get_results_url(&self, build_handle: &str, job: &BTreeMap) -> String; + fn get_description(&self, build_handle: &str, job: &BTreeMap) + -> Option; + fn wait_build(&self, build_handle: &str) -> Result>; } diff --git a/src/jenkins.rs b/src/jenkins.rs index 4738fca7a069..91ee2eafddf6 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -79,63 +79,8 @@ impl CIBackend for JenkinsBackend { None => Err("No Location header returned"), } } -} - -impl JenkinsBackend { - fn headers(&self) -> HeaderMap { - let mut headers = HeaderMap::new(); - if let Some(ref username) = self.username { - if let Some(ref token) = self.token { - headers.insert( - AUTHORIZATION, - format!( - "Basic {}", - base64::encode(&format!("{}:{}", username, token)) - ) - .parse() - .unwrap(), - ); - } - }; - headers - } - - fn get_url(&self, url: U) -> Result { - self.reqwest_client.get(url).headers(self.headers()).send() - } - fn post_url(&self, url: U) -> Result { - self.reqwest_client.post(url).headers(self.headers()).send() - } - - fn get_api_json_object(&self, base_url: &str) -> Result> { - let url = format!("{}api/json", base_url); - let mut result_str = String::new(); - loop { - let mut resp = match self.get_url(&url) { - Ok(r) => r, - Err(e) => { - // TODO: We have to time out rather than spinning indefinitely - warn!("Couldn't hit Jenkins API: {}", e); - sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); - continue; - } - }; - - if resp.status().is_server_error() { - // TODO: Timeout - sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); - continue; - } - resp.read_to_string(&mut result_str) - .map_err(|e| format!("Couldn't read from server: {}", e))?; - break; - } - serde_json::from_str(&result_str) - .map_err(|e| format!("Couldn't parse JSON from Jenkins: {}", e).into()) - } - - pub fn get_build_handle(&self, build_queue_entry: &str) -> Result> { + fn get_build_handle(&self, build_queue_entry: &str) -> Result> { loop { let entry = self.get_api_json_object(build_queue_entry)?; match entry.get("executable") { @@ -156,15 +101,7 @@ impl JenkinsBackend { } } - pub fn get_build_status(&self, build_handle: &str) -> Result> { - match self.get_api_json_object(build_handle)?["building"].as_bool() { - Some(true) => Ok(BuildStatus::Running), - Some(false) => Ok(BuildStatus::Done), - None => Err("Error getting build status".into()), - } - } - - pub fn get_build_result(&self, build_handle: &str) -> Result> { + fn get_build_result(&self, build_handle: &str) -> Result> { match self .get_api_json_object(build_handle)? .get("result") @@ -181,7 +118,7 @@ impl JenkinsBackend { } } - pub fn get_results_url(&self, build_handle: &str, job: &BTreeMap) -> String { + fn get_results_url(&self, build_handle: &str, job: &BTreeMap) -> String { let default_url = format!("{}/", build_handle); match job.get("artifact") { Some(artifact) => { @@ -198,7 +135,7 @@ impl JenkinsBackend { } } - pub fn get_description( + fn get_description( &self, build_handle: &str, job: &BTreeMap, @@ -220,7 +157,7 @@ impl JenkinsBackend { } } - pub fn wait_build(&self, build_handle: &str) -> Result> { + fn wait_build(&self, build_handle: &str) -> Result> { // TODO: Implement a timeout? while self.get_build_status(build_handle)? != BuildStatus::Done { sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); @@ -228,3 +165,66 @@ impl JenkinsBackend { Ok(BuildStatus::Done) } } + +impl JenkinsBackend { + fn headers(&self) -> HeaderMap { + let mut headers = HeaderMap::new(); + if let Some(ref username) = self.username { + if let Some(ref token) = self.token { + headers.insert( + AUTHORIZATION, + format!( + "Basic {}", + base64::encode(&format!("{}:{}", username, token)) + ) + .parse() + .unwrap(), + ); + } + }; + headers + } + + fn get_url(&self, url: U) -> Result { + self.reqwest_client.get(url).headers(self.headers()).send() + } + + fn post_url(&self, url: U) -> Result { + self.reqwest_client.post(url).headers(self.headers()).send() + } + + fn get_api_json_object(&self, base_url: &str) -> Result> { + let url = format!("{}api/json", base_url); + let mut result_str = String::new(); + loop { + let mut resp = match self.get_url(&url) { + Ok(r) => r, + Err(e) => { + // TODO: We have to time out rather than spinning indefinitely + warn!("Couldn't hit Jenkins API: {}", e); + sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); + continue; + } + }; + + if resp.status().is_server_error() { + // TODO: Timeout + sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); + continue; + } + resp.read_to_string(&mut result_str) + .map_err(|e| format!("Couldn't read from server: {}", e))?; + break; + } + serde_json::from_str(&result_str) + .map_err(|e| format!("Couldn't parse JSON from Jenkins: {}", e).into()) + } + + pub fn get_build_status(&self, build_handle: &str) -> Result> { + match self.get_api_json_object(build_handle)?["building"].as_bool() { + Some(true) => Ok(BuildStatus::Running), + Some(false) => Ok(BuildStatus::Done), + None => Err("Error getting build status".into()), + } + } +}