Skip to content

Commit c2ddecd

Browse files
committed
update erythang
1 parent 19d59ae commit c2ddecd

File tree

5 files changed

+141
-136
lines changed

5 files changed

+141
-136
lines changed

src/bin/migrate.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,11 @@ fn migrations() -> Vec<Migration> {
456456
),
457457
Migration::add_table(20150804170128, "teams", "
458458
id SERIAL PRIMARY KEY,
459-
name VARCHAR NOT NULL UNIQUE,
460-
github_id INTEGER NOT NULL UNIQUE
459+
login VARCHAR NOT NULL UNIQUE,
460+
github_id INTEGER NOT NULL UNIQUE,
461+
name VARCHAR,
462+
url VARCHAR,
463+
avatar VARCHAR
461464
"),
462465
Migration::run(20150804170129,
463466
"ALTER TABLE crate_owners RENAME user_id TO owner_id",

src/http.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
use curl;
22
use oauth2::*;
33
use app::App;
4+
use util::{CargoResult, internal, ChainError, human};
5+
use rustc_serialize::{json, Decodable};
6+
use std::str;
47

5-
/// Does all the nonsense for sending a GET to Github
8+
9+
/// Does all the nonsense for sending a GET to Github. Doesn't handle parsing
10+
/// because custom error-code handling may be desirable. Use
11+
/// parse_github_response to handle the "common" processing of responses.
612
pub fn github(app: &App, url: &str, auth: &Token)
713
-> Result<curl::http::Response, curl::ErrCode> {
814
info!("HTTP: {}", url);
@@ -14,6 +20,35 @@ pub fn github(app: &App, url: &str, auth: &Token)
1420
.exec()
1521
}
1622

23+
/// Checks for normal responses
24+
pub fn parse_github_response<T: Decodable>(resp: curl::http::Response)
25+
-> CargoResult<T> {
26+
match resp.get_code() {
27+
200 => {} // Ok!
28+
403 => {
29+
return Err(human("It looks like you don't have permission \
30+
to query a necessary property from Github
31+
to complete this request. \
32+
You may need to re-authenticate on \
33+
crates.io to grant permission to read \
34+
github org memberships. Just go to \
35+
https://crates.io/login"));
36+
}
37+
_ => {
38+
return Err(internal(format!("didn't get a 200 result from
39+
github: {}", resp)));
40+
}
41+
}
42+
43+
let json = try!(str::from_utf8(resp.get_body()).ok().chain_error(||{
44+
internal("github didn't send a utf8-response")
45+
}));
46+
47+
json::decode(json).chain_error(|| {
48+
internal("github didn't send a valid json response")
49+
})
50+
}
51+
1752
/// Gets a token with the given string as the access token, but all
1853
/// other info null'd out. Generally, just to be fed to the `github` fn.
1954
pub fn token(token: String) -> Token {

src/krate.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,17 @@ impl Crate {
314314
}
315315

316316
pub fn owner_add(&self, app: &App, conn: &Connection, req_user: &User,
317-
name: &str) -> CargoResult<()> {
317+
login: &str) -> CargoResult<()> {
318318

319-
let owner = match Owner::find_by_name(conn, name) {
319+
let owner = match Owner::find_by_login(conn, login) {
320320
Ok(owner@Owner::User(_)) => { owner }
321321
Ok(Owner::Team(team)) => if try!(team.contains_user(app, req_user)) {
322322
Owner::Team(team)
323323
} else {
324-
return Err(human(format!("only members of {} can add it as an owner", name)));
324+
return Err(human(format!("only members of {} can add it as an owner", login)));
325325
},
326-
Err(err) => if name.contains(":") {
327-
Owner::Team(try!(Team::create(app, conn, name, req_user)))
326+
Err(err) => if login.contains(":") {
327+
Owner::Team(try!(Team::create(app, conn, login, req_user)))
328328
} else {
329329
return Err(err);
330330
},
@@ -339,9 +339,9 @@ impl Crate {
339339
}
340340

341341
pub fn owner_remove(&self, conn: &Connection, _req_user: &User,
342-
name: &str) -> CargoResult<()> {
343-
let owner = try!(Owner::find_by_name(conn, name).map_err(|_| {
344-
human(format!("could not find user with login `{}`", name))
342+
login: &str) -> CargoResult<()> {
343+
let owner = try!(Owner::find_by_login(conn, login).map_err(|_| {
344+
human(format!("could not find owner with login `{}`", login))
345345
}));
346346
try!(conn.execute("UPDATE crate_owners
347347
SET deleted = TRUE, updated_at = $1
@@ -1022,23 +1022,23 @@ fn modify_owners(req: &mut Request, add: bool) -> CargoResult<Response> {
10221022
human("invalid json request")
10231023
}));
10241024

1025-
let names = try!(request.owners.or(request.users).ok_or_else(|| {
1025+
let logins = try!(request.owners.or(request.users).ok_or_else(|| {
10261026
human("invalid json request")
10271027
}));
10281028

1029-
for name in &names {
1029+
for login in &logins {
10301030
if add {
1031-
if owners.iter().any(|owner| owner.name() == *name) {
1032-
return Err(human(format!("`{}` is already an owner", name)))
1031+
if owners.iter().any(|owner| owner.login() == *login) {
1032+
return Err(human(format!("`{}` is already an owner", login)))
10331033
}
1034-
try!(krate.owner_add(req.app(), tx, &user, &name));
1034+
try!(krate.owner_add(req.app(), tx, &user, &login));
10351035
} else {
10361036
// Removing the team that gives you rights is prevented because
10371037
// team members only have Rights::Publish
1038-
if *name == user.gh_login {
1038+
if *login == user.gh_login {
10391039
return Err(human("cannot remove yourself as an owner"))
10401040
}
1041-
try!(krate.owner_remove(tx, &user, &name));
1041+
try!(krate.owner_remove(tx, &user, &login));
10421042
}
10431043
}
10441044

0 commit comments

Comments
 (0)