-
Notifications
You must be signed in to change notification settings - Fork 155
feat: Add support for executing pre-receive hooks #908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1422dc5
feat: wip for pre-receive hooks implementation
fabiovincenzi 1233039
fix: run pre-hook in repository folder
fabiovincenzi b6953fe
fix: change position of preReceive in the chain
fabiovincenzi d2dc9a9
test: update chain test to work with preReceive
fabiovincenzi eb78943
test: add basic tests for preReceive
fabiovincenzi ec51f00
chore: remove unused declarations
fabiovincenzi 939bca4
test: include sub directories in test script
fabiovincenzi 5adb1e4
Merge branch 'main' of https://github.com/finos/git-proxy into pre-re…
fabiovincenzi c9d463f
Merge branch 'main' into pre-receive
JamieSlome 81c40b3
Merge branch 'main' into pre-receive
fabiovincenzi 8627635
chore(deps): remove mocha update
fabiovincenzi f87763f
feat: increase test coverage for failing status in pre-receive hook
fabiovincenzi 48f49e0
chore(deps): remove mocha update
fabiovincenzi 54970fb
fix: skipping pre-receive if hook file doesn't exist
fabiovincenzi 0808aab
Merge branch 'main' into pre-receive
fabiovincenzi 1e21b09
Merge branch 'main' into pre-receive
fabiovincenzi 46306d1
Merge branch 'main' into pre-receive
fabiovincenzi 435cb63
Merge branch 'main' into pre-receive
fabiovincenzi 774a55e
Merge branch 'main' into pre-receive
JamieSlome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const Step = require('../../actions').Step; | ||
| const { spawnSync } = require('child_process'); | ||
|
|
||
| const sanitizeInput = (_req, action) => { | ||
| return `${action.commitFrom} ${action.commitTo} ${action.branch} \n`; | ||
| }; | ||
|
|
||
| const exec = async (req, action, hookFilePath = './hooks/pre-receive.sh') => { | ||
| const step = new Step('executeExternalPreReceiveHook'); | ||
|
|
||
| try { | ||
| const resolvedPath = path.resolve(hookFilePath); | ||
| const hookDir = path.dirname(resolvedPath); | ||
|
|
||
| if (!fs.existsSync(hookDir) || !fs.existsSync(resolvedPath)) { | ||
| step.log('Pre-receive hook not found, skipping execution.'); | ||
| action.addStep(step); | ||
| return action; | ||
| } | ||
|
|
||
| const repoPath = `${action.proxyGitPath}/${action.repoName}`; | ||
|
|
||
| step.log(`Executing pre-receive hook from: ${resolvedPath}`); | ||
|
|
||
| const sanitizedInput = sanitizeInput(req, action); | ||
|
|
||
| const hookProcess = spawnSync(resolvedPath, [], { | ||
| input: sanitizedInput, | ||
| encoding: 'utf-8', | ||
| cwd: repoPath, | ||
| }); | ||
|
|
||
| const { stdout, stderr, status } = hookProcess; | ||
|
|
||
| const stderrTrimmed = stderr ? stderr.trim() : ''; | ||
| const stdoutTrimmed = stdout ? stdout.trim() : ''; | ||
|
|
||
| if (status !== 0) { | ||
| step.error = true; | ||
| step.log(`Hook stderr: ${stderrTrimmed}`); | ||
| step.setError(stdoutTrimmed); | ||
| action.addStep(step); | ||
| return action; | ||
| } | ||
|
|
||
| step.log('Pre-receive hook executed successfully'); | ||
| action.addStep(step); | ||
| return action; | ||
| } catch (error) { | ||
| step.error = true; | ||
| step.setError(`Hook execution error: ${error.message}`); | ||
| action.addStep(step); | ||
| return action; | ||
| } | ||
| }; | ||
|
|
||
| exec.displayName = 'executeExternalPreReceiveHook.exec'; | ||
| exports.exec = exec; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Mock repository. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #!/bin/bash | ||
| while read oldrev newrev refname; do | ||
| echo "Push allowed to $refname" | ||
| done | ||
| exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #!/bin/bash | ||
| while read oldrev newrev refname; do | ||
| echo "Push rejected to $refname" | ||
| done | ||
| exit 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| const { expect } = require('chai'); | ||
| const sinon = require('sinon'); | ||
| const path = require('path'); | ||
| const { exec } = require('../../src/proxy/processors/push-action/preReceive'); | ||
|
|
||
| describe('Pre-Receive Hook Execution', function () { | ||
| let action; | ||
| let req; | ||
|
|
||
| beforeEach(() => { | ||
| req = {}; | ||
| action = { | ||
| steps: [], | ||
| commitFrom: 'oldCommitHash', | ||
| commitTo: 'newCommitHash', | ||
| branch: 'feature-branch', | ||
| proxyGitPath: 'test/preReceive/mock/repo', | ||
| repoName: 'test-repo', | ||
| addStep: function (step) { | ||
| this.steps.push(step); | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it('should execute hook successfully', async () => { | ||
| const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-allow.sh'); | ||
|
|
||
| const result = await exec(req, action, scriptPath); | ||
|
|
||
| expect(result.steps).to.have.lengthOf(1); | ||
| expect(result.steps[0].error).to.be.false; | ||
| expect( | ||
| result.steps[0].logs.some((log) => log.includes('Pre-receive hook executed successfully')), | ||
| ).to.be.true; | ||
| }); | ||
|
|
||
| it('should skip execution when hook file does not exist', async () => { | ||
| const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/missing-hook.sh'); | ||
|
|
||
| const result = await exec(req, action, scriptPath); | ||
|
|
||
| expect(result.steps).to.have.lengthOf(1); | ||
| expect(result.steps[0].error).to.be.false; | ||
| expect( | ||
| result.steps[0].logs.some((log) => | ||
| log.includes('Pre-receive hook not found, skipping execution.'), | ||
| ), | ||
| ).to.be.true; | ||
| }); | ||
|
|
||
| it('should skip execution when hook directory does not exist', async () => { | ||
| const scriptPath = path.resolve(__dirname, 'non-existent-directory/pre-receive.sh'); | ||
|
|
||
| const result = await exec(req, action, scriptPath); | ||
|
|
||
| expect(result.steps).to.have.lengthOf(1); | ||
| expect(result.steps[0].error).to.be.false; | ||
| expect( | ||
| result.steps[0].logs.some((log) => | ||
| log.includes('Pre-receive hook not found, skipping execution.'), | ||
| ), | ||
| ).to.be.true; | ||
| }); | ||
|
|
||
| it('should fail when hook execution returns an error', async () => { | ||
| const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-reject.sh'); | ||
|
|
||
| const result = await exec(req, action, scriptPath); | ||
|
|
||
| expect(result.steps).to.have.lengthOf(1); | ||
|
|
||
| const step = result.steps[0]; | ||
|
|
||
| expect(step.error).to.be.true; | ||
| expect(step.logs.some((log) => log.includes('Hook stderr:'))).to.be.true; | ||
|
|
||
| expect(step.errorMessage).to.exist; | ||
|
|
||
| expect(action.steps).to.deep.include(step); | ||
| }); | ||
|
|
||
| it('should catch and handle unexpected errors', async () => { | ||
| const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-allow.sh'); | ||
|
|
||
| sinon.stub(require('fs'), 'existsSync').throws(new Error('Unexpected FS error')); | ||
|
|
||
| const result = await exec(req, action, scriptPath); | ||
|
|
||
| expect(result.steps).to.have.lengthOf(1); | ||
| expect(result.steps[0].error).to.be.true; | ||
| expect( | ||
| result.steps[0].logs.some((log) => log.includes('Hook execution error: Unexpected FS error')), | ||
| ).to.be.true; | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.