Skip to content

Commit da16e83

Browse files
committed
refactor
1 parent 30f2049 commit da16e83

File tree

2 files changed

+14
-35
lines changed

2 files changed

+14
-35
lines changed

src/krate.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use git;
2828
use keyword::EncodableKeyword;
2929
use upload;
3030
use user::RequestUser;
31-
use owner::{EncodableOwner, Owner, Rights, OwnerKind, rights};
31+
use owner::{EncodableOwner, Owner, Rights, OwnerKind, Team, rights};
3232
use util::errors::{NotFound, CargoError};
3333
use util::{LimitErrorReader, HashingReader};
3434
use util::{RequestUtils, CargoResult, internal, ChainError, human};
@@ -318,7 +318,19 @@ impl Crate {
318318
pub fn owner_add(&self, app: &App, conn: &Connection, req_user: &User,
319319
name: &str) -> CargoResult<()> {
320320

321-
let owner = try!(Owner::find_by_name_for_add(app, conn, name, req_user));
321+
let owner = match Owner::find_by_name(conn, name) {
322+
Ok(owner@Owner::User(_)) => { owner }
323+
Ok(Owner::Team(team)) => if try!(team.contains_user(app, req_user)) {
324+
Owner::Team(team)
325+
} else {
326+
return Err(human(format!("only members of {} can add it as an owner", name)));
327+
},
328+
Err(err) => if name.contains(":") {
329+
Owner::Team(try!(Team::create(app, conn, name, req_user)))
330+
} else {
331+
return Err(err);
332+
},
333+
};
322334

323335
try!(conn.execute("INSERT INTO crate_owners
324336
(crate_id, owner_id, created_at, updated_at,

src/owner.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -253,39 +253,6 @@ impl Owner {
253253
Ok(owner)
254254
}
255255

256-
/// Find the owner by name, with the intent of adding it as an owner.
257-
///
258-
/// This differs from find_by_name in that in the case of a Team,
259-
/// it will verify the req_user is on the team first.
260-
///
261-
/// If the req_user is on the Team, it will create the team in the DB
262-
/// if it is not already present. When this occurs, this will set the
263-
/// One True Name of the team. All future references to the team must
264-
/// use the exact casing provided here. If a different casing is provided
265-
/// to this method, we may still succeed if Github returns us the same ID
266-
/// as the One True Name. However in this case, the One True Name will
267-
/// still be selected.
268-
pub fn find_by_name_for_add(app: &App, conn: &Connection, name: &str, req_user: &User)
269-
-> CargoResult<Owner> {
270-
if !name.contains(":") {
271-
return Ok(Owner::User(try!(User::find_by_login(conn, name).map_err(|_|
272-
human(format!("could not find user with login `{}`", name))
273-
))));
274-
}
275-
276-
// We're working with a Team, try to just get it out of the DB.
277-
if let Ok(team) = Team::find_by_name(conn, name) {
278-
return if try!(team.contains_user(app, req_user)) {
279-
Ok(Owner::Team(team))
280-
} else {
281-
Err(human(format!("only members of {} can add it as an owner", name)))
282-
};
283-
}
284-
285-
// Failed to retrieve from the DB, must be a new Team, try to add it.
286-
Ok(Owner::Team(try!(Team::create(app, conn, name, req_user))))
287-
}
288-
289256
pub fn kind(&self) -> i32 {
290257
match *self {
291258
Owner::User(_) => OwnerKind::User as i32,

0 commit comments

Comments
 (0)