@@ -5,27 +5,30 @@ import { trimTrailingDotGit } from '../../../db/helper';
55// Execute if the repo is approved
66const exec = async ( req : any , action : Action ) : Promise < Action > => {
77 const step = new Step ( 'checkUserPushPermission' ) ;
8- const user = action . user ;
8+ const userEmail = action . userEmail ;
99
10- if ( ! user ) {
11- console . log ( 'Action has no user set. This may be due to a fast-forward ref update. Deferring to getMissingData action.' ) ;
10+ if ( ! userEmail ) {
11+ console . log (
12+ 'Action has no userEmail set. This may be due to a fast-forward ref update. Deferring to getMissingData action.' ,
13+ ) ;
1214 return action ;
1315 }
1416
15- return await validateUser ( user , action , step ) ;
17+ return await validateUser ( userEmail , action , step ) ;
1618} ;
1719
1820/**
1921 * Helper that validates the user's push permission.
2022 * This can be used by other actions that need it. For example, when the user is missing from the commit data,
2123 * validation is deferred to getMissingData, but the logic is the same.
22- * @param {string } user The user to validate
24+ * @param {string } userEmail The user email to validate
2325 * @param {Action } action The action object
2426 * @param {Step } step The step object
2527 * @return {Promise<Action> } The action object
2628 */
27- const validateUser = async ( user : string , action : Action , step : Step ) : Promise < Action > => {
29+ const validateUser = async ( userEmail : string , action : Action , step : Step ) : Promise < Action > => {
2830 const repoSplit = trimTrailingDotGit ( action . repo . toLowerCase ( ) ) . split ( '/' ) ;
31+
2932 // we expect there to be exactly one / separating org/repoName
3033 if ( repoSplit . length != 2 ) {
3134 step . setError ( 'Server-side issue extracting repoName' ) ;
@@ -36,13 +39,8 @@ const validateUser = async (user: string, action: Action, step: Step): Promise<A
3639 const repoName = repoSplit [ 1 ] ;
3740 let isUserAllowed = false ;
3841
39- // n.b. action.user will be set to whatever the user had set in their user.name config in their git client.
40- // it is not necessarily the GitHub username. GitHub looks users by email address as should we
41- const userEmail = action . userEmail ;
42- let username = 'unknown' ;
43-
4442 // Find the user associated with this email address
45- const list = await getUsers ( { email : action . userEmail } ) ;
43+ const list = await getUsers ( { email : userEmail } ) ;
4644
4745 if ( list . length > 1 ) {
4846 console . error ( `Multiple users found with email address ${ userEmail } , ending` ) ;
@@ -59,18 +57,15 @@ const validateUser = async (user: string, action: Action, step: Step): Promise<A
5957 } else if ( list . length == 0 ) {
6058 console . error ( `No user with email address ${ userEmail } found` ) ;
6159 } else {
62- username = list [ 0 ] . username ;
63- isUserAllowed = await isUserPushAllowed ( repoName , username ) ;
60+ isUserAllowed = await isUserPushAllowed ( repoName , list [ 0 ] . username ) ;
6461 }
6562
66- console . log ( `User ${ username } < ${ userEmail } > permission on Repo ${ repoName } : ${ isUserAllowed } ` ) ;
63+ console . log ( `User ${ userEmail } permission on Repo ${ repoName } : ${ isUserAllowed } ` ) ;
6764
6865 if ( ! isUserAllowed ) {
6966 console . log ( 'User not allowed to Push' ) ;
7067 step . error = true ;
71- step . log (
72- `User ${ username } <${ userEmail } > is not allowed to push on repo ${ action . repo } , ending` ,
73- ) ;
68+ step . log ( `User ${ userEmail } is not allowed to push on repo ${ action . repo } , ending` ) ;
7469
7570 step . setError (
7671 `Your push has been blocked (${ action . userEmail } ` +
@@ -81,7 +76,7 @@ const validateUser = async (user: string, action: Action, step: Step): Promise<A
8176 return action ;
8277 }
8378
84- step . log ( `User ${ username } < ${ userEmail } > is allowed to push on repo ${ action . repo } ` ) ;
79+ step . log ( `User ${ userEmail } is allowed to push on repo ${ action . repo } ` ) ;
8580 action . addStep ( step ) ;
8681 return action ;
8782} ;
0 commit comments