Skip to content

Commit aa2993a

Browse files
committed
refactor(ts): refactor check processors to ts
1 parent f7c3cda commit aa2993a

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed

src/proxy/processors/push-action/checkAuthorEmails.js renamed to src/proxy/processors/push-action/checkAuthorEmails.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
const Step = require('../../actions').Step;
2-
const config = require('../../../config');
1+
import { Action, Step } from '../../actions';
2+
import { getCommitConfig } from '../../../config';
3+
import { Commit } from '../../actions/Action';
34

4-
const commitConfig = config.getCommitConfig();
5+
const commitConfig = getCommitConfig();
56

6-
function isEmailAllowed(email) {
7+
const isEmailAllowed = (email: string): boolean => {
78
const [emailLocal, emailDomain] = email.split('@');
89
console.log({ emailLocal, emailDomain });
910

@@ -28,13 +29,12 @@ function isEmailAllowed(email) {
2829
return true;
2930
}
3031

31-
// Execute if the repo is approved
32-
const exec = async (req, action) => {
32+
const exec = async (req: any, action: Action): Promise<Action> => {
3333
console.log({ req, action });
3434

3535
const step = new Step('checkAuthorEmails');
3636

37-
const uniqueAuthorEmails = [...new Set(action.commitData.map((commit) => commit.authorEmail))];
37+
const uniqueAuthorEmails = [...new Set(action.commitData?.map((commit: Commit) => commit.authorEmail))];
3838
console.log({ uniqueAuthorEmails });
3939

4040
const illegalEmails = uniqueAuthorEmails.filter((email) => !isEmailAllowed(email));
@@ -62,4 +62,5 @@ const exec = async (req, action) => {
6262
};
6363

6464
exec.displayName = 'checkAuthorEmails.exec';
65-
exports.exec = exec;
65+
66+
export { exec };

src/proxy/processors/push-action/checkCommitMessages.js renamed to src/proxy/processors/push-action/checkCommitMessages.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const Step = require('../../actions').Step;
2-
const config = require('../../../config');
1+
import { Action, Step } from '../../actions';
2+
import { getCommitConfig } from '../../../config';
33

4-
const commitConfig = config.getCommitConfig();
4+
const commitConfig = getCommitConfig();
55

6-
function isMessageAllowed(commitMessage) {
6+
const isMessageAllowed = (commitMessage: string): boolean => {
77
console.log(`isMessageAllowed(${commitMessage})`);
88

99
// Commit message is empty, i.e. '', null or undefined
@@ -19,18 +19,18 @@ function isMessageAllowed(commitMessage) {
1919
}
2020

2121
// Configured blocked literals
22-
const blockedLiterals = commitConfig.message.block.literals;
22+
const blockedLiterals: string[] = commitConfig.message.block.literals;
2323

2424
// Configured blocked patterns
25-
const blockedPatterns = commitConfig.message.block.patterns;
25+
const blockedPatterns: string[] = commitConfig.message.block.patterns;
2626

2727
// Find all instances of blocked literals in commit message...
28-
const positiveLiterals = blockedLiterals.map((literal) =>
28+
const positiveLiterals = blockedLiterals.map((literal: string) =>
2929
commitMessage.toLowerCase().includes(literal.toLowerCase()),
3030
);
3131

3232
// Find all instances of blocked patterns in commit message...
33-
const positivePatterns = blockedPatterns.map((pattern) =>
33+
const positivePatterns = blockedPatterns.map((pattern: string) =>
3434
commitMessage.match(new RegExp(pattern, 'gi')),
3535
);
3636

@@ -50,12 +50,12 @@ function isMessageAllowed(commitMessage) {
5050
}
5151

5252
// Execute if the repo is approved
53-
const exec = async (req, action) => {
53+
const exec = async (req: any, action: Action): Promise<Action> => {
5454
console.log({ req, action });
5555

5656
const step = new Step('checkCommitMessages');
5757

58-
const uniqueCommitMessages = [...new Set(action.commitData.map((commit) => commit.message))];
58+
const uniqueCommitMessages = [...new Set(action.commitData?.map((commit) => commit.message))];
5959
console.log({ uniqueCommitMessages });
6060

6161
const illegalMessages = uniqueCommitMessages.filter((message) => !isMessageAllowed(message));
@@ -83,4 +83,5 @@ const exec = async (req, action) => {
8383
};
8484

8585
exec.displayName = 'checkCommitMessages.exec';
86-
exports.exec = exec;
86+
87+
export { exec };
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
const Step = require('../../actions').Step;
2-
const data = require('../../../db');
1+
import { Action, Step } from '../../actions';
2+
import { getPush } from '../../../db';
33

44
// Execute function
5-
const exec = async (req, action) => {
5+
const exec = async (req: any, action: Action): Promise<Action> => {
66
const step = new Step('checkIfWaitingAuth');
77
try {
8-
const existingAction = await data.getPush(action.id);
8+
const existingAction = await getPush(action.id);
99
if (existingAction) {
1010
if (!action.error) {
1111
if (existingAction.authorised) {
@@ -14,7 +14,7 @@ const exec = async (req, action) => {
1414
}
1515
}
1616
}
17-
} catch (e) {
17+
} catch (e: any) {
1818
step.setError(e.toString('utf-8'));
1919
throw e;
2020
} finally {
@@ -24,4 +24,5 @@ const exec = async (req, action) => {
2424
};
2525

2626
exec.displayName = 'checkIfWaitingAuth.exec';
27-
exports.exec = exec;
27+
28+
export { exec };

src/proxy/processors/push-action/checkRepoInAuthorisedList.js renamed to src/proxy/processors/push-action/checkRepoInAuthorisedList.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
const Step = require('../../actions').Step;
2-
const db = require('../../../db');
1+
import { Action, Step } from '../../actions';
2+
import { getRepos } from '../../../db';
3+
import { Repo } from '../../../db/types';
34

45
// Execute if the repo is approved
5-
const exec = async (req, action, authorisedList = db.getRepos) => {
6+
const exec = async (
7+
req: any,
8+
action: Action,
9+
authorisedList: () => Promise<Repo[]> = getRepos,
10+
): Promise<Action> => {
611
const step = new Step('checkRepoInAuthorisedList');
712

813
const list = await authorisedList();
914
console.log(list);
1015

11-
const found = list.find((x) => {
16+
const found = list.find((x: Repo) => {
1217
const targetName = action.repo.replace('.git', '').toLowerCase();
1318
const allowedName = `${x.project}/${x.name}`.replace('.git', '').toLowerCase();
1419
console.log(`${targetName} = ${allowedName}`);
@@ -34,4 +39,5 @@ const exec = async (req, action, authorisedList = db.getRepos) => {
3439
};
3540

3641
exec.displayName = 'checkRepoInAuthorisedList.exec';
37-
exports.exec = exec;
42+
43+
export { exec };

src/proxy/processors/push-action/checkUserPushPermission.js renamed to src/proxy/processors/push-action/checkUserPushPermission.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
const Step = require('../../actions').Step;
2-
const db = require('../../../db');
1+
import { Action, Step } from '../../actions';
2+
import { getUsers, isUserPushAllowed } from '../../../db';
33

44
// Execute if the repo is approved
5-
const exec = async (req, action) => {
5+
const exec = async (req: any, action: Action): Promise<Action> => {
66
const step = new Step('checkUserPushPermission');
77

88
const repoName = action.repo.split('/')[1].replace('.git', '');
99
let isUserAllowed = false;
1010
let user = action.user;
1111

1212
// Find the user associated with this Git Account
13-
const list = await db.getUsers({ gitAccount: action.user });
13+
const list = await getUsers({ gitAccount: action.user });
1414

1515
console.log(JSON.stringify(list));
1616

1717
if (list.length == 1) {
1818
user = list[0].username;
19-
isUserAllowed = await db.isUserPushAllowed(repoName, user);
19+
isUserAllowed = await isUserPushAllowed(repoName, user);
2020
}
2121

2222
console.log(`User ${user} permission on Repo ${repoName} : ${isUserAllowed}`);
@@ -30,8 +30,8 @@ const exec = async (req, action) => {
3030

3131
step.setError(
3232
`Rejecting push as user ${action.user} ` +
33-
`is not allowed to push on repo ` +
34-
`${action.repo}`,
33+
`is not allowed to push on repo ` +
34+
`${action.repo}`,
3535
);
3636
action.addStep(step);
3737
return action;
@@ -43,4 +43,5 @@ const exec = async (req, action) => {
4343
};
4444

4545
exec.displayName = 'checkUserPushPermission.exec';
46-
exports.exec = exec;
46+
47+
export { exec };

0 commit comments

Comments
 (0)