Skip to content

Commit 45104be

Browse files
authored
Stop escaping generated patches in comments (#18)
* Stop escaping generated patches in comments * Verify rendered patch writes preserve quotes * Bundle * Update AGENTS.md
1 parent 9e4d3cc commit 45104be

File tree

5 files changed

+49
-3
lines changed

5 files changed

+49
-3
lines changed

AGENTS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,8 @@ requests should describe the motivation, summarize testing (`npm test`,
4949
`npm run bundle`), and link the tracking issue. Attach screenshots for
5050
user-visible output changes and ensure generated `dist/` artifacts are updated
5151
alongside source when behavior changes.
52+
53+
## IMPORTANT RULES
54+
55+
Before submitting a PR, run `npm run bundle` to update the javascript bundles.
56+
Otherwise, CI will fail.

dist/generate-pr-patch/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/generate-pr-patch/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/generate-pr-patch/pullRequestCommentTemplate.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import fs from 'fs'
2+
import os from 'os'
3+
import path from 'path'
4+
15
import { renderComment } from './pullRequestCommentTemplate.js'
26

37
describe('renderComment', () => {
@@ -53,6 +57,43 @@ describe('renderComment', () => {
5357
expect(result).toContain('patch contents')
5458
})
5559

60+
it('renders the generated patch without HTML escaping', () => {
61+
const result = renderComment({
62+
generatedCommentBody: 'CI failed on the lint step',
63+
followupPrCreationError: 'Authentication failed',
64+
generatedPatch:
65+
'diff --git a/example.ts b/example.ts\n@@ -1,4 +1,4 @@\n-import { something } from "old"\n+import { something } from "new"'
66+
})
67+
68+
expect(result).toContain('import { something } from "old"')
69+
expect(result).toContain('import { something } from "new"')
70+
expect(result).not.toContain('"')
71+
})
72+
73+
it('preserves quotes when the rendered comment is written to disk', () => {
74+
const result = renderComment({
75+
generatedCommentBody: 'CI failed on the lint step',
76+
followupPrCreationError: 'Authentication failed',
77+
generatedPatch:
78+
'diff --git a/example.ts b/example.ts\n@@ -1,4 +1,4 @@\n-import { something } from "old"\n+import { something } from "new"'
79+
})
80+
81+
expect(result).toBeDefined()
82+
83+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'comment-test-'))
84+
try {
85+
const filePath = path.join(tmpDir, 'comment.md')
86+
fs.writeFileSync(filePath, result ?? '', { encoding: 'utf-8' })
87+
88+
const fileContents = fs.readFileSync(filePath, { encoding: 'utf-8' })
89+
expect(fileContents).toContain('import { something } from "old"')
90+
expect(fileContents).toContain('import { something } from "new"')
91+
expect(fileContents).not.toContain('"')
92+
} finally {
93+
fs.rmSync(tmpDir, { recursive: true, force: true })
94+
}
95+
})
96+
5697
it('does not render patch if passed', () => {
5798
const result = renderComment({
5899
generatedCommentBody: 'CI failed on the lint step',

src/generate-pr-patch/pullRequestCommentTemplate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ I've opened an automated follow-up PR #{{followupPrNumber}} with proposed fixes.
2727
{{#if generatedPatch}}
2828
The patch I tried to generate is as follows:
2929
\`\`\`diff
30-
{{generatedPatch}}
30+
{{{generatedPatch}}}
3131
\`\`\`
3232
{{else}}
3333
No patch was generated.

0 commit comments

Comments
 (0)