@@ -281,6 +281,32 @@ async fn modify_owners(
281
281
Ok ( json ! ( { "msg" : comma_sep_msg, "ok" : true } ) )
282
282
}
283
283
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
+
284
310
/// Invite `login` as an owner of this crate, returning the created
285
311
/// [`NewOwnerInvite`].
286
312
async fn add_owner (
@@ -292,7 +318,7 @@ async fn add_owner(
292
318
) -> Result < NewOwnerInvite , OwnerAddError > {
293
319
use diesel:: insert_into;
294
320
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 ?;
296
322
match owner {
297
323
// Users are invited and must accept before being added
298
324
Owner :: User ( user) => {
0 commit comments