forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchain.js
More file actions
111 lines (101 loc) · 2.96 KB
/
chain.js
File metadata and controls
111 lines (101 loc) · 2.96 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
const proc = require('./processors');
const { attemptAutoApproval, attemptAutoRejection } = require('./actions/autoActions');
const pushActionChain = [
proc.push.parsePush,
proc.push.checkRepoInAuthorisedList,
proc.push.checkCommitMessages,
proc.push.checkAuthorEmails,
proc.push.checkUserPushPermission,
proc.push.checkIfWaitingAuth,
proc.push.pullRemote,
proc.push.writePack,
proc.push.preReceive,
proc.push.getDiff,
proc.push.clearBareClone,
proc.push.scanDiff,
proc.push.blockForAuth,
];
const pullActionChain = [proc.push.checkRepoInAuthorisedList];
let pluginsInserted = false;
const executeChain = async (req) => {
let action;
try {
action = await proc.pre.parseAction(req);
const actions = await getChain(action);
for (const i in actions) {
if (!i) continue;
const fn = actions[i];
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;
const getChain = async (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;
}
if (action.type === 'default') return [];
};
module.exports = {
set chainPluginLoader(loader) {
chainPluginLoader = loader;
},
get chainPluginLoader() {
return chainPluginLoader;
},
get pluginsInserted() {
return pluginsInserted;
},
get pushActionChain() {
return pushActionChain;
},
get pullActionChain() {
return pullActionChain;
},
executeChain,
getChain,
};