-
Notifications
You must be signed in to change notification settings - Fork 643
Begin refactoring module layout #1068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors-voyager
merged 5 commits into
rust-lang:master
from
jtgeibel:begin-module-refactor
Sep 27, 2017
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3543012
Reorganize routes to group them by function
jtgeibel ffc9af3
Move `krate` and `version` api modules into subdirectories
jtgeibel 6793f4a
Move `categories::sync` functionality under a new `boot` module
jtgeibel 52cea71
Pull github interaction out of `http` module into a `github` module
jtgeibel 7e88ba5
Address review comments [skip ci]
jtgeibel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod categories; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! This module implements functionality for interacting with GitHub. | ||
|
||
use curl; | ||
use curl::easy::{Easy, List}; | ||
|
||
use oauth2::*; | ||
|
||
use serde_json; | ||
use serde::Deserialize; | ||
|
||
use std::str; | ||
|
||
use app::App; | ||
use util::{human, internal, CargoResult, ChainError}; | ||
|
||
/// Does all the nonsense for sending a GET to Github. Doesn't handle parsing | ||
/// because custom error-code handling may be desirable. Use | ||
/// `parse_github_response` to handle the "common" processing of responses. | ||
pub fn github(app: &App, url: &str, auth: &Token) -> Result<(Easy, Vec<u8>), curl::Error> { | ||
let url = format!("{}://api.github.com{}", app.config.api_protocol, url); | ||
info!("GITHUB HTTP: {}", url); | ||
|
||
let mut headers = List::new(); | ||
headers | ||
.append("Accept: application/vnd.github.v3+json") | ||
.unwrap(); | ||
headers.append("User-Agent: hello!").unwrap(); | ||
headers | ||
.append(&format!("Authorization: token {}", auth.access_token)) | ||
.unwrap(); | ||
|
||
let mut handle = app.handle(); | ||
handle.url(&url).unwrap(); | ||
handle.get(true).unwrap(); | ||
handle.http_headers(headers).unwrap(); | ||
|
||
let mut data = Vec::new(); | ||
{ | ||
let mut transfer = handle.transfer(); | ||
transfer | ||
.write_function(|buf| { | ||
data.extend_from_slice(buf); | ||
Ok(buf.len()) | ||
}) | ||
.unwrap(); | ||
transfer.perform()?; | ||
} | ||
Ok((handle, data)) | ||
} | ||
|
||
/// Checks for normal responses | ||
pub fn parse_github_response<'de, 'a: 'de, T: Deserialize<'de>>( | ||
mut resp: Easy, | ||
data: &'a [u8], | ||
) -> CargoResult<T> { | ||
match resp.response_code().unwrap() { | ||
200 => {} | ||
// Ok! | ||
403 => { | ||
return Err(human( | ||
"It looks like you don't have permission \ | ||
to query a necessary property from Github \ | ||
to complete this request. \ | ||
You may need to re-authenticate on \ | ||
crates.io to grant permission to read \ | ||
github org memberships. Just go to \ | ||
https://crates.io/login", | ||
)); | ||
} | ||
n => { | ||
let resp = String::from_utf8_lossy(data); | ||
return Err(internal(&format_args!( | ||
"didn't get a 200 result from \ | ||
github, got {} with: {}", | ||
n, | ||
resp | ||
))); | ||
} | ||
} | ||
|
||
let json = str::from_utf8(data) | ||
.ok() | ||
.chain_error(|| internal("github didn't send a utf8-response"))?; | ||
|
||
serde_json::from_str(json).chain_error(|| internal("github didn't send a valid json response")) | ||
} | ||
|
||
/// Gets a token with the given string as the access token, but all | ||
/// other info null'd out. Generally, just to be fed to the `github` fn. | ||
pub fn token(token: String) -> Token { | ||
Token { | ||
access_token: token, | ||
scopes: Vec::new(), | ||
token_type: String::new(), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
use diesel::prelude::*; | ||
|
||
use app::App; | ||
use http; | ||
use github; | ||
use schema::*; | ||
use util::{human, CargoResult}; | ||
use {Crate, User}; | ||
|
@@ -147,7 +147,7 @@ impl Team { | |
/// Tries to create a Github Team from scratch. Assumes `org` and `team` are | ||
/// correctly parsed out of the full `name`. `name` is passed as a | ||
/// convenience to avoid rebuilding it. | ||
pub fn create_github_team( | ||
fn create_github_team( | ||
app: &App, | ||
conn: &PgConnection, | ||
login: &str, | ||
|
@@ -184,9 +184,9 @@ impl Team { | |
// FIXME: we just set per_page=100 and don't bother chasing pagination | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it turns out that the only thing using this function is on line 138 in |
||
// links. A hundred teams should be enough for any org, right? | ||
let url = format!("/orgs/{}/teams?per_page=100", org_name); | ||
let token = http::token(req_user.gh_access_token.clone()); | ||
let (handle, data) = http::github(app, &url, &token)?; | ||
let teams: Vec<GithubTeam> = http::parse_github_response(handle, &data)?; | ||
let token = github::token(req_user.gh_access_token.clone()); | ||
let (handle, data) = github::github(app, &url, &token)?; | ||
let teams: Vec<GithubTeam> = github::parse_github_response(handle, &data)?; | ||
|
||
let team = teams | ||
.into_iter() | ||
|
@@ -209,8 +209,8 @@ impl Team { | |
} | ||
|
||
let url = format!("/orgs/{}", org_name); | ||
let (handle, resp) = http::github(app, &url, &token)?; | ||
let org: Org = http::parse_github_response(handle, &resp)?; | ||
let (handle, resp) = github::github(app, &url, &token)?; | ||
let org: Org = github::parse_github_response(handle, &resp)?; | ||
|
||
NewTeam::new(login, team.id, team.name, org.avatar_url).create_or_update(conn) | ||
} | ||
|
@@ -276,15 +276,15 @@ fn team_with_gh_id_contains_user(app: &App, github_id: i32, user: &User) -> Carg | |
} | ||
|
||
let url = format!("/teams/{}/memberships/{}", &github_id, &user.gh_login); | ||
let token = http::token(user.gh_access_token.clone()); | ||
let (mut handle, resp) = http::github(app, &url, &token)?; | ||
let token = github::token(user.gh_access_token.clone()); | ||
let (mut handle, resp) = github::github(app, &url, &token)?; | ||
|
||
// Officially how `false` is returned | ||
if handle.response_code().unwrap() == 404 { | ||
return Ok(false); | ||
} | ||
|
||
let membership: Membership = http::parse_github_response(handle, &resp)?; | ||
let membership: Membership = github::parse_github_response(handle, &resp)?; | ||
|
||
// There is also `state: pending` for which we could possibly give | ||
// some feedback, but it's not obvious how that should work. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a doc comment here to give a high level overview of what this module does and where it's used?