CLI-248 git hooks#160
CLI-248 git hooks#160kirill-knize-sonarsource merged 2 commits intotask/kk/CLI-244-245-callback-infrastructurefrom
Conversation
SummaryThis PR migrates git pre-commit hook logic from shell scripts into a new TypeScript handler ( What changed: The shell scripts ( Why: Decouples shell scripting from business logic. Makes the code type-safe, testable via unit tests with mocks, and easier to maintain. Graceful degradation (skipping when auth/binary unavailable) is clearer in TypeScript. Test changes: Added comprehensive unit tests for What reviewers should knowStart here:
Key decisions:
Non-obvious details:
Gotchas:
|
e5bc9f8 to
6f2f053
Compare
d2c374f to
6752b74
Compare
6f2f053 to
aa6bc64
Compare
6752b74 to
e69525f
Compare
e69525f to
45c3f51
Compare
45c3f51 to
d4de5d8
Compare
80c4a76 to
4853f25
Compare
d4de5d8 to
6123a4b
Compare
e2211ff to
581f8af
Compare
dc076f4 to
f266954
Compare
581f8af to
00bea22
Compare
| try { | ||
| const exitCode = await scanFiles(binaryPath, stagedFiles, auth); | ||
| if (exitCode === EXIT_CODE_SECRETS_FOUND) { | ||
| throw new CommandFailedError('Secrets detected in staged files'); | ||
| } | ||
| } catch (err) { | ||
| if (err instanceof CommandFailedError) throw err; | ||
| logger.debug(`git pre-commit secrets scan failed: ${(err as Error).message}`); | ||
| throw new CommandFailedError('Secrets scan failed'); | ||
| } |
There was a problem hiding this comment.
Logic duplication: This try-catch block — scanFiles → check EXIT_CODE_SECRETS_FOUND → re-throw as CommandFailedError, with a catch that re-throws CommandFailedError and wraps everything else — is duplicated verbatim in git-pre-push.ts lines 52–61. The only differences are the error message strings ('staged files' vs 'pushed commits') and the debug log prefix.
If the exit-code check or error-wrapping logic ever needs to change (e.g. new exit codes, changed error format, added telemetry), both files must be updated in sync.
Extract a shared helper into secrets-scan.ts (or a new run-secrets.ts), e.g.:
export async function runSecretsOrFail(
binaryPath: string,
files: string[],
auth: ResolvedAuth,
context: string,
): Promise<void> {
try {
const exitCode = await scanFiles(binaryPath, files, auth);
if (exitCode === EXIT_CODE_SECRETS_FOUND) {
throw new CommandFailedError(`Secrets detected in ${context}`);
}
} catch (err) {
if (err instanceof CommandFailedError) throw err;
logger.debug(`${context} secrets scan failed: ${(err as Error).message}`);
throw new CommandFailedError('Secrets scan failed');
}
}Then call await runSecretsOrFail(binaryPath, stagedFiles, auth, 'staged files') and await runSecretsOrFail(binaryPath, files, auth, 'pushed commits') respectively.
- Mark as noise
39242f7 to
4ef81c1
Compare
c4b4665 to
e44c9d3
Compare
9191141 to
129cc50
Compare
a7e0a09 to
384bf12
Compare
384bf12 to
658ffd5
Compare
658ffd5 to
0da8b82
Compare
0da8b82 to
b72e120
Compare
b72e120 to
69449ba
Compare
557751a to
5ca64ea
Compare
69449ba to
c38b117
Compare
Replaces the embedded shell logic in the pre-commit hook script with a thin launcher that delegates to `sonar hook git-pre-commit`. The TypeScript handler resolves auth and binary path, lists staged files via git, and calls the secrets binary directly.
c38b117 to
6a60055
Compare
| const result = await runSecretsBinary(binaryPath, stagedFiles, auth); | ||
| const exitCode = result.exitCode ?? 1; | ||
| if (exitCode === EXIT_CODE_SECRETS_FOUND) { | ||
| throw new CommandFailedError('Secrets detected in staged files'); |
There was a problem hiding this comment.
As a user I only see this generic message, maybe we could return more comprehensive error message that includes what caused the failure
| const result = await harness.run(''); | ||
| expect(result.exitCode).toBe(0); | ||
| expect(result.stdout).not.toContain('callback'); | ||
| // 'hook' must not appear as a listed command entry (indented under COMMANDS) |
There was a problem hiding this comment.
Maybe comment could be slightly more descriptive about why the regex pattern matters
eray-felek-sonarsource
left a comment
There was a problem hiding this comment.
Small comments, otherwise tested and LGTM
|
There was a problem hiding this comment.
LGTM! ✅
The previously flagged logic duplication between git-pre-commit.ts and git-pre-push.ts remains open, but git-pre-push.ts does not exist yet in this codebase — so there is no actual current duplication to fix. The concern is valid for when that handler is eventually added.
d7cffa2
into
task/kk/CLI-244-245-callback-infrastructure



No description provided.