Skip to content

Commit d16e599

Browse files
committed
ci(translator): address CodeRabbit review findings
- Fix multiline regex truncating JSON when translated body contains a fenced code block: try parsing raw first, only strip outer fences as a fallback, and use literal newline anchors instead of the /m flag - Clamp translated title to 256 chars before the issues.update call to prevent a 422 from the GitHub API on verbose or CJK-heavy titles - Paginate listComments so the duplicate-comment guard works correctly on issues that already have more than 100 comments
1 parent 2465896 commit d16e599

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

‎.github/workflows/issue-translator.yml‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,15 @@ jobs:
7171
const raw = process.env.AI_RESPONSE ?? '';
7272
let parsed;
7373
try {
74-
const cleaned = raw.replace(/^```(?:json)?\s*/m, '').replace(/\s*```\s*$/m, '').trim();
75-
parsed = JSON.parse(cleaned);
74+
// Try parsing as-is first; only strip outer fences if that fails.
75+
// Avoid /m flag — it makes $ match mid-string newlines and can
76+
// truncate the payload when the translated body contains code fences.
77+
try {
78+
parsed = JSON.parse(raw.trim());
79+
} catch {
80+
const stripped = raw.trim().replace(/^```(?:json)?\s*\n/, '').replace(/\n```\s*$/, '');
81+
parsed = JSON.parse(stripped);
82+
}
7683
} catch (err) {
7784
core.warning(`AI response was not valid JSON: ${raw}`);
7885
return;
@@ -83,8 +90,8 @@ jobs:
8390
return;
8491
}
8592
86-
const title = typeof parsed.translated_title === 'string'
87-
? parsed.translated_title.trim() : '';
93+
const title = (typeof parsed.translated_title === 'string'
94+
? parsed.translated_title.trim() : '').slice(0, 256); // GitHub title limit
8895
const body = typeof parsed.translated_body === 'string'
8996
? parsed.translated_body.trim() : '';
9097
const lang = typeof parsed.detected_language === 'string'
@@ -107,8 +114,8 @@ jobs:
107114
return;
108115
}
109116
110-
// Guard: don't post twice (e.g. if the workflow re-runs)
111-
const { data: comments } = await github.rest.issues.listComments({
117+
// Guard: don't post twice — paginate to handle issues with >100 comments
118+
const comments = await github.paginate(github.rest.issues.listComments, {
112119
owner, repo, issue_number, per_page: 100,
113120
});
114121
if (comments.some(c => c.body?.includes(MARKER))) {

0 commit comments

Comments
 (0)