80 lines
2.4 KiB
Bash
Executable file
80 lines
2.4 KiB
Bash
Executable file
#!/bin/sh
|
|
# prepare-commit-msg: append build + test status to every commit message.
|
|
# Skipped for merge, squash, and rebase commits.
|
|
|
|
COMMIT_MSG_FILE="$1"
|
|
COMMIT_SOURCE="${2:-}"
|
|
|
|
case "$COMMIT_SOURCE" in
|
|
merge|squash) exit 0 ;;
|
|
esac
|
|
|
|
BUILD_STATUS="pass"
|
|
TEST_STATUS="pass"
|
|
TEST_DETAIL=""
|
|
|
|
STATUS_DIR=${AGENT_STATUS_DIR:-${CLAWDIE_VAR_DIR:-"$(pwd)/tmp/status"}}
|
|
BUILD_PATH="$STATUS_DIR/build-status.json"
|
|
TEST_PATH="$STATUS_DIR/test-status.json"
|
|
|
|
sh scripts/write-test-build-status.sh >/dev/null 2>&1 || true
|
|
|
|
FOOTER=$(node -e '
|
|
const fs = require("fs");
|
|
const [buildPath, testPath] = process.argv.slice(1);
|
|
function readStatus(filePath) {
|
|
try {
|
|
return JSON.parse(fs.readFileSync(filePath, "utf8"));
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
function toFooterStatus(value) {
|
|
if (value === "ok") return "pass";
|
|
if (value === "fail") return "FAIL";
|
|
return "unknown";
|
|
}
|
|
const build = readStatus(buildPath);
|
|
const tests = readStatus(testPath);
|
|
let testDetail = "";
|
|
if (tests?.status === "ok" && typeof tests.totalTests === "number") {
|
|
const files =
|
|
typeof tests.totalFiles === "number" && tests.totalFiles > 0
|
|
? ` (${tests.totalFiles} files)`
|
|
: "";
|
|
testDetail = `${tests.totalTests} passed${files}`;
|
|
} else if (
|
|
tests?.status === "fail" &&
|
|
typeof tests.failingTests === "number"
|
|
) {
|
|
testDetail = `${tests.failingTests} failed`;
|
|
}
|
|
const footer =
|
|
`Build: ${toFooterStatus(build?.status)} | Tests: ${toFooterStatus(tests?.status)}` +
|
|
(testDetail ? ` — ${testDetail}` : "");
|
|
process.stdout.write(footer);
|
|
' "$BUILD_PATH" "$TEST_PATH")
|
|
|
|
node -e '
|
|
const fs = require("fs");
|
|
const file = process.argv[1];
|
|
let text = fs.readFileSync(file, "utf8");
|
|
const footerRe = /\s*\n---\n(?:\s*\n)*(?:Build:[^\n]*(?:\n(?:\s*\n)*Tests:[^\n]*)?|Tests:[^\n]*)(?:\s*\n)*$/u;
|
|
let previous;
|
|
do {
|
|
previous = text;
|
|
text = text.replace(footerRe, "");
|
|
} while (text !== previous);
|
|
fs.writeFileSync(file, text.trimEnd() + "\n");
|
|
' "$COMMIT_MSG_FILE"
|
|
|
|
{
|
|
printf '\n---\n'
|
|
printf '%s\n' "$FOOTER"
|
|
} >> "$COMMIT_MSG_FILE"
|
|
|
|
# Also append to local log (gitignored, survives between pushes)
|
|
COMMIT_SUBJECT=$(head -1 "$COMMIT_MSG_FILE")
|
|
TIMESTAMP=$(./scripts/date-format.sh display-ts 2>/dev/null || date '+%d.%b.%Y %H:%M:%S' | tr '[:upper:]' '[:lower:]')
|
|
mkdir -p logs
|
|
printf '%s | %s | %s\n' "$TIMESTAMP" "$FOOTER" "$COMMIT_SUBJECT" >> logs/test-runs.log
|