@@ -281,6 +281,32 @@ async fn modify_owners(
281281 Ok ( json ! ( { "msg" : comma_sep_msg, "ok" : true } ) )
282282}
283283
284+ /// Finds the owner by name. Always recreates teams to get the most
285+ /// up-to-date GitHub ID. Fails out if the user isn't found in the
286+ /// database, the team isn't found on GitHub, or if the user isn't a member
287+ /// of the team on GitHub.
288+ ///
289+ /// May be a user's GH login or a full team name. This is case
290+ /// sensitive.
291+ pub async fn find_or_create_owner (
292+ app : & App ,
293+ conn : & mut AsyncPgConnection ,
294+ req_user : & User ,
295+ name : & str ,
296+ ) -> AppResult < Owner > {
297+ if name. contains ( ':' ) {
298+ Ok ( Owner :: Team (
299+ Team :: create_or_update ( app, conn, name, req_user) . await ?,
300+ ) )
301+ } else {
302+ User :: find_by_login ( conn, name)
303+ . await
304+ . optional ( ) ?
305+ . map ( Owner :: User )
306+ . ok_or_else ( || bad_request ( format_args ! ( "could not find user with login `{name}`" ) ) )
307+ }
308+ }
309+
284310/// Invite `login` as an owner of this crate, returning the created
285311/// [`NewOwnerInvite`].
286312async fn add_owner (
@@ -292,7 +318,7 @@ async fn add_owner(
292318) -> Result < NewOwnerInvite , OwnerAddError > {
293319 use diesel:: insert_into;
294320
295- let owner = Owner :: find_or_create_by_login ( app, conn, req_user, login) . await ?;
321+ let owner = find_or_create_owner ( app, conn, req_user, login) . await ?;
296322 match owner {
297323 // Users are invited and must accept before being added
298324 Owner :: User ( user) => {
0 commit comments