[1/3] setup_rustfmt_hook: create hooks dir if it doesn't exist
diff mbox series

Message ID 20181109042154.16361-1-ruscur@russell.cc
State Superseded
Headers show
Series
  • [1/3] setup_rustfmt_hook: create hooks dir if it doesn't exist
Related show

Commit Message

Russell Currey Nov. 9, 2018, 4:21 a.m. UTC
Signed-off-by: Russell Currey <ruscur@russell.cc>
---
 setup_rustfmt_hook.sh |  1 +
 src/jenkins.rs        | 25 ++++++++++++-------------
 src/main.rs           |  1 +
 src/patchwork.rs      | 42 ++++++++++++++++++------------------------
 4 files changed, 32 insertions(+), 37 deletions(-)

Comments

Russell Currey Nov. 9, 2018, 4:25 a.m. UTC | #1
On Fri, 2018-11-09 at 15:21 +1100, Russell Currey wrote:
> Signed-off-by: Russell Currey <ruscur@russell.cc>

Oops, looks like there were some rustfmt that got automatically added
to this patch.  Won't resend for now.

Patch
diff mbox series

diff --git a/setup_rustfmt_hook.sh b/setup_rustfmt_hook.sh
index 76a8932..4088926 100755
--- a/setup_rustfmt_hook.sh
+++ b/setup_rustfmt_hook.sh
@@ -2,6 +2,7 @@ 
 rustup component add rustfmt-preview
 
 rustfmt_path=`which rustfmt`
+mkdir -p .git/hooks
 echo "#!/bin/bash
 declare -a rust_files=()
 files=\$(git diff-index --name-only HEAD)
diff --git a/src/jenkins.rs b/src/jenkins.rs
index 0aa5212..9db11f3 100644
--- a/src/jenkins.rs
+++ b/src/jenkins.rs
@@ -29,7 +29,7 @@  use std::sync::Arc;
 use std::thread::sleep;
 use std::time::Duration;
 
-use reqwest::header::{Authorization, Basic, Headers, Location};
+use reqwest::header::{HeaderMap, AUTHORIZATION, LOCATION};
 use reqwest::{Client, IntoUrl, Response};
 use serde_json::{self, Value};
 
@@ -68,8 +68,8 @@  impl CIBackend for JenkinsBackend {
             .extend_pairs(params)
             .finish();
 
-        let resp =
-            self.post_url(&format!(
+        let resp = self
+            .post_url(&format!(
                 "{}/job/{}/buildWithParameters?{}",
                 self.base_url, job_name, params
             )).expect("HTTP request error"); // TODO don't panic here
@@ -88,8 +88,8 @@  pub enum JenkinsBuildStatus {
 }
 
 impl JenkinsBackend {
-    fn headers(&self) -> Headers {
-        let mut headers = Headers::new();
+    fn headers(&self) -> HeaderMap {
+        let mut headers = HeaderMap::new();
         if let Some(ref username) = self.username {
             headers.set(Authorization(Basic {
                 username: username.clone(),
@@ -139,14 +139,13 @@  impl JenkinsBackend {
             match entry.get("executable") {
                 Some(exec) => {
                     return Some(
-                        exec
-                                          .as_object() // Option<BTreeMap>
-                                          .unwrap() // BTreeMap
-                                          .get("url") // Option<&str>
-                                          .unwrap() // &str ?
-                                          .as_str()
-                                          .unwrap()
-                                          .to_string(),
+                        exec.as_object() // Option<BTreeMap>
+                            .unwrap() // BTreeMap
+                            .get("url") // Option<&str>
+                            .unwrap() // &str ?
+                            .as_str()
+                            .unwrap()
+                            .to_string(),
                     );
                 }
                 None => sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)),
diff --git a/src/main.rs b/src/main.rs
index 7a0deba..6fe0bdb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -30,6 +30,7 @@  extern crate env_logger;
 extern crate serde;
 #[macro_use]
 extern crate serde_derive;
+extern crate base64;
 extern crate serde_json;
 extern crate toml;
 
diff --git a/src/patchwork.rs b/src/patchwork.rs
index a0722c7..0072225 100644
--- a/src/patchwork.rs
+++ b/src/patchwork.rs
@@ -25,9 +25,7 @@  use std::result::Result;
 use tempdir::TempDir;
 
 use reqwest;
-use reqwest::header::{
-    qitem, Accept, Authorization, Basic, Connection, ContentType, Headers, Link, RelationType,
-};
+use reqwest::header::{HeaderMap, ACCEPT, AUTHORIZATION, CONTENT_TYPE};
 use reqwest::Client;
 use reqwest::Response;
 use reqwest::StatusCode;
@@ -35,6 +33,8 @@  use reqwest::StatusCode;
 use serde::{self, Serializer};
 use serde_json;
 
+use base64;
+
 use utils;
 
 // TODO: more constants.  constants for format strings of URLs and such.
@@ -205,16 +205,16 @@  impl TestResult {
 
 pub struct PatchworkServer {
     pub url: String,
-    headers: Headers,
+    headers: HeaderMap,
     pub client: std::sync::Arc<Client>,
 }
 
 impl PatchworkServer {
     #[cfg_attr(feature = "cargo-clippy", allow(ptr_arg))]
     pub fn new(url: &String, client: &std::sync::Arc<Client>) -> PatchworkServer {
-        let mut headers = Headers::new();
-        headers.set(Accept(vec![qitem(reqwest::mime::APPLICATION_JSON)]));
-        headers.set(ContentType(reqwest::mime::APPLICATION_JSON));
+        let mut headers = HeaderMap::new();
+        headers.insert(ACCEPT, "application/json");
+        headers.insert(CONTENT_TYPE, "application/json");
         PatchworkServer {
             url: url.clone(),
             client: client.clone(),
@@ -231,34 +231,28 @@  impl PatchworkServer {
     ) {
         match (username, password, token) {
             (&None, &None, &Some(ref token)) => {
-                self.headers.set(Authorization(format!("Token {}", token)));
+                self.headers
+                    .insert(AUTHORIZATION, format!("Token {}", token));
             }
             (&Some(ref username), &Some(ref password), &None) => {
-                self.headers.set(Authorization(Basic {
-                    username: username.clone(),
-                    password: Some(password.clone()),
-                }));
+                self.headers.insert(
+                    AUTHORIZATION,
+                    format!(
+                        "Basic {}",
+                        base64::encode(format!("{}:{}", username, password))
+                    ),
+                );
             }
             _ => panic!("Invalid patchwork authentication details"),
         }
     }
 
     pub fn get_url(&self, url: &str) -> std::result::Result<Response, reqwest::Error> {
-        self.client
-            .get(&*url)
-            .headers(self.headers.clone())
-            .header(Connection::close())
-            .send()
+        self.client.get(&*url).headers(self.headers.clone()).send()
     }
 
     pub fn get_url_string(&self, url: &str) -> std::result::Result<String, reqwest::Error> {
-        let mut resp = try!(
-            self.client
-                .get(&*url)
-                .headers(self.headers.clone())
-                .header(Connection::close())
-                .send()
-        );
+        let mut resp = try!(self.client.get(&*url).headers(self.headers.clone()).send());
         let mut body: Vec<u8> = vec![];
         io::copy(&mut resp, &mut body).unwrap();
         Ok(String::from_utf8(body).unwrap())