Skip to content

Commit cf2596c

Browse files
Hannia Valerahanniavalera
authored andcommitted
switch to use spawnSync for gh CLI commands and improve error handling
1 parent d9455c0 commit cf2596c

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

.github/scripts/check-coverage-and-open-issue.mjs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
// @ts-check
33
import { readFileSync, writeFileSync, unlinkSync } from 'fs';
4-
import { execSync, spawnSync } from 'child_process';
4+
import { spawnSync } from 'child_process';
55
import { relative, resolve, join } from 'path';
66
import { tmpdir } from 'os';
77

@@ -62,10 +62,17 @@ if (belowThreshold.length === 0 && totalLines >= THRESHOLD) {
6262
// ── 3. Check for existing open coverage issue (avoid duplicates) ─────────────
6363
let existingIssueNumber = null;
6464
try {
65-
existingIssueNumber = execSync(
66-
`gh issue list --repo ${REPO} --label test-coverage --state open --json number --jq '.[0].number'`,
67-
{ encoding: 'utf8' }
68-
).trim() || null;
65+
const result = spawnSync('gh', [
66+
'issue', 'list',
67+
'--repo', REPO,
68+
'--label', 'test-coverage',
69+
'--state', 'open',
70+
'--json', 'number',
71+
'--jq', '.[0].number'
72+
], { encoding: 'utf8' });
73+
if (result.status === 0 && result.stdout.trim()) {
74+
existingIssueNumber = result.stdout.trim();
75+
}
6976
} catch {
7077
// gh CLI may fail if label doesn't exist yet — continue to create
7178
}
@@ -153,16 +160,18 @@ ${remainingNote}
153160
`;
154161

155162
// ── 5. Ensure the test-coverage label exists ─────────────────────────────────
156-
try {
157-
spawnSync('gh', [
163+
{
164+
const result = spawnSync('gh', [
158165
'label', 'create', 'test-coverage',
159166
'--repo', REPO,
160167
'--color', '0075ca',
161168
'--description', 'Opened by the coverage agent',
162169
'--force'
163170
], { stdio: 'inherit' });
164-
} catch {
165-
// --force handles existing labels; ignore unexpected errors
171+
// Non-zero is acceptable here — label may already exist without --force support
172+
if (result.error) {
173+
console.warn('Warning: could not ensure test-coverage label exists:', result.error.message);
174+
}
166175
}
167176

168177
// ── 6. Create or update the coverage issue ───────────────────────────────────
@@ -173,24 +182,29 @@ const bodyFile = join(tmpdir(), `coverage-issue-body-${Date.now()}.md`);
173182
writeFileSync(bodyFile, issueBody, 'utf8');
174183

175184
try {
185+
let result;
176186
if (existingIssueNumber) {
177187
console.log(`\nUpdating existing issue #${existingIssueNumber}`);
178-
spawnSync('gh', [
188+
result = spawnSync('gh', [
179189
'issue', 'edit', existingIssueNumber,
180190
'--repo', REPO,
181191
'--title', title,
182192
'--body-file', bodyFile
183193
], { stdio: 'inherit' });
184194
} else {
185195
console.log(`\nOpening issue: ${title}`);
186-
spawnSync('gh', [
196+
result = spawnSync('gh', [
187197
'issue', 'create',
188198
'--repo', REPO,
189199
'--title', title,
190200
'--body-file', bodyFile,
191201
'--label', 'test-coverage'
192202
], { stdio: 'inherit' });
193203
}
204+
if (result.status !== 0) {
205+
console.error(`gh command failed with exit code ${result.status}`);
206+
process.exit(result.status ?? 1);
207+
}
194208
} finally {
195209
try { unlinkSync(bodyFile); } catch { /* cleanup best-effort */ }
196210
}

0 commit comments

Comments
 (0)