Skip to content

Commit 27ba43e

Browse files
authored
Fix padding when editing PR comments, and detection of non-root issue comment edits (#89)
2 parents f8b5faa + 8a0a94f commit 27ba43e

File tree

7 files changed

+9214
-18
lines changed

7 files changed

+9214
-18
lines changed

.claude/commands/corpus-loop.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ If you see `"title": "TODO_TITLE"` or similar hardcoded `TODO` values in the JSO
5252

5353
## Extraction code style
5454

55-
- Don't hedge your bets and write lots of fallback code or strings of `?.`. Have a specific piece of data you want to get, use non-null `!` assertions where necessary to be clear about getting.
55+
- Don't hedge your bets and write lots of fallback code or strings of `?.`. Have a specific piece of data you want to get, use non-null ! assertions where necessary to be clear about getting.
5656
- If a field is empty, represent it with an empty string. Don't use placeholders when extracting data.
5757
- The pages we are scraping are going to change over time, and it's easier to fix broken ones if we know exactly what used to work. If the code has lots of branching paths, it's harder to tell what it was doing.
5858

src/lib/enhancers/github/GitHubEditEnhancer.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { commonGitHubOptions, prepareGitHubHighlighter } from './github-common'
88
const GH_EDIT = 'GH_EDIT' as const
99

1010
export interface GitHubEditSpot extends CommentSpot {
11+
isIssue: boolean
1112
type: typeof GH_EDIT
1213
}
1314

@@ -21,41 +22,45 @@ export class GitHubEditEnhancer implements CommentEnhancer<GitHubEditSpot> {
2122
return null
2223
}
2324

24-
// Only enhance textareas that are for editing issue/PR body
25-
const isIssueBodyEdit = textarea.closest('.react-issue-body')
26-
const isPRBodyEdit =
27-
textarea.id?.match(/^issue-\d+-body$/) || textarea.name === 'pull_request[body]'
28-
29-
if (!isIssueBodyEdit && !isPRBodyEdit) {
30-
return null
31-
}
32-
3325
// Parse GitHub URL structure: /owner/repo/issues/123 or /owner/repo/pull/456
3426
const match = location.pathname.match(/^\/([^/]+)\/([^/]+)\/(?:issues|pull)\/(\d+)/)
3527
if (!match) {
3628
return null
3729
}
38-
3930
const [, owner, repo, numberStr] = match
4031
const number = parseInt(numberStr!, 10)
4132
const unique_key = `github.com:${owner}/${repo}:${number}:edit-body`
4233

34+
// Only enhance textareas that are for editing issue/PR body
35+
const isIssueBodyRootEdit = textarea.closest('.react-issue-body')
36+
const isIssueBodyCommentEdit = textarea.closest('[data-wrapper-timeline-id]')
37+
const isPRBodyEdit =
38+
textarea.name === 'pull_request[body]' || textarea.name === 'issue_comment[body]'
39+
// ^this is the root pr comment ^this is the other pr comments (surprising!)
40+
41+
if (!isIssueBodyRootEdit && !isIssueBodyCommentEdit && !isPRBodyEdit) {
42+
return null
43+
}
44+
4345
logger.debug(`${this.constructor.name} enhanced issue/PR body textarea`, unique_key)
4446
return {
47+
isIssue: !!(isIssueBodyRootEdit || isIssueBodyCommentEdit),
4548
type: GH_EDIT,
4649
unique_key,
4750
}
4851
}
4952

