Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cli/commands/hook/git-pre-push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// Replaces the shell logic that was previously embedded in the git hook script.

import { spawnProcess } from '../../../lib/process';
import { print } from '../../../ui';
import { CommandFailedError } from '../_common/error';
import { EXIT_CODE_SECRETS_FOUND, runSecretsBinary } from '../analyze/secrets';
import type { HookDependencies } from './hook-dependencies';
Expand Down Expand Up @@ -54,6 +55,8 @@ async function scanRef(ref: PushRef, emptyTree: string, deps: HookDependencies):
try {
const result = await runSecretsBinary(deps.binaryPath, files, deps.auth);
if ((result.exitCode ?? 1) === EXIT_CODE_SECRETS_FOUND) {
const output = [result.stderr, result.stdout].filter(Boolean).join('\n');
if (output) print(output);
throw new CommandFailedError('Secrets detected in pushed commits.', {
remediationHint:
'Remove the reported secret, amend the commit if needed, then retry the push.',
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/specs/hook/hook-git-pre-push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ describe('sonar hook git-pre-push', () => {
});

expect(result.exitCode).toBe(1);
expect(result.output).toContain('secret.js');
},
{ timeout: 30000 },
);
Expand Down Expand Up @@ -189,6 +190,7 @@ describe('sonar hook git-pre-push', () => {
});

expect(result.exitCode).toBe(1);
expect(result.output).toContain('secret.js');
},
{ timeout: 30000 },
);
Expand Down