Skip to content

Commit 54970fb

Browse files
committed
fix: skipping pre-receive if hook file doesn't exist
1 parent 48f49e0 commit 54970fb

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ const exec = async (req, action, hookFilePath = './hooks/pre-receive.sh') => {
1212

1313
try {
1414
const resolvedPath = path.resolve(hookFilePath);
15+
const hookDir = path.dirname(resolvedPath);
1516

16-
if (!fs.existsSync(resolvedPath)) {
17-
throw new Error(`Hook file not found: ${resolvedPath}`);
17+
if (!fs.existsSync(hookDir) || !fs.existsSync(resolvedPath)) {
18+
step.log('Pre-receive hook not found, skipping execution.');
19+
action.addStep(step);
20+
return action;
1821
}
1922

2023
const repoPath = `${action.proxyGitPath}/${action.repoName}`;

test/preReceive/preReceive.test.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,31 @@ describe('Pre-Receive Hook Execution', function () {
3838
).to.be.true;
3939
});
4040

41-
it('should fail when hook file does not exist', async () => {
41+
it('should skip execution when hook file does not exist', async () => {
4242
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/missing-hook.sh');
4343

4444
const result = await exec(req, action, scriptPath);
4545

4646
expect(result.steps).to.have.lengthOf(1);
47-
expect(result.steps[0].error).to.be.true;
47+
expect(result.steps[0].error).to.be.false;
48+
expect(
49+
result.steps[0].logs.some((log) =>
50+
log.includes('Pre-receive hook not found, skipping execution.'),
51+
),
52+
).to.be.true;
53+
});
54+
55+
it('should skip execution when hook directory does not exist', async () => {
56+
const scriptPath = path.resolve(__dirname, 'non-existent-directory/pre-receive.sh');
57+
58+
const result = await exec(req, action, scriptPath);
59+
60+
expect(result.steps).to.have.lengthOf(1);
61+
expect(result.steps[0].error).to.be.false;
4862
expect(
49-
result.steps[0].logs.some((log) => log.includes('Hook execution error: Hook file not found')),
63+
result.steps[0].logs.some((log) =>
64+
log.includes('Pre-receive hook not found, skipping execution.'),
65+
),
5066
).to.be.true;
5167
});
5268

@@ -66,4 +82,18 @@ describe('Pre-Receive Hook Execution', function () {
6682

6783
expect(action.steps).to.deep.include(step);
6884
});
85+
86+
it('should catch and handle unexpected errors', async () => {
87+
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-allow.sh');
88+
89+
sinon.stub(require('fs'), 'existsSync').throws(new Error('Unexpected FS error'));
90+
91+
const result = await exec(req, action, scriptPath);
92+
93+
expect(result.steps).to.have.lengthOf(1);
94+
expect(result.steps[0].error).to.be.true;
95+
expect(
96+
result.steps[0].logs.some((log) => log.includes('Hook execution error: Unexpected FS error')),
97+
).to.be.true;
98+
});
6999
});

0 commit comments

Comments
 (0)