From a584b9c783d9d922ef2583f29f6353cc015afa9c Mon Sep 17 00:00:00 2001 From: Pietro Albini Date: Sat, 13 Jul 2019 22:29:34 +0200 Subject: don't require the github token for add-person --- src/github.rs | 48 +++++++++++++++++++++++++++++++++--------------- src/main.rs | 2 +- src/validate.rs | 22 ++++++++++------------ 3 files changed, 44 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/github.rs b/src/github.rs index 55bfc19..d4706f9 100644 --- a/src/github.rs +++ b/src/github.rs @@ -1,4 +1,4 @@ -use failure::{bail, Error, ResultExt}; +use failure::{bail, Error}; use reqwest::header::{self, HeaderValue}; use reqwest::{Client, Method, RequestBuilder}; use std::borrow::Cow; @@ -33,29 +33,40 @@ struct GraphNodes { pub(crate) struct GitHubApi { http: Client, - token: String, + token: Option, } impl GitHubApi { - pub(crate) fn new() -> Result { - let token = std::env::var(TOKEN_VAR) - .with_context(|_| format!("missing environment variable {}", TOKEN_VAR))?; - Ok(GitHubApi { + pub(crate) fn new() -> Self { + GitHubApi { http: Client::new(), - token: token.to_string(), - }) + token: std::env::var(TOKEN_VAR).ok(), + } } - fn prepare(&self, method: Method, url: &str) -> Result { + fn prepare( + &self, + require_auth: bool, + method: Method, + url: &str, + ) -> Result { let url = if url.starts_with("https://") { Cow::Borrowed(url) } else { Cow::Owned(format!("{}{}", API_BASE, url)) }; - Ok(self.http.request(method, url.as_ref()).header( - header::AUTHORIZATION, - HeaderValue::from_str(&format!("token {}", self.token))?, - )) + if require_auth { + self.require_auth()?; + } + + let mut req = self.http.request(method, url.as_ref()); + if let Some(token) = &self.token { + req = req.header( + header::AUTHORIZATION, + HeaderValue::from_str(&format!("token {}", token))?, + ); + } + Ok(req) } fn graphql(&self, query: &str, variables: V) -> Result @@ -69,7 +80,7 @@ impl GitHubApi { variables: V, } let res: GraphResult = self - .prepare(Method::POST, "graphql")? + .prepare(true, Method::POST, "graphql")? .json(&Request { query, variables }) .send()? .error_for_status()? @@ -83,9 +94,16 @@ impl GitHubApi { } } + pub(crate) fn require_auth(&self) -> Result<(), Error> { + if self.token.is_none() { + bail!("missing environment variable {}", TOKEN_VAR); + } + Ok(()) + } + pub(crate) fn user(&self, login: &str) -> Result { Ok(self - .prepare(Method::GET, &format!("users/{}", login))? + .prepare(false, Method::GET, &format!("users/{}", login))? .send()? .error_for_status()? .json()?) diff --git a/src/main.rs b/src/main.rs index d0e6ae2..d8669bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,7 +80,7 @@ fn run() -> Result<(), Error> { email: Option<&'a str>, } - let github = github::GitHubApi::new()?; + let github = github::GitHubApi::new(); let user = github.user(github_name)?; let github_name = user.login; diff --git a/src/validate.rs b/src/validate.rs index 82f2fee..4c4ebfb 100644 --- a/src/validate.rs +++ b/src/validate.rs @@ -34,19 +34,17 @@ pub(crate) fn validate(data: &Data, strict: bool) -> Result<(), Error> { check(data, &mut errors); } - match GitHubApi::new() { - Ok(github) => { - for check in GITHUB_CHECKS { - check(data, &github, &mut errors); - } + let github = GitHubApi::new(); + if let Err(err) = github.require_auth() { + if strict { + return Err(err); + } else { + warn!("couldn't perform checks relying on the GitHub API, some errors will not be detected"); + warn!("cause: {}", err); } - Err(err) => { - if strict { - return Err(err); - } else { - warn!("couldn't perform checks relying on the GitHub API, some errors will not be detected"); - warn!("cause: {}", err); - } + } else { + for check in GITHUB_CHECKS { + check(data, &github, &mut errors); } } -- cgit v1.2.3