fix: address triaged review findings

This commit is contained in:
2026-05-14 02:18:17 +00:00
parent fb5c28114d
commit 1ad87ac4a4
3 changed files with 40 additions and 7 deletions
+12 -1
View File
@@ -11,7 +11,18 @@ const api = (path) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${path}`;
*/ */
export async function getPRDiff() { export async function getPRDiff() {
const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}.diff`), { headers: headers(), timeout: 60000, httpsAgent }); const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}.diff`), { headers: headers(), timeout: 60000, httpsAgent });
return filterDiff(resp.data, ['.gitea/', '.amazonq/', '.claude/', '.codex/', '.gemini/', '.github/', 'CLAUDE.md', 'GEMINI.md', 'TODO.md', 'README.md']); return filterDiff(resp.data, [
'.gitea/',
'.amazonq/',
'.claude/',
'.codex/',
'.gemini/',
'.github/',
'CLAUDE.md',
'GEMINI.md',
'TODO.md',
'README.md',
]);
} }
/** /**
+3 -6
View File
@@ -1,12 +1,11 @@
import { describe, it, afterEach, mock } from 'node:test'; import { describe, it, afterEach, mock } from 'node:test';
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import axios from 'axios'; import axios from 'axios';
import { getPRDiff, filterDiff, postComment } from './gitea.js';
afterEach(() => mock.restoreAll()); afterEach(() => mock.restoreAll());
describe('gitea', async () => { describe('gitea', () => {
const { getPRDiff, filterDiff, postComment } = await import('./gitea.js');
it('getPRDiff calls Gitea diff API with Authorization header', async () => { it('getPRDiff calls Gitea diff API with Authorization header', async () => {
let capturedUrl, capturedOpts; let capturedUrl, capturedOpts;
mock.method(axios, 'get', async (url, opts) => { mock.method(axios, 'get', async (url, opts) => {
@@ -59,9 +58,7 @@ describe('gitea', async () => {
}); });
}); });
describe('filterDiff', async () => { describe('filterDiff', () => {
const { filterDiff } = await import('./gitea.js');
const block = (file) => `diff --git a/${file} b/${file}\n--- a/${file}\n+++ b/${file}\n@@ -1 +1 @@\n-old\n+new\n`; const block = (file) => `diff --git a/${file} b/${file}\n--- a/${file}\n+++ b/${file}\n@@ -1 +1 @@\n-old\n+new\n`;
it('filters out configured folder blocks', () => { it('filters out configured folder blocks', () => {
+25
View File
@@ -6,6 +6,7 @@ import path from 'path';
import { stripCodeFence, repairJSONArrayWithAI, validateJSONArrayFile, ensureJSONArrayFileExists } from './json.js'; import { stripCodeFence, repairJSONArrayWithAI, validateJSONArrayFile, ensureJSONArrayFileExists } from './json.js';
describe('json helpers', () => { describe('json helpers', () => {
const MAX_JSON_BYTES = 1024 * 1024;
let workspace; let workspace;
beforeEach(() => { beforeEach(() => {
@@ -76,6 +77,16 @@ describe('json helpers', () => {
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[]\n'); assert.equal(fs.readFileSync(fullPath, 'utf8'), '[]\n');
}); });
it('reads a valid JSON file whose size equals the maximum limit', async () => {
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, `[]${' '.repeat(MAX_JSON_BYTES - 2)}`, 'utf8');
const result = await validateJSONArrayFile(fullPath, '.gitea/ai-review/findings.json');
assert.deepEqual(result, { exists: true, valid: true, repaired: false });
});
it('repairs invalid JSON using AI output and rewrites the file', async () => { it('repairs invalid JSON using AI output and rewrites the file', async () => {
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json'); const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
fs.mkdirSync(path.dirname(fullPath), { recursive: true }); fs.mkdirSync(path.dirname(fullPath), { recursive: true });
@@ -90,6 +101,20 @@ describe('json helpers', () => {
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[{"fixed":true}]\n'); assert.equal(fs.readFileSync(fullPath, 'utf8'), '[{"fixed":true}]\n');
}); });
it('preserves a trailing newline returned by AI repair', async () => {
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
fs.writeFileSync(fullPath, '{broken', 'utf8');
const result = await validateJSONArrayFile(fullPath, '.gitea/ai-review/findings.json', async (_fullPath, _label, original) => {
assert.equal(original, '{broken');
return '[{"fixed":true}]\n';
});
assert.deepEqual(result, { exists: true, valid: true, repaired: true });
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[{"fixed":true}]\n');
});
it('throws when AI repair fails', async () => { it('throws when AI repair fails', async () => {
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json'); const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
fs.mkdirSync(path.dirname(fullPath), { recursive: true }); fs.mkdirSync(path.dirname(fullPath), { recursive: true });