-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathcheckAuthorEmails.ts
More file actions
59 lines (45 loc) · 1.56 KB
/
checkAuthorEmails.ts
File metadata and controls
59 lines (45 loc) · 1.56 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
import { Action, Step } from '../../actions';
import { getCommitConfig } from '../../../config';
import { CommitData } from '../types';
import { isEmail } from 'validator';
const isEmailAllowed = (email: string): boolean => {
const commitConfig = getCommitConfig();
if (!email || !isEmail(email)) {
return false;
}
const [emailLocal, emailDomain] = email.split('@');
if (
commitConfig?.author?.email?.domain?.allow &&
!new RegExp(commitConfig.author.email.domain.allow, 'gi').test(emailDomain)
) {
return false;
}
if (
commitConfig?.author?.email?.local?.block &&
new RegExp(commitConfig.author.email.local.block, 'gi').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((commitData: CommitData) => commitData.authorEmail)),
];
const illegalEmails = uniqueAuthorEmails.filter((email) => !isEmailAllowed(email));
if (illegalEmails.length > 0) {
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;
}
step.log(`The following commit author e-mails are legal: ${uniqueAuthorEmails}`);
action.addStep(step);
return action;
};
exec.displayName = 'checkAuthorEmails.exec';
export { exec };