Skip to content

Commit 828f7ac

Browse files
committed
chore: apply AI review suggestions for getGitCommitMessage error handling
- Use ignoreReturnCode: true in getExecOutput call and check exitCode explicitly, incorporating stderr into the thrown error for better diagnostics (copilot suggestion) - Add try/catch around getExecOutput call to wrap thrown errors with descriptive message (covers the throw-path for gemini/copilot suggestions) - Add tests for error path where getExecOutput throws (gemini suggestion) - Add tests for error path where getExecOutput returns non-zero exit code (copilot suggestion)
1 parent 41dcdba commit 828f7ac

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/__tests__/index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,41 @@ describe('gitHub Action Integration', () => {
157157
expect(core.error).toBeDefined()
158158
})
159159
})
160+
161+
describe('getGitCommitMessage error paths', () => {
162+
it('surfaces a descriptive error when getExecOutput throws', async () => {
163+
vi.mocked(exec.getExecOutput).mockRejectedValueOnce(
164+
new Error('spawn git ENOENT'),
165+
)
166+
167+
vi.resetModules()
168+
await import('../index')
169+
170+
// Allow the async run() to settle
171+
await new Promise(resolve => setTimeout(resolve, 0))
172+
173+
expect(vi.mocked(core.setFailed)).toHaveBeenCalledWith(
174+
expect.stringContaining('Failed to retrieve git commit message'),
175+
)
176+
})
177+
178+
it('surfaces a descriptive error when getExecOutput returns non-zero exit code', async () => {
179+
vi.mocked(exec.getExecOutput).mockResolvedValueOnce({
180+
exitCode: 128,
181+
stdout: '',
182+
stderr: 'fatal: not a git repository',
183+
})
184+
185+
vi.resetModules()
186+
await import('../index')
187+
188+
await new Promise(resolve => setTimeout(resolve, 0))
189+
190+
expect(vi.mocked(core.setFailed)).toHaveBeenCalledWith(
191+
expect.stringContaining('Failed to retrieve git commit message'),
192+
)
193+
})
194+
})
160195
})
161196

162197
describe('octokit API v6 compatibility', () => {

src/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@ import { aliasDomainsToDeployment, createVercelClient, vercelDeploy, vercelInspe
1010
const { context } = github
1111

1212
async function getGitCommitMessage(): Promise<string> {
13+
let result: Awaited<ReturnType<typeof exec.getExecOutput>>
14+
1315
try {
14-
const { stdout } = await exec.getExecOutput('git', ['log', '-1', '--pretty=format:%B'], { silent: true })
15-
return stdout.trim()
16+
result = await exec.getExecOutput(
17+
'git',
18+
['log', '-1', '--pretty=format:%B'],
19+
{ silent: true, ignoreReturnCode: true },
20+
)
1621
}
1722
catch (error) {
1823
const message = error instanceof Error ? error.message : String(error)
@@ -21,6 +26,18 @@ async function getGitCommitMessage(): Promise<string> {
2126
+ 'Ensure this action runs in a git repository with at least one commit.',
2227
)
2328
}
29+
30+
if (result.exitCode !== 0) {
31+
const detail = result.stderr.trim()
32+
|| `git exited with code ${result.exitCode}`
33+
34+
throw new Error(
35+
`Failed to retrieve git commit message: ${detail}. `
36+
+ 'Ensure this action runs in a git repository with at least one commit.',
37+
)
38+
}
39+
40+
return result.stdout.trim()
2441
}
2542

2643
function logContextDebug(): void {

0 commit comments

Comments
 (0)