From patchwork Thu Jun 16 00:59:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 636152 X-Patchwork-Delegate: ruscur@russell.cc Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rVQ5l5dlKz9t1n for ; Thu, 16 Jun 2016 10:59:55 +1000 (AEST) Received: from ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3rVQ5l4XDxzDqdL for ; Thu, 16 Jun 2016 10:59:55 +1000 (AEST) X-Original-To: snowpatch@lists.ozlabs.org Delivered-To: snowpatch@lists.ozlabs.org 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 3rVQ5h3H0VzDqd0 for ; Thu, 16 Jun 2016 10:59:51 +1000 (AEST) Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.11/8.16.0.11) with SMTP id u5G0xZ9r123732 for ; Wed, 15 Jun 2016 20:59:50 -0400 Received: from e23smtp06.au.ibm.com (e23smtp06.au.ibm.com [202.81.31.148]) by mx0a-001b2d01.pphosted.com with ESMTP id 23jgmphu4e-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Wed, 15 Jun 2016 20:59:50 -0400 Received: from localhost by e23smtp06.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 16 Jun 2016 10:59:46 +1000 Received: from d23dlp02.au.ibm.com (202.81.31.213) by e23smtp06.au.ibm.com (202.81.31.212) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 16 Jun 2016 10:59:45 +1000 X-IBM-Helo: d23dlp02.au.ibm.com X-IBM-MailFrom: andrew.donnellan@au1.ibm.com X-IBM-RcptTo: snowpatch@lists.ozlabs.org Received: from d23relay07.au.ibm.com (d23relay07.au.ibm.com [9.190.26.37]) by d23dlp02.au.ibm.com (Postfix) with ESMTP id EE3482BB0063 for ; Thu, 16 Jun 2016 10:59:44 +1000 (EST) Received: from d23av06.au.ibm.com (d23av06.au.ibm.com [9.190.235.151]) by d23relay07.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id u5G0xiWu63963270 for ; Thu, 16 Jun 2016 10:59:44 +1000 Received: from d23av06.au.ibm.com (localhost [127.0.0.1]) by d23av06.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id u5G0xiCB005762 for ; Thu, 16 Jun 2016 10:59:44 +1000 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.192.253.14]) by d23av06.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id u5G0xiQM005752 for ; Thu, 16 Jun 2016 10:59:44 +1000 Received: from ajd.au.ibm.com (unknown [9.192.165.206]) (using TLSv1.2 with cipher AES128-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id D6D8AA0138 for ; Thu, 16 Jun 2016 10:59:43 +1000 (AEST) From: Andrew Donnellan To: snowpatch@lists.ozlabs.org Date: Thu, 16 Jun 2016 10:59:36 +1000 X-Mailer: git-send-email 2.8.1 X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16061600-0040-0000-0000-000001B506DC X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 16061600-0041-0000-0000-000009E4194C Message-Id: <1466038776-7096-1-git-send-email-andrew.donnellan@au1.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2016-06-15_15:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=4 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1604210000 definitions=main-1606160011 Subject: [snowpatch] [PATCH] jenkins: refactor API HTTP GET boilerplate into new method X-BeenThere: snowpatch@lists.ozlabs.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Continuous Integration for patch-based workflows List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: snowpatch-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "snowpatch" All our side-effect-free Jenkins API calls follow the same pattern of creating a hyper Client, making a GET request to the relevant JSON API endpoint, and decoding the returned JSON into a JSON Object (right now the top-level data types are all Objects). Refactor this boilerplate into a new method, get_api_json_object(). Signed-off-by: Andrew Donnellan --- Build tested only. --- src/jenkins.rs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/jenkins.rs b/src/jenkins.rs index 8ce9c3c..fb10829 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -74,34 +74,27 @@ pub enum JenkinsBuildStatus { } impl<'a> JenkinsBackend<'a> { - pub fn get_build_url(&self, build_queue_entry: &str) -> Option { - let client = Client::new(); // TODO - let url = format!("{}api/json", build_queue_entry); - - let mut resp = client.get(&url).send().expect("HTTP request error"); // TODO don't panic here + fn get_api_json_object(&self, base_url: &str) -> rustc_serialize::json::Object { + // TODO: Don't panic on failure, fail more gracefully + let client = Client::new(); // TODO: Can we reduce the number of Clients we create? + let url = format!("{}api/json", base_url); + let mut resp = client.get(&url).send().expect("HTTP request error"); let mut result_str = String::new(); resp.read_to_string(&mut result_str) .unwrap_or_else(|err| panic!("Couldn't read from server: {}", err)); - let json = Json::from_str(&result_str).unwrap(); - let obj = json.as_object().unwrap(); - - match obj.get("executable") { + let json = Json::from_str(&result_str).unwrap_or_else(|err| panic!("Couldn't parse JSON from Jenkins: {}", err)); + json.as_object().unwrap().clone() + } + + pub fn get_build_url(&self, build_queue_entry: &str) -> Option { + match self.get_api_json_object(build_queue_entry).get("executable") { Some(exec) => Some(exec.as_object().unwrap().get("url").unwrap().as_string().unwrap().to_string()), None => None } } pub fn get_build_status(&self, build_url: &str) -> JenkinsBuildStatus { - let client = Client::new(); - let url = format!("{}api/json", build_url); - let mut resp = client.get(&url).send().expect("HTTP request error"); - let mut result_str = String::new(); - resp.read_to_string(&mut result_str) - .unwrap_or_else(|err| panic!("Couldn't read from server: {}", err)); - let json = Json::from_str(&result_str) - .unwrap_or_else(|err| panic!("Couldn't decode JSON: {}", err)); - - match json.as_object().unwrap().get("building").unwrap().as_boolean().unwrap() { + match self.get_api_json_object(build_url).get("building").unwrap().as_boolean().unwrap() { true => JenkinsBuildStatus::Running, false => JenkinsBuildStatus::Done, }