summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPietro Albini <pietro@pietroalbini.org>2019-07-13 22:29:34 +0200
committerPietro Albini <pietro@pietroalbini.org>2019-07-13 22:29:34 +0200
commita584b9c783d9d922ef2583f29f6353cc015afa9c (patch)
tree54ce0286c89f7f068b64919d197ba51f38d604d1 /src
parentb8f0a391fedb4fb817c04ff4907336e1eece2214 (diff)
don't require the github token for add-person
Diffstat (limited to 'src')
-rw-r--r--src/github.rs48
-rw-r--r--src/main.rs2
-rw-r--r--src/validate.rs22
3 files changed, 44 insertions, 28 deletions
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<T> {
pub(crate) struct GitHubApi {
http: Client,
- token: String,
+ token: Option<String>,
}
impl GitHubApi {
- pub(crate) fn new() -> Result<Self, Error> {
- 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<RequestBuilder, Error> {
+ fn prepare(
+ &self,
+ require_auth: bool,
+ method: Method,
+ url: &str,
+ ) -> Result<RequestBuilder, Error> {
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<R, V>(&self, query: &str, variables: V) -> Result<R, Error>
@@ -69,7 +80,7 @@ impl GitHubApi {
variables: V,
}
let res: GraphResult<R> = 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<User, Error> {
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);
}
}