-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathcheckUserPushPermission.ts
More file actions
74 lines (60 loc) · 2.41 KB
/
checkUserPushPermission.ts
File metadata and controls
74 lines (60 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Action, Step } from '../../actions';
import { getUsers, isUserPushAllowed } from '../../../db';
import { trimTrailingDotGit } from '../../../db/helper';
// Execute if the repo is approved
const exec = async (req: any, action: Action): Promise<Action> => {
const step = new Step('checkUserPushPermission');
const user = action.user;
if (!user) {
console.log('Action has no user set. This may be due to a fast-forward ref update. Deferring to getMissingData action.');
return action;
}
return await validateUser(user, action, step);
};
/**
* Helper that validates the user's push permission.
* This can be used by other actions that need it. For example, when the user is missing from the commit data,
* validation is deferred to getMissingData, but the logic is the same.
* @param {string} user The user to validate
* @param {Action} action The action object
* @param {Step} step The step object
* @return {Promise<Action>} The action object
*/
const validateUser = async (user: string, action: Action, step: Step): Promise<Action> => {
const repoSplit = trimTrailingDotGit(action.repo.toLowerCase()).split('/');
// we expect there to be exactly one / separating org/repoName
if (repoSplit.length != 2) {
step.setError('Server-side issue extracting repoName');
action.addStep(step);
return action;
}
// pull the 2nd value of the split for repoName
const repoName = repoSplit[1];
let isUserAllowed = false;
// Find the user associated with this Git Account
const list = await getUsers({ gitAccount: action.user });
console.log(`Users for this git account: ${JSON.stringify(list)}`);
if (list.length == 1) {
user = list[0].username;
isUserAllowed = await isUserPushAllowed(repoName, user);
}
console.log(`User ${user} permission on Repo ${repoName} : ${isUserAllowed}`);
if (!isUserAllowed) {
console.log('User not allowed to Push');
step.error = true;
step.log(`User ${user} is not allowed to push on repo ${action.repo}, ending`);
console.log('setting error');
step.setError(
`Rejecting push as user ${action.user} ` +
`is not allowed to push on repo ` +
`${action.repo}`,
);
action.addStep(step);
return action;
}
step.log(`User ${user} is allowed to push on repo ${action.repo}`);
action.addStep(step);
return action;
};
exec.displayName = 'checkUserPushPermission.exec';
export { exec, validateUser };