fix(git-server): capture and log hook output on failures
swadhinbiswas committed 8 months ago
9dd7289
1 parent 0fdca41
commit 9dd7289
1 file changed
+24 -2
Showing 1 changed file+24-2
▼src/lib/git-server.ts
+24-2
| @@ -266,13 +266,35 @@ async function executeHook(repoPath: string, hookName: string, args: string[], e | ||
266 | 266 | const child = spawn(hookPath, [], { |
267 | 267 | cwd: repoPath, |
268 | 268 | env: { ...process.env, ...env }, |
269 | - stdio: ["pipe", "ignore", "ignore"] // We don't care about output for now | |
269 | + stdio: ["pipe", "pipe", "pipe"] | |
270 | + }); | |
271 | + let stdout = ""; | |
272 | + let stderr = ""; | |
273 | + | |
274 | + child.stdout.on("data", (chunk) => { | |
275 | + stdout += chunk.toString(); | |
276 | + }); | |
277 | + child.stderr.on("data", (chunk) => { | |
278 | + stderr += chunk.toString(); | |
270 | 279 | }); |
271 | 280 | |
272 | 281 | child.stdin.write(input); |
273 | 282 | child.stdin.end(); |
274 | 283 | |
275 | - child.on("close", () => resolve()); | |
284 | + child.on("close", (code) => { | |
285 | + if (code !== 0) { | |
286 | + logger.warn( | |
287 | + { | |
288 | + hook: hookName, | |
289 | + exitCode: code, | |
290 | + stdout: stdout.slice(0, 1000), | |
291 | + stderr: stderr.slice(0, 1000), | |
292 | + }, | |
293 | + "Hook exited with non-zero status" | |
294 | + ); | |
295 | + } | |
296 | + resolve(); | |
297 | + }); | |
276 | 298 | child.on("error", (err) => { |
277 | 299 | logger.error({ err, hook: hookName }, "Failed to execute hook"); |
278 | 300 | resolve(); |
279 | 301 | |