Skip to content

Commit 1422dc5

Browse files
committed
feat: wip for pre-receive hooks implementation
1 parent 94c866c commit 1422dc5

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

src/proxy/chain.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const proc = require('./processors');
22

33
const pushActionChain = [
4+
proc.push.preReceive,
45
proc.push.parsePush,
56
proc.push.checkRepoInAuthorisedList,
67
proc.push.checkCommitMessages,

src/proxy/processors/push-action/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
exports.parsePush = require('./parsePush').exec;
2+
exports.preReceive = require('./preReceive').exec;
23
exports.checkRepoInAuthorisedList = require('./checkRepoInAuthorisedList').exec;
34
exports.audit = require('./audit').exec;
45
exports.pullRemote = require('./pullRemote').exec;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const Step = require('../../actions').Step;
4+
const { spawnSync } = require('child_process');
5+
6+
const sanitizeInput = (req, action) => {
7+
return `${action.oldCommit} ${action.newCommit} ${action.ref}\n`;
8+
};
9+
10+
const exec = async (req, action, hookFilePath = './hooks/pre-receive.sh') => {
11+
const step = new Step('executeExternalPreReceiveHook');
12+
13+
try {
14+
const resolvedPath = path.resolve(hookFilePath);
15+
16+
if (!fs.existsSync(resolvedPath)) {
17+
throw new Error(`Hook file not found: ${resolvedPath}`);
18+
}
19+
20+
console.log(`Executing pre-receive hook from: ${resolvedPath}`);
21+
22+
const sanitizedInput = sanitizeInput(req, action);
23+
24+
const hookProcess = spawnSync(resolvedPath, [], {
25+
input: JSON.stringify(sanitizedInput),
26+
encoding: 'utf-8',
27+
});
28+
29+
const { stdout, stderr, status } = hookProcess;
30+
31+
if (status !== 0) {
32+
console.error(`Pre-receive hook failed with exit code ${status}`);
33+
step.error = true;
34+
step.log(`Hook stderr: ${stderr.trim()}`);
35+
step.setError(stdout.trim());
36+
action.addStep(step);
37+
return action;
38+
}
39+
40+
console.log('Pre-receive hook executed successfully');
41+
console.log(`Hook stdout: ${stdout.trim()}`);
42+
step.log('Pre-receive hook executed successfully');
43+
action.addStep(step);
44+
return action;
45+
} catch (error) {
46+
console.error('Error during pre-receive hook execution:', error);
47+
step.error = true;
48+
step.setError(`Hook execution error: ${error.message}`);
49+
action.addStep(step);
50+
return action;
51+
}
52+
};
53+
54+
exec.displayName = 'executeExternalPreReceiveHook.exec';
55+
exports.exec = exec;

0 commit comments

Comments
 (0)