50-
enhance(textArea: HTMLTextAreaElement, _spot: GitHubEditSpot): OverTypeInstance {
53+
enhance(textArea: HTMLTextAreaElement, spot: GitHubEditSpot): OverTypeInstance {
5154
prepareGitHubHighlighter()
5255
const overtypeContainer = modifyDOM(textArea)
53-
return new OverType(overtypeContainer, {
56+
const overtype = new OverType(overtypeContainer, {
5457
...commonGitHubOptions,
55-
minHeight: '102px',
56-
padding: 'var(--base-size-8)',
57-
placeholder: 'Add your comment here...',
58+
padding: spot.isIssue ? 'var(--base-size-16)' : 'var(--base-size-8)',
5859
})[0]!
60+
if (!spot.isIssue) {
61+
// TODO: autoheight not working
62+
}
63+
return overtype
5964
}
6065

6166
tableUpperDecoration(_spot: GitHubEditSpot): React.ReactNode {

tests/corpus/_corpus-index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ export const CORPUS: Record<string, CorpusEntry> = {
1414
},
1515
// HAR corpus (initial page loads)
1616
gh_issue_edit: {
17-
description: 'editing an existing comment on an issue',
17+
description: 'edit an existing comment on an issue',
1818
type: 'html',
1919
url: 'https://github.com/diffplug/gitcasso/issues/56',
2020
},
21+
gh_issue_edit_multiple: {
22+
description: 'edit an existing comment on an issue (root and appended)',
23+
type: 'html',
24+
url: 'https://github.com/diffplug/testing-deletable/issues/3',
25+
},
2126
gh_issue_new: {
2227
description: 'a new issue wiht some fields filled out',
2328
type: 'html',
@@ -28,10 +33,15 @@ export const CORPUS: Record<string, CorpusEntry> = {
2833
url: 'https://github.com/diffplug/selfie/pull/517',
2934
},
3035
gh_pr_edit: {
31-
description: 'editing an existing comment on a PR',
36+
description: 'edit an existing comment on a PR',
3237
type: 'html',
3338
url: 'https://github.com/diffplug/gitcasso/pull/58',
3439
},
40+
gh_pr_edit_multiple: {
41+
description: 'edit an existing comment on a PR (root and appended)',
42+
type: 'html',
43+
url: 'https://github.com/diffplug/testing-deletable/pull/5',
44+
},
3545
gh_pr_new: {
3646
type: 'har',
3747
url: 'https://github.com/diffplug/selfie/compare/main...cavia-porcellus:selfie:main?expand=1',

tests/corpus/gh_issue_edit_multiple.html

Lines changed: 1532 additions & 0 deletions
Large diffs are not rendered by default.

tests/corpus/gh_pr_edit_multiple.html

Lines changed: 7501 additions & 0 deletions
Large diffs are not rendered by default.

tests/lib/enhancers/__snapshots__/gh-detection.test.ts.snap

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ exports[`github detection > gh_issue_edit:should detect correct spots 1`] = `
2121
{
2222
"for": "id=:rc3: name=null className=prc-Textarea-TextArea-13q4j focus-visible overtype-input",
2323
"spot": {
24+
"isIssue": true,
2425
"type": "GH_EDIT",
2526
"unique_key": "github.com:diffplug/gitcasso:56:edit-body",
2627
},
@@ -39,6 +40,38 @@ exports[`github detection > gh_issue_edit:should detect correct spots 1`] = `
3940
]
4041
`;
4142

43+
exports[`github detection > gh_issue_edit_multiple:should detect correct spots 1`] = `
44+
[
45+
{
46+
"for": "id=:r76: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
47+
"spot": {
48+
"isIssue": true,
49+
"type": "GH_EDIT",
50+
"unique_key": "github.com:diffplug/testing-deletable:3:edit-body",
51+
},
52+
},
53+
{
54+
"for": "id=:r8k: name=null className=prc-Textarea-TextArea-13q4j focus-visible overtype-input",
55+
"spot": {
56+
"isIssue": true,
57+
"type": "GH_EDIT",
58+
"unique_key": "github.com:diffplug/testing-deletable:3:edit-body",
59+
},
60+
},
61+
{
62+
"for": "id=:r5b: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
63+
"spot": {
64+
"domain": "github.com",
65+
"number": 3,
66+
"slug": "diffplug/testing-deletable",
67+
"title": "Test 3",
68+
"type": "GH_ISSUE_APPEND",
69+
"unique_key": "github.com:diffplug/testing-deletable:3",
70+
},
71+
},
72+
]
73+
`;
74+
4275
exports[`github detection > gh_issue_new:should detect correct spots 1`] = `
4376
[
4477
{
@@ -80,6 +113,7 @@ exports[`github detection > gh_pr_edit:should detect correct spots 1`] = `
80113
{
81114
"for": "id=issue-3429313834-body name=pull_request[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable CommentBox-input FormControl-textarea js-saved-reply-shortcut-comment-field focus-visible overtype-input",
82115
"spot": {
116+
"isIssue": false,
83117
"type": "GH_EDIT",
84118
"unique_key": "github.com:diffplug/gitcasso:58:edit-body",
85119
},
@@ -100,6 +134,43 @@ exports[`github detection > gh_pr_edit:should detect correct spots 1`] = `
100134
]
101135
`;
102136

137+
exports[`github detection > gh_pr_edit_multiple:should detect correct spots 1`] = `
138+
[
139+
{
140+
"for": "id=issue-3454672384-body name=pull_request[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable CommentBox-input FormControl-textarea js-saved-reply-shortcut-comment-field overtype-input",
141+
"spot": {
142+
"isIssue": false,
143+
"type": "GH_EDIT",
144+
"unique_key": "github.com:diffplug/testing-deletable:5:edit-body",
145+
},
146+
},
147+
{
148+
"for": "id=convert-to-issue-body-3335416053 name=issue[body] className=form-control input-contrast comment-form-textarea js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable",
149+
"spot": "NO_SPOT",
150+
},
151+
{
152+
"for": "id=issuecomment-3335416053-body name=issue_comment[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable CommentBox-input FormControl-textarea js-saved-reply-shortcut-comment-field focus-visible overtype-input",
153+
"spot": {
154+
"isIssue": false,
155+
"type": "GH_EDIT",
156+
"unique_key": "github.com:diffplug/testing-deletable:5:edit-body",
157+
},
158+
},
159+
{
160+
"for": "id=new_comment_field name=comment[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit FormControl-textarea CommentBox-input js-size-to-fit size-to-fit js-session-resumable js-saved-reply-shortcut-comment-field overtype-input",
161+
"spot": {
162+
"domain": "github.com",
163+
"number": 5,
164+
"slug": "diffplug/testing-deletable",
165+
"title": "Update README.md
166+
#5",
167+
"type": "GH_PR_APPEND",
168+
"unique_key": "github.com:diffplug/testing-deletable:5",
169+
},
170+
},
171+
]
172+
`;
173+
103174
exports[`github detection > gh_pr_new:should detect correct spots 1`] = `
104175
[
105176
{

tests/lib/enhancers/__snapshots__/gh-ui.test.ts.snap

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,46 @@ exports[`github ui > gh_issue_edit:should render correct UI elements 1`] = `
5959
]
6060
`;
6161

62+
exports[`github ui > gh_issue_edit_multiple:should render correct UI elements 1`] = `
63+
[
64+
{
65+
"for": "id=:r76: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
66+
"title": "N/A",
67+
"upperDecoration": <span>
68+
N/A
69+
</span>,
70+
},
71+
{
72+
"for": "id=:r8k: name=null className=prc-Textarea-TextArea-13q4j focus-visible overtype-input",
73+
"title": "N/A",
74+
"upperDecoration": <span>
75+
N/A
76+
</span>,
77+
},
78+
{
79+
"for": "id=:r5b: name=null className=prc-Textarea-TextArea-13q4j overtype-input",
80+
"title": "Test 3",
81+
"upperDecoration": <React.Fragment>
82+
<span
83+
className="flex h-4 w-4 flex-shrink-0 items-center justify-center"
84+
>
85+
<IssueOpenedIcon
86+
size={16}
87+
/>
88+
</span>
89+
#
90+
3
91+
<a
92+
className="truncate hover:underline"
93+
href="https://github.com/diffplug/testing-deletable"
94+
>
95+
diffplug/testing-deletable
96+
</a>
97+
</React.Fragment>,
98+
},
99+
]
100+
`;
101+
62102
exports[`github ui > gh_issue_new:should render correct UI elements 1`] = `
63103
[
64104
{
@@ -134,6 +174,43 @@ exports[`github ui > gh_pr_edit:should render correct UI elements 1`] = `
134174
]
135175
`;
136176

177+
exports[`github ui > gh_pr_edit_multiple:should render correct UI elements 1`] = `
178+
[
179+
{
180+
"for": "id=issue-3454672384-body name=pull_request[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable CommentBox-input FormControl-textarea js-saved-reply-shortcut-comment-field overtype-input",
181+
"title": "N/A",
182+
"upperDecoration": <span>
183+
N/A
184+
</span>,
185+
},
186+
{
187+
"for": "id=issuecomment-3335416053-body name=issue_comment[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit js-size-to-fit size-to-fit js-session-resumable CommentBox-input FormControl-textarea js-saved-reply-shortcut-comment-field focus-visible overtype-input",
188+
"title": "N/A",
189+
"upperDecoration": <span>
190+
N/A
191+
</span>,
192+
},
193+
{
194+
"for": "id=new_comment_field name=comment[body] className=js-comment-field js-paste-markdown js-task-list-field js-quick-submit FormControl-textarea CommentBox-input js-size-to-fit size-to-fit js-session-resumable js-saved-reply-shortcut-comment-field overtype-input",
195+
"title": "Update README.md
196+
#5",
197+
"upperDecoration": <React.Fragment>
198+
<span
199+
className="font-mono text-muted-foreground text-sm"
200+
>
201+
diffplug/testing-deletable
202+
</span>
203+
<span
204+
className="ml-2 font-medium"
205+
>
206+
PR #
207+
5
208+
</span>
209+
</React.Fragment>,
210+
},
211+
]
212+
`;
213+
137214
exports[`github ui > gh_pr_new:should render correct UI elements 1`] = `
138215
[
139216
{

0 commit comments

Comments
 (0)