@jonit-dev/night-watch-cli 1.7.35 → 1.7.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,281 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # Night Watch QA Cron Runner (project-agnostic)
5
+ # Usage: night-watch-qa-cron.sh /path/to/project
6
+ #
7
+ # NOTE: This script expects environment variables to be set by the caller.
8
+ # The Node.js CLI will inject config values via environment variables.
9
+ # Required env vars (with defaults shown):
10
+ # NW_QA_MAX_RUNTIME=3600 - Maximum runtime in seconds (1 hour)
11
+ # NW_PROVIDER_CMD=claude - AI provider CLI to use (claude, codex, etc.)
12
+ # NW_BRANCH_PATTERNS=feat/,night-watch/ - Comma-separated branch prefixes to match
13
+ # NW_QA_SKIP_LABEL=skip-qa - Label to skip QA on a PR
14
+ # NW_QA_ARTIFACTS=both - Artifact mode (both, tests, report)
15
+ # NW_QA_AUTO_INSTALL_PLAYWRIGHT=1 - Auto-install Playwright browsers
16
+ # NW_DRY_RUN=0 - Set to 1 for dry-run mode (prints diagnostics only)
17
+
18
+ PROJECT_DIR="${1:?Usage: $0 /path/to/project}"
19
+ PROJECT_NAME=$(basename "${PROJECT_DIR}")
20
+ LOG_DIR="${PROJECT_DIR}/logs"
21
+ LOG_FILE="${LOG_DIR}/night-watch-qa.log"
22
+ MAX_RUNTIME="${NW_QA_MAX_RUNTIME:-3600}" # 1 hour
23
+ MAX_LOG_SIZE="524288" # 512 KB
24
+ PROVIDER_CMD="${NW_PROVIDER_CMD:-claude}"
25
+ BRANCH_PATTERNS_RAW="${NW_BRANCH_PATTERNS:-feat/,night-watch/}"
26
+ SKIP_LABEL="${NW_QA_SKIP_LABEL:-skip-qa}"
27
+ QA_ARTIFACTS="${NW_QA_ARTIFACTS:-both}"
28
+ QA_AUTO_INSTALL_PLAYWRIGHT="${NW_QA_AUTO_INSTALL_PLAYWRIGHT:-1}"
29
+
30
+ # Ensure NVM / Node / Claude are on PATH
31
+ export NVM_DIR="${HOME}/.nvm"
32
+ [ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh"
33
+
34
+ # NOTE: Environment variables should be set by the caller (Node.js CLI).
35
+ # The .env.night-watch sourcing has been removed - config is now injected via env vars.
36
+
37
+ mkdir -p "${LOG_DIR}"
38
+
39
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
40
+ # shellcheck source=night-watch-helpers.sh
41
+ source "${SCRIPT_DIR}/night-watch-helpers.sh"
42
+ PROJECT_RUNTIME_KEY=$(project_runtime_key "${PROJECT_DIR}")
43
+ # NOTE: Lock file path must match qaLockPath() in src/utils/status-data.ts
44
+ LOCK_FILE="/tmp/night-watch-qa-${PROJECT_RUNTIME_KEY}.lock"
45
+
46
+ emit_result() {
47
+ local status="${1:?status required}"
48
+ local details="${2:-}"
49
+ if [ -n "${details}" ]; then
50
+ echo "NIGHT_WATCH_RESULT:${status}|${details}"
51
+ else
52
+ echo "NIGHT_WATCH_RESULT:${status}"
53
+ fi
54
+ }
55
+
56
+ # Validate provider
57
+ if ! validate_provider "${PROVIDER_CMD}"; then
58
+ echo "ERROR: Unknown provider: ${PROVIDER_CMD}" >&2
59
+ exit 1
60
+ fi
61
+
62
+ rotate_log
63
+
64
+ if ! acquire_lock "${LOCK_FILE}"; then
65
+ emit_result "skip_locked"
66
+ exit 0
67
+ fi
68
+
69
+ cd "${PROJECT_DIR}"
70
+
71
+ # Convert comma-separated branch prefixes into a regex that matches branch starts.
72
+ BRANCH_REGEX=""
73
+ IFS=',' read -r -a BRANCH_PATTERNS <<< "${BRANCH_PATTERNS_RAW}"
74
+ for pattern in "${BRANCH_PATTERNS[@]}"; do
75
+ trimmed_pattern=$(printf '%s' "${pattern}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
76
+ if [ -n "${trimmed_pattern}" ]; then
77
+ BRANCH_REGEX="${BRANCH_REGEX}${BRANCH_REGEX:+|}^${trimmed_pattern}"
78
+ fi
79
+ done
80
+
81
+ if [ -z "${BRANCH_REGEX}" ]; then
82
+ BRANCH_REGEX='^(feat/|night-watch/)'
83
+ fi
84
+
85
+ # List open PRs with their details for filtering
86
+ PR_JSON=$(gh pr list --state open --json number,headRefName,title,labels 2>/dev/null || echo "[]")
87
+
88
+ # Count PRs matching branch patterns
89
+ OPEN_PRS=$(
90
+ echo "${PR_JSON}" \
91
+ | jq -r '.[].headRefName' 2>/dev/null \
92
+ | { grep -E "${BRANCH_REGEX}" || true; } \
93
+ | wc -l \
94
+ | tr -d '[:space:]'
95
+ )
96
+
97
+ if [ "${OPEN_PRS}" -eq 0 ]; then
98
+ log "SKIP: No open PRs matching branch patterns (${BRANCH_PATTERNS_RAW})"
99
+ emit_result "skip_no_open_prs"
100
+ exit 0
101
+ fi
102
+
103
+ REPO=$(gh repo view --json nameWithOwner --jq '.nameWithOwner' 2>/dev/null || echo "")
104
+
105
+ # Collect PRs that need QA
106
+ PRS_NEEDING_QA=""
107
+ QA_NEEDED=0
108
+
109
+ while IFS=$'\t' read -r pr_number pr_branch pr_title pr_labels; do
110
+ if [ -z "${pr_number}" ] || [ -z "${pr_branch}" ]; then
111
+ continue
112
+ fi
113
+
114
+ # Filter by branch pattern
115
+ if ! printf '%s\n' "${pr_branch}" | grep -Eq "${BRANCH_REGEX}"; then
116
+ continue
117
+ fi
118
+
119
+ # Skip PRs with the skip label
120
+ if echo "${pr_labels}" | grep -q "${SKIP_LABEL}"; then
121
+ log "SKIP-QA: PR #${pr_number} (${pr_branch}) has '${SKIP_LABEL}' label"
122
+ continue
123
+ fi
124
+
125
+ # Skip PRs with [skip-qa] in their title
126
+ if echo "${pr_title}" | grep -qi '\[skip-qa\]'; then
127
+ log "SKIP-QA: PR #${pr_number} (${pr_branch}) has [skip-qa] in title"
128
+ continue
129
+ fi
130
+
131
+ # Skip PRs that already have a QA comment (idempotency)
132
+ ALL_COMMENTS=$(
133
+ {
134
+ gh pr view "${pr_number}" --json comments --jq '.comments[].body' 2>/dev/null || true
135
+ if [ -n "${REPO}" ]; then
136
+ gh api "repos/${REPO}/issues/${pr_number}/comments" --jq '.[].body' 2>/dev/null || true
137
+ fi
138
+ } | sort -u
139
+ )
140
+ if echo "${ALL_COMMENTS}" | grep -q '<!-- night-watch-qa-marker -->'; then
141
+ log "SKIP-QA: PR #${pr_number} (${pr_branch}) already has QA comment"
142
+ continue
143
+ fi
144
+
145
+ QA_NEEDED=1
146
+ PRS_NEEDING_QA="${PRS_NEEDING_QA} #${pr_number}"
147
+ done < <(
148
+ echo "${PR_JSON}" \
149
+ | jq -r '.[] | [.number, .headRefName, .title, ([.labels[].name] | join(","))] | @tsv' 2>/dev/null || true
150
+ )
151
+
152
+ if [ "${QA_NEEDED}" -eq 0 ]; then
153
+ log "SKIP: All ${OPEN_PRS} open PR(s) matching patterns already have QA comments"
154
+ emit_result "skip_all_qa_done"
155
+ exit 0
156
+ fi
157
+
158
+ PRS_NEEDING_QA=$(echo "${PRS_NEEDING_QA}" \
159
+ | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]][[:space:]]*/ /g' -e 's/[[:space:]]*$//')
160
+ PRS_NEEDING_QA_CSV="${PRS_NEEDING_QA// /,}"
161
+
162
+ if [ -n "${NW_DEFAULT_BRANCH:-}" ]; then
163
+ DEFAULT_BRANCH="${NW_DEFAULT_BRANCH}"
164
+ else
165
+ DEFAULT_BRANCH=$(detect_default_branch "${PROJECT_DIR}")
166
+ fi
167
+ QA_WORKTREE_DIR="$(dirname "${PROJECT_DIR}")/${PROJECT_NAME}-nw-qa-runner"
168
+
169
+ log "START: Found PR(s) needing QA:${PRS_NEEDING_QA}"
170
+
171
+ cleanup_worktrees "${PROJECT_DIR}"
172
+
173
+ # Dry-run mode: print diagnostics and exit
174
+ if [ "${NW_DRY_RUN:-0}" = "1" ]; then
175
+ echo "=== Dry Run: QA Runner ==="
176
+ echo "Provider: ${PROVIDER_CMD}"
177
+ echo "Branch Patterns: ${BRANCH_PATTERNS_RAW}"
178
+ echo "Skip Label: ${SKIP_LABEL}"
179
+ echo "QA Artifacts: ${QA_ARTIFACTS}"
180
+ echo "Auto-install Playwright: ${QA_AUTO_INSTALL_PLAYWRIGHT}"
181
+ echo "Open PRs needing QA:${PRS_NEEDING_QA}"
182
+ echo "Default Branch: ${DEFAULT_BRANCH}"
183
+ echo "QA Worktree: ${QA_WORKTREE_DIR}"
184
+ echo "Timeout: ${MAX_RUNTIME}s"
185
+ exit 0
186
+ fi
187
+
188
+ EXIT_CODE=0
189
+
190
+ # Process each PR that needs QA
191
+ for pr_ref in ${PRS_NEEDING_QA}; do
192
+ pr_num="${pr_ref#\#}"
193
+
194
+ cleanup_worktrees "${PROJECT_DIR}"
195
+ if ! prepare_detached_worktree "${PROJECT_DIR}" "${QA_WORKTREE_DIR}" "${DEFAULT_BRANCH}" "${LOG_FILE}"; then
196
+ log "FAIL: Unable to create isolated QA worktree ${QA_WORKTREE_DIR} for PR #${pr_num}"
197
+ EXIT_CODE=1
198
+ break
199
+ fi
200
+
201
+ log "QA: Checking out PR #${pr_num} in worktree"
202
+ if ! (cd "${QA_WORKTREE_DIR}" && gh pr checkout "${pr_num}" >> "${LOG_FILE}" 2>&1); then
203
+ log "WARN: Failed to checkout PR #${pr_num}, skipping"
204
+ EXIT_CODE=1
205
+ cleanup_worktrees "${PROJECT_DIR}"
206
+ continue
207
+ fi
208
+
209
+ case "${PROVIDER_CMD}" in
210
+ claude)
211
+ if (
212
+ cd "${QA_WORKTREE_DIR}" && timeout "${MAX_RUNTIME}" \
213
+ claude -p "/night-watch-qa" \
214
+ --dangerously-skip-permissions \
215
+ >> "${LOG_FILE}" 2>&1
216
+ ); then
217
+ log "QA: PR #${pr_num} — provider completed successfully"
218
+ else
219
+ local_exit=$?
220
+ log "QA: PR #${pr_num} — provider exited with code ${local_exit}"
221
+ if [ ${local_exit} -eq 124 ]; then
222
+ EXIT_CODE=124
223
+ break
224
+ fi
225
+ EXIT_CODE=${local_exit}
226
+ fi
227
+ ;;
228
+ codex)
229
+ if (
230
+ cd "${QA_WORKTREE_DIR}" && timeout "${MAX_RUNTIME}" \
231
+ codex --quiet \
232
+ --yolo \
233
+ --prompt "$(cat "${QA_WORKTREE_DIR}/.claude/commands/night-watch-qa.md")" \
234
+ >> "${LOG_FILE}" 2>&1
235
+ ); then
236
+ log "QA: PR #${pr_num} — provider completed successfully"
237
+ else
238
+ local_exit=$?
239
+ log "QA: PR #${pr_num} — provider exited with code ${local_exit}"
240
+ if [ ${local_exit} -eq 124 ]; then
241
+ EXIT_CODE=124
242
+ break
243
+ fi
244
+ EXIT_CODE=${local_exit}
245
+ fi
246
+ ;;
247
+ *)
248
+ log "ERROR: Unknown provider: ${PROVIDER_CMD}"
249
+ exit 1
250
+ ;;
251
+ esac
252
+
253
+ cleanup_worktrees "${PROJECT_DIR}"
254
+ done
255
+
256
+ cleanup_worktrees "${PROJECT_DIR}"
257
+
258
+ if [ ${EXIT_CODE} -eq 0 ]; then
259
+ log "DONE: QA runner completed successfully"
260
+ if [ -n "${REPO}" ]; then
261
+ emit_result "success_qa" "prs=${PRS_NEEDING_QA_CSV}|repo=${REPO}"
262
+ else
263
+ emit_result "success_qa" "prs=${PRS_NEEDING_QA_CSV}"
264
+ fi
265
+ elif [ ${EXIT_CODE} -eq 124 ]; then
266
+ log "TIMEOUT: QA runner killed after ${MAX_RUNTIME}s"
267
+ if [ -n "${REPO}" ]; then
268
+ emit_result "timeout" "prs=${PRS_NEEDING_QA_CSV}|repo=${REPO}"
269
+ else
270
+ emit_result "timeout" "prs=${PRS_NEEDING_QA_CSV}"
271
+ fi
272
+ else
273
+ log "FAIL: QA runner exited with code ${EXIT_CODE}"
274
+ if [ -n "${REPO}" ]; then
275
+ emit_result "failure" "prs=${PRS_NEEDING_QA_CSV}|repo=${REPO}"
276
+ else
277
+ emit_result "failure" "prs=${PRS_NEEDING_QA_CSV}"
278
+ fi
279
+ fi
280
+
281
+ exit "${EXIT_CODE}"
@@ -0,0 +1,90 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+
4
+ # Night Watch Slicer Cron Runner (project-agnostic)
5
+ # Usage: night-watch-slicer-cron.sh /path/to/project
6
+ #
7
+ # This is a thin wrapper that acquires a lock and calls `night-watch slice`.
8
+ # The CLI command handles all the logic directly in TypeScript.
9
+ #
10
+ # NOTE: This script expects environment variables to be set by the caller.
11
+ # The Node.js CLI will inject config values via environment variables.
12
+ # Required env vars (with defaults shown):
13
+ # NW_SLICER_MAX_RUNTIME=600 - Maximum runtime in seconds (10 minutes)
14
+ # NW_PROVIDER_CMD=claude - AI provider CLI to use (claude, codex, etc.)
15
+ # NW_DRY_RUN=0 - Set to 1 for dry-run mode (prints diagnostics only)
16
+
17
+ PROJECT_DIR="${1:?Usage: $0 /path/to/project}"
18
+ PROJECT_NAME=$(basename "${PROJECT_DIR}")
19
+ LOG_DIR="${PROJECT_DIR}/logs"
20
+ LOG_FILE="${LOG_DIR}/night-watch-slicer.log"
21
+ LOCK_FILE=""
22
+ MAX_RUNTIME="${NW_SLICER_MAX_RUNTIME:-600}" # 10 minutes
23
+ MAX_LOG_SIZE="524288" # 512 KB
24
+ PROVIDER_CMD="${NW_PROVIDER_CMD:-claude}"
25
+
26
+ # Ensure NVM / Node / Night Watch CLI are on PATH
27
+ export NVM_DIR="${HOME}/.nvm"
28
+ [ -s "${NVM_DIR}/nvm.sh" ] && . "${NVM_DIR}/nvm.sh"
29
+
30
+ mkdir -p "${LOG_DIR}"
31
+
32
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
33
+ # shellcheck source=night-watch-helpers.sh
34
+ source "${SCRIPT_DIR}/night-watch-helpers.sh"
35
+ PROJECT_RUNTIME_KEY=$(project_runtime_key "${PROJECT_DIR}")
36
+ LOCK_FILE="/tmp/night-watch-slicer-${PROJECT_RUNTIME_KEY}.lock"
37
+
38
+ # Validate provider
39
+ if ! validate_provider "${PROVIDER_CMD}"; then
40
+ echo "ERROR: Unknown provider: ${PROVIDER_CMD}" >&2
41
+ exit 1
42
+ fi
43
+
44
+ rotate_log
45
+
46
+ if ! acquire_lock "${LOCK_FILE}"; then
47
+ exit 0
48
+ fi
49
+
50
+ cleanup_on_exit() {
51
+ rm -f "${LOCK_FILE}"
52
+ }
53
+
54
+ trap cleanup_on_exit EXIT
55
+
56
+ log "START: Running roadmap slicer for ${PROJECT_DIR}"
57
+
58
+ # Dry-run mode: print diagnostics and exit
59
+ if [ "${NW_DRY_RUN:-0}" = "1" ]; then
60
+ echo "=== Dry Run: Roadmap Slicer ==="
61
+ echo "Provider: ${PROVIDER_CMD}"
62
+ echo "Project Dir: ${PROJECT_DIR}"
63
+ echo "Timeout: ${MAX_RUNTIME}s"
64
+ exit 0
65
+ fi
66
+
67
+ # Resolve night-watch CLI
68
+ CLI_BIN=""
69
+ if ! CLI_BIN=$(resolve_night_watch_cli); then
70
+ log "ERROR: Could not resolve night-watch CLI"
71
+ exit 1
72
+ fi
73
+
74
+ # Run the slice command with timeout
75
+ EXIT_CODE=0
76
+ if timeout "${MAX_RUNTIME}" "${CLI_BIN}" slice >> "${LOG_FILE}" 2>&1; then
77
+ EXIT_CODE=0
78
+ else
79
+ EXIT_CODE=$?
80
+ fi
81
+
82
+ if [ ${EXIT_CODE} -eq 0 ]; then
83
+ log "DONE: Slicer completed successfully"
84
+ elif [ ${EXIT_CODE} -eq 124 ]; then
85
+ log "TIMEOUT: Slicer killed after ${MAX_RUNTIME}s"
86
+ else
87
+ log "FAIL: Slicer exited with code ${EXIT_CODE}"
88
+ fi
89
+
90
+ exit ${EXIT_CODE}
@@ -0,0 +1,77 @@
1
+ #!/usr/bin/env bats
2
+
3
+ # Tests for night-watch-helpers.sh claim functions
4
+
5
+ setup() {
6
+ # Source the helpers
7
+ SCRIPT_DIR="$(cd "$(dirname "${BATS_TEST_FILENAME}")" && pwd)"
8
+
9
+ # Set required globals
10
+ export LOG_FILE="/tmp/night-watch-test-$$.log"
11
+
12
+ source "${SCRIPT_DIR}/night-watch-helpers.sh"
13
+
14
+ # Create temp PRD directory
15
+ TEST_PRD_DIR=$(mktemp -d)
16
+ echo "# Test PRD" > "${TEST_PRD_DIR}/01-test-prd.md"
17
+ echo "# Test PRD 2" > "${TEST_PRD_DIR}/02-test-prd.md"
18
+ }
19
+
20
+ teardown() {
21
+ rm -rf "${TEST_PRD_DIR}"
22
+ rm -f "${LOG_FILE}"
23
+ }
24
+
25
+ @test "claim_prd creates .claim file with JSON" {
26
+ claim_prd "${TEST_PRD_DIR}" "01-test-prd.md"
27
+
28
+ [ -f "${TEST_PRD_DIR}/01-test-prd.md.claim" ]
29
+
30
+ local content
31
+ content=$(cat "${TEST_PRD_DIR}/01-test-prd.md.claim")
32
+
33
+ # Check JSON contains expected fields
34
+ echo "${content}" | grep -q '"timestamp":'
35
+ echo "${content}" | grep -q '"hostname":'
36
+ echo "${content}" | grep -q '"pid":'
37
+ }
38
+
39
+ @test "is_claimed returns 0 for active claim" {
40
+ claim_prd "${TEST_PRD_DIR}" "01-test-prd.md"
41
+
42
+ run is_claimed "${TEST_PRD_DIR}" "01-test-prd.md" 7200
43
+ [ "$status" -eq 0 ]
44
+ }
45
+
46
+ @test "is_claimed returns 1 for stale claim" {
47
+ # Write a claim with an old timestamp (1 second)
48
+ printf '{"timestamp":1000000000,"hostname":"test","pid":1}\n' \
49
+ > "${TEST_PRD_DIR}/01-test-prd.md.claim"
50
+
51
+ run is_claimed "${TEST_PRD_DIR}" "01-test-prd.md" 7200
52
+ [ "$status" -eq 1 ]
53
+ }
54
+
55
+ @test "is_claimed returns 1 for no claim" {
56
+ run is_claimed "${TEST_PRD_DIR}" "01-test-prd.md" 7200
57
+ [ "$status" -eq 1 ]
58
+ }
59
+
60
+ @test "release_claim removes .claim file" {
61
+ claim_prd "${TEST_PRD_DIR}" "01-test-prd.md"
62
+ [ -f "${TEST_PRD_DIR}/01-test-prd.md.claim" ]
63
+
64
+ release_claim "${TEST_PRD_DIR}" "01-test-prd.md"
65
+ [ ! -f "${TEST_PRD_DIR}/01-test-prd.md.claim" ]
66
+ }
67
+
68
+ @test "find_eligible_prd skips claimed PRD" {
69
+ # Claim the first PRD
70
+ claim_prd "${TEST_PRD_DIR}" "01-test-prd.md"
71
+
72
+ # find_eligible_prd should skip 01 and return 02
73
+ local result
74
+ result=$(find_eligible_prd "${TEST_PRD_DIR}" 7200)
75
+
76
+ [ "${result}" = "02-test-prd.md" ]
77
+ }