forked from G-Research-Forks/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.ts
More file actions
115 lines (105 loc) · 3.3 KB
/
chain.ts
File metadata and controls
115 lines (105 loc) · 3.3 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { PluginLoader } from '../plugin';
import { Action } from './actions';
import * as proc from './processors';
import { attemptAutoApproval, attemptAutoRejection } from './actions/autoActions';
const pushActionChain: ((req: any, action: Action) => Promise<Action>)[] = [
proc.push.parsePush,
proc.push.checkEmptyBranch,
proc.push.checkRepoInAuthorisedList,
proc.push.checkCommitMessages,
proc.push.checkAuthorEmails,
proc.push.checkUserPushPermission,
proc.push.pullRemote,
proc.push.writePack,
proc.push.checkHiddenCommits,
proc.push.checkIfWaitingAuth,
proc.push.preReceive,
proc.push.getDiff,
// run before clear remote
proc.push.gitleaks,
proc.push.clearBareClone,
proc.push.scanDiff,
proc.push.blockForAuth,
];
const pullActionChain: ((req: any, action: Action) => Promise<Action>)[] = [
proc.push.checkRepoInAuthorisedList,
];
let pluginsInserted = false;
export const executeChain = async (req: any, res: any): Promise<Action> => {
let action: Action = {} as Action;
try {
action = await proc.pre.parseAction(req);
const actionFns = await getChain(action);
for (const fn of actionFns) {
action = await fn(req, action);
if (!action.continue()) {
return action;
}
if (action.allowPush) {
return action;
}
}
} finally {
await proc.push.audit(req, action);
if (action.autoApproved) {
attemptAutoApproval(action);
} else if (action.autoRejected) {
attemptAutoRejection(action);
}
}
return action;
};
/**
* The plugin loader used for the GitProxy chain.
* @type {import('../plugin').PluginLoader}
*/
let chainPluginLoader: PluginLoader;
export const getChain = async (
action: Action,
): Promise<((req: any, action: Action) => Promise<Action>)[]> => {
if (chainPluginLoader === undefined) {
console.error(
'Plugin loader was not initialized! This is an application error. Please report it to the GitProxy maintainers. Skipping plugins...',
);
pluginsInserted = true;
}
if (!pluginsInserted) {
console.log(
`Inserting loaded plugins (${chainPluginLoader.pushPlugins.length} push, ${chainPluginLoader.pullPlugins.length} pull) into proxy chains`,
);
for (const pluginObj of chainPluginLoader.pushPlugins) {
console.log(`Inserting push plugin ${pluginObj.constructor.name} into chain`);
// insert custom functions after parsePush but before other actions
pushActionChain.splice(1, 0, pluginObj.exec);
}
for (const pluginObj of chainPluginLoader.pullPlugins) {
console.log(`Inserting pull plugin ${pluginObj.constructor.name} into chain`);
// insert custom functions before other pull actions
pullActionChain.splice(0, 0, pluginObj.exec);
}
// This is set to true so that we don't re-insert the plugins into the chain
pluginsInserted = true;
}
if (action.type === 'pull') return pullActionChain;
if (action.type === 'push') return pushActionChain;
return [];
};
export default {
set chainPluginLoader(loader) {
chainPluginLoader = loader;
},
get chainPluginLoader() {
return chainPluginLoader;
},
get pluginsInserted() {
return pluginsInserted;
},
get pushActionChain() {
return pushActionChain;
},
get pullActionChain() {
return pullActionChain;
},
executeChain,
getChain,
};