Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions .github/workflows/code-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ jobs:
with:
script: |
const fs = require('node:fs');
const { hasToken, hasCanary, makeFallback } =
const { hasToken, hasCanary, makeFallback, extractJson } =
require(`${process.env.GITHUB_WORKSPACE}/_prepare/scripts/scan`);

const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`;
Expand Down Expand Up @@ -653,13 +653,8 @@ jobs:
return;
}

// Tolerate preamble/postamble or ```json fences — slice from first { to last }.
let candidate = last.trim();
const firstBrace = candidate.indexOf('{');
const lastBrace = candidate.lastIndexOf('}');
if (firstBrace !== -1 && lastBrace > firstBrace) {
candidate = candidate.slice(firstBrace, lastBrace + 1);
}
// Tolerate preamble/postamble or ```json fences.
let candidate = extractJson(last.trim()) ?? last.trim();

let review;
try { review = JSON.parse(candidate); }
Expand Down Expand Up @@ -963,7 +958,7 @@ jobs:
script: |
const fs = require('node:fs');
const path = require('node:path');
const { hasToken, hasCanary, makeFallback } =
const { hasToken, hasCanary, makeFallback, extractJson } =
require(path.join(process.env.GITHUB_WORKSPACE, '_prepare/scripts/scan'));

const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`;
Expand All @@ -978,11 +973,10 @@ jobs:
let review = null;
try { const obj = JSON.parse(raw); if (obj.body !== undefined) review = obj; } catch {}
if (!review) {
const start = raw.indexOf('{');
const end = raw.lastIndexOf('}');
if (start !== -1 && end !== -1) {
const extracted = extractJson(raw);
if (extracted) {
try {
const obj = JSON.parse(raw.slice(start, end + 1));
const obj = JSON.parse(extracted);
if (obj.body !== undefined && Array.isArray(obj.comments)) review = obj;
} catch {}
}
Expand Down
14 changes: 13 additions & 1 deletion src/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ function validateReview(review) {
}
return { errors };
}
module.exports = { hasToken, hasCanary, makeFallback, validateReview };
// Scan backward from the last } to find its depth-matching { so that stray
// brace pairs in prose before the JSON block (e.g. `Foo{}`) are skipped.
function extractJson(text) {
const lastBrace = text.lastIndexOf('}');
if (lastBrace === -1) return null;
let depth = 0;
for (let i = lastBrace; i >= 0; i--) {
if (text[i] === '}') depth++;
else if (text[i] === '{' && --depth === 0) return text.slice(i, lastBrace + 1);
}
return null;
}
module.exports = { hasToken, hasCanary, makeFallback, validateReview, extractJson };
41 changes: 40 additions & 1 deletion tests/scan.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';
const assert = require('node:assert/strict');
const { hasToken, hasCanary, makeFallback, validateReview } = require('../src/scan.js');
const { hasToken, hasCanary, makeFallback, validateReview, extractJson } = require('../src/scan.js');

// ---------------------------------------------------------------------------
// hasToken
Expand Down Expand Up @@ -205,3 +205,42 @@ test('validateReview - comment line must be positive integer', () => {
assert.ok(validateReview({ body: '', event: 'COMMENT', comments: [{ ...base, line: 1.5 }] }).errors.length > 0);
assert.equal(validateReview({ body: '', event: 'COMMENT', comments: [{ ...base, line: 1 }] }).errors.length, 0);
});

// ---------------------------------------------------------------------------
// extractJson
// ---------------------------------------------------------------------------
test('extractJson - bare JSON round-trips correctly', () => {
const json = '{"body":"LGTM","event":"COMMENT","comments":[]}';
assert.equal(extractJson(json), json);
});

test('extractJson - strips markdown code fence', () => {
const json = '{"body":"ok","event":"COMMENT","comments":[]}';
const fenced = '```json\n' + json + '\n```';
assert.equal(extractJson(fenced), json);
});

test('extractJson - skips stray brace pair in prose before JSON', () => {
const json = '{"body":"ok","event":"COMMENT","comments":[]}';
const prose = 'Consider using the Foo{} pattern.\n\n' + json;
assert.equal(extractJson(prose), json);
});

test('extractJson - skips multiple stray brace pairs in prose', () => {
const json = '{"body":"ok","event":"COMMENT","comments":[]}';
const prose = 'Use {} for empty objects and {key: val} for maps.\n\n' + json;
assert.equal(extractJson(prose), json);
});

test('extractJson - returns null when no braces present', () => {
assert.equal(extractJson('no braces here'), null);
});

test('extractJson - returns null for empty string', () => {
assert.equal(extractJson(''), null);
});

test('extractJson - handles JSON with brace pairs inside string values', () => {
const json = '{"body":"use {} syntax","event":"COMMENT","comments":[]}';
assert.equal(extractJson(json), json);
});