|
@@ -1,7 +1,6 @@
|
|
|
use failure::{Error, ResultExt};
|
|
|
-use reqwest::header::USER_AGENT;
|
|
|
-use reqwest::Error as HttpError;
|
|
|
-use reqwest::{Client, RequestBuilder, Response};
|
|
|
+use reqwest::header::{AUTHORIZATION, USER_AGENT};
|
|
|
+use reqwest::{Client, Error as HttpError, RequestBuilder, Response};
|
|
|
|
|
|
#[derive(Debug, serde::Deserialize)]
|
|
|
pub struct User {
|
|
@@ -9,13 +8,20 @@ pub struct User {
|
|
|
}
|
|
|
|
|
|
impl User {
|
|
|
+ pub fn current(client: &GithubClient) -> Result<Self, Error> {
|
|
|
+ Ok(client
|
|
|
+ .get("https://api.github.com/user")
|
|
|
+ .send_req()?
|
|
|
+ .json()?)
|
|
|
+ }
|
|
|
+
|
|
|
pub fn is_team_member(&self, client: &GithubClient) -> Result<bool, Error> {
|
|
|
let client = client.raw();
|
|
|
let url = format!("{}/teams.json", rust_team_data::v1::BASE_URL);
|
|
|
let permission: rust_team_data::v1::Teams = client
|
|
|
.get(&url)
|
|
|
.send()
|
|
|
- .and_then(|r| r.error_for_status())
|
|
|
+ .and_then(Response::error_for_status)
|
|
|
.and_then(|mut r| r.json())
|
|
|
.context("could not get team data")?;
|
|
|
let map = permission.teams;
|
|
@@ -30,6 +36,7 @@ pub struct Label {
|
|
|
|
|
|
impl Label {
|
|
|
fn exists(&self, repo_api_prefix: &str, client: &GithubClient) -> bool {
|
|
|
+ #[allow(clippy::redundant_pattern_matching)]
|
|
|
match client
|
|
|
.get(&format!("{}/labels/{}", repo_api_prefix, self.name))
|
|
|
.send_req()
|
|
@@ -68,7 +75,7 @@ impl Issue {
|
|
|
}
|
|
|
client
|
|
|
.post(&self.comments_url)
|
|
|
- .json(&PostComment { body: body })
|
|
|
+ .json(&PostComment { body })
|
|
|
.send_req()
|
|
|
.context("failed to post comment")?;
|
|
|
Ok(())
|
|
@@ -150,7 +157,7 @@ trait RequestSend: Sized {
|
|
|
impl RequestSend for RequestBuilder {
|
|
|
fn configure(self, g: &GithubClient) -> RequestBuilder {
|
|
|
self.header(USER_AGENT, "rust-lang-triagebot")
|
|
|
- .basic_auth(&g.username, Some(&g.token))
|
|
|
+ .header(AUTHORIZATION, format!("token {}", g.token))
|
|
|
}
|
|
|
|
|
|
fn send_req(self) -> Result<Response, HttpError> {
|
|
@@ -163,28 +170,19 @@ impl RequestSend for RequestBuilder {
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
pub struct GithubClient {
|
|
|
- username: String,
|
|
|
token: String,
|
|
|
client: Client,
|
|
|
}
|
|
|
|
|
|
impl GithubClient {
|
|
|
- pub fn new(c: Client, token: String, username: String) -> Self {
|
|
|
- GithubClient {
|
|
|
- client: c,
|
|
|
- token,
|
|
|
- username,
|
|
|
- }
|
|
|
+ pub fn new(client: Client, token: String) -> Self {
|
|
|
+ GithubClient { client, token }
|
|
|
}
|
|
|
|
|
|
pub fn raw(&self) -> &Client {
|
|
|
&self.client
|
|
|
}
|
|
|
|
|
|
- pub fn username(&self) -> &str {
|
|
|
- self.username.as_str()
|
|
|
- }
|
|
|
-
|
|
|
fn get(&self, url: &str) -> RequestBuilder {
|
|
|
log::trace!("get {:?}", url);
|
|
|
self.client.get(url).configure(self)
|