forked from G-Research-Forks/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckAuthorEmails.ts
More file actions
66 lines (50 loc) · 1.79 KB
/
checkAuthorEmails.ts
File metadata and controls
66 lines (50 loc) · 1.79 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
import { Action, Step } from '../../actions';
import { getCommitConfig } from '../../../config';
import { Commit } from '../../actions/Action';
import { isEmail } from 'validator';
const commitConfig = getCommitConfig();
const isEmailAllowed = (email: string): boolean => {
if (!email || !isEmail(email)) {
return false;
}
const [emailLocal, emailDomain] = email.split('@');
if (
commitConfig.author.email.domain.allow &&
!new RegExp(commitConfig.author.email.domain.allow, 'g').test(emailDomain)
) {
return false;
}
if (
commitConfig.author.email.local.block &&
new RegExp(commitConfig.author.email.local.block, 'g').test(emailLocal)
) {
return false;
}
return true;
};
const exec = async (req: any, action: Action): Promise<Action> => {
const step = new Step('checkAuthorEmails');
const uniqueAuthorEmails = [
...new Set(action.commitData?.map((commit: Commit) => commit.authorEmail)),
];
console.log({ uniqueAuthorEmails });
const illegalEmails = uniqueAuthorEmails.filter((email) => !isEmailAllowed(email));
console.log({ illegalEmails });
const usingIllegalEmails = illegalEmails.length > 0;
console.log({ usingIllegalEmails });
if (usingIllegalEmails) {
console.log(`The following commit author e-mails are illegal: ${illegalEmails}`);
step.error = true;
step.log(`The following commit author e-mails are illegal: ${illegalEmails}`);
step.setError(
'Your push has been blocked. Please verify your Git configured e-mail address is valid (e.g. john.smith@example.com)',
);
action.addStep(step);
return action;
}
console.log(`The following commit author e-mails are legal: ${uniqueAuthorEmails}`);
action.addStep(step);
return action;
};
exec.displayName = 'checkAuthorEmails.exec';
export { exec };