Skip to content

Commit 9359b6b

Browse files
committed
Wrap --marker argument in HTML comment tags automatically
1 parent 19e1ab5 commit 9359b6b

4 files changed

Lines changed: 31 additions & 26 deletions

File tree

‎.github/actions/post-comment/action.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ description: "Create or update a sticky PR comment identified by a caller-provid
33
inputs:
44
marker:
55
description: >
6-
Non-empty marker identifying the comment. Use a unique,
7-
stable HTML comment for each distinct comment.
6+
Non-empty plain-text identifier for the comment. Use a unique, stable value
7+
for each distinct comment; it is automatically wrapped in a hidden HTML comment.
88
required: true
99
body:
1010
description: "Full comment markdown."

‎.github/workflows/pr-checks.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ jobs:
295295
!startsWith(github.event.pull_request.head.ref, 'update-bundle/')
296296
uses: ./.github/actions/post-comment
297297
with:
298-
marker: "<!-- post-comment:CHANGELOG.md -->"
298+
marker: "post-comment:CHANGELOG.md"
299299
body: |
300300
⚠️ `CHANGELOG.md` has been modified in this PR. Please do not modify `CHANGELOG.md` directly.
301301
Instead, create a change-note file in `unreleased-change-notes/`. The `CHANGELOG.md`
@@ -312,7 +312,7 @@ jobs:
312312
!startsWith(github.event.pull_request.head.ref, 'update-bundle/')
313313
uses: ./.github/actions/post-comment
314314
with:
315-
marker: "<!-- post-comment:CHANGELOG.md -->"
315+
marker: "post-comment:CHANGELOG.md"
316316
body: |
317317
⚠️ `CHANGELOG.md` has been modified in this PR. Please do not modify `CHANGELOG.md` directly.
318318
Instead, create a change-note file in `unreleased-change-notes/`. The `CHANGELOG.md`

‎pr-checks/post-comment.test.ts‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const validFlags = [
2020
"--body",
2121
"hello",
2222
"--marker",
23-
"<!-- marker -->",
23+
"marker",
2424
"--issue-id",
2525
"42",
2626
"--repository",
@@ -48,7 +48,7 @@ describe("parseOptions", async () => {
4848
const options = parseOptions(validFlags);
4949
assert.deepEqual(options, {
5050
body: "hello",
51-
marker: "<!-- marker -->",
51+
marker: "marker",
5252
issueId: 42,
5353
repository: { owner: "github", repo: "codeql-action" },
5454
});
@@ -136,7 +136,7 @@ describe("findExistingComment", async () => {
136136
client,
137137
{ owner: "owner", repo: "repo" },
138138
1,
139-
"<!-- marker -->",
139+
"marker",
140140
);
141141
assert.equal(id, 2);
142142
});
@@ -147,7 +147,7 @@ describe("findExistingComment", async () => {
147147
client,
148148
{ owner: "owner", repo: "repo" },
149149
1,
150-
"<!-- marker -->",
150+
"marker",
151151
);
152152
assert.equal(id, undefined);
153153
});
@@ -156,7 +156,7 @@ describe("findExistingComment", async () => {
156156
/** A baseline set of `Options`, for use in `setComment` tests. */
157157
const baseOptions: Options = {
158158
body: "hello",
159-
marker: "<!-- marker -->",
159+
marker: "marker",
160160
issueId: 42,
161161
repository: { owner: "owner", repo: "repo" },
162162
};
@@ -166,16 +166,16 @@ function assertCommentContent(
166166
marker: string,
167167
body: string,
168168
): void {
169-
assert.ok(comment.body?.startsWith(marker));
169+
assert.ok(comment.body?.startsWith(`<!-- ${marker} -->`));
170170
assert.ok(comment.body?.endsWith(body));
171171
}
172172

173173
describe("setComment", async () => {
174174
await it("uses the supplied marker without affecting other comments", async () => {
175-
const marker = "<!-- custom-warning -->";
175+
const marker = "custom-warning";
176176
const { client, comments } = fakeClient([
177-
{ id: 1, body: `${baseOptions.marker}\nunrelated warning` },
178-
{ id: 2, body: `${marker}\nold warning` },
177+
{ id: 1, body: "<!-- marker -->\nunrelated warning" },
178+
{ id: 2, body: "<!-- custom-warning -->\nold warning" },
179179
]);
180180
const options = parseOptions(withFlag("--marker", marker));
181181

@@ -185,7 +185,7 @@ describe("setComment", async () => {
185185
assert.equal(comments.length, 2);
186186
assert.deepEqual(comments[0], {
187187
id: 1,
188-
body: `${baseOptions.marker}\nunrelated warning`,
188+
body: "<!-- marker -->\nunrelated warning",
189189
});
190190
assert.equal(comments[1].id, 2);
191191
assertCommentContent(comments[1], marker, options.body);
@@ -196,7 +196,7 @@ describe("setComment", async () => {
196196
// No comment has the marker before posting.
197197
assert.equal(comments.length, 0);
198198
assert.ok(
199-
!comments.some(({ body }) => body?.startsWith(baseOptions.marker)),
199+
!comments.some(({ body }) => body?.startsWith("<!-- marker -->")),
200200
);
201201

202202
await setComment(client, baseOptions);
@@ -214,7 +214,7 @@ describe("setComment", async () => {
214214
const { client, comments } = fakeClient(originalComments);
215215
// Existing comments do not match the marker.
216216
assert.ok(
217-
!comments.some(({ body }) => body?.startsWith(baseOptions.marker)),
217+
!comments.some(({ body }) => body?.startsWith("<!-- marker -->")),
218218
);
219219

220220
await setComment(client, baseOptions);
@@ -226,7 +226,7 @@ describe("setComment", async () => {
226226
originalComments,
227227
);
228228
const markedComments = comments.filter(({ body }) =>
229-
body?.startsWith(baseOptions.marker),
229+
body?.startsWith("<!-- marker -->"),
230230
);
231231
// Exactly one comment contains the expected marker and body.
232232
assert.equal(markedComments.length, 1);
@@ -241,7 +241,7 @@ describe("setComment", async () => {
241241

242242
await it("updates the existing comment when one is found", async () => {
243243
const { client, comments } = fakeClient([
244-
{ id: 7, body: `${baseOptions.marker}\nold` },
244+
{ id: 7, body: "<!-- marker -->\nold" },
245245
]);
246246

247247
await setComment(client, baseOptions);
@@ -254,7 +254,7 @@ describe("setComment", async () => {
254254

255255
await it("replaces a warning with the resolved state using the same marker", async () => {
256256
const { client, comments } = fakeClient([
257-
{ id: 9, body: `${baseOptions.marker}\nold` },
257+
{ id: 9, body: "<!-- marker -->\nold" },
258258
]);
259259
const body =
260260
"`CHANGELOG.md` is not modified in this PR. No changelog action is needed.";

‎pr-checks/post-comment.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
/*
44
Creates or updates a pull request comment identified by a marker. The marker
55
is supplied via `--marker` (or the `.github/actions/post-comment` action's `marker` input)
6-
and must be non-empty. The marker is prepended to `--body` and used to find
7-
the comment on later runs.
6+
and must be non-empty. Supply a plain-text identifier; the CLI wraps it in a
7+
hidden HTML comment, prepends it to `--body`, and uses it to find the comment on later runs.
88
99
Example:
1010
```sh
1111
GH_TOKEN=<token> npx tsx ./pr-checks/post-comment.ts \
1212
--body "The changelog needs updating." \
13-
--marker "<!-- my-bot -->" \
13+
--marker "my-bot" \
1414
--issue-id 123 \
1515
--repository github/codeql-action
1616
```
@@ -27,14 +27,18 @@ import { type ApiClient, getApiClient, resolveToken } from "./api-client";
2727
export interface Options {
2828
/** The full comment body. */
2929
body: string;
30-
/** The hidden HTML-comment marker used to find a pre-existing comment. */
30+
/** Plain-text identifier wrapped in a hidden HTML comment. */
3131
marker: string;
3232
/** The issue/pull request number to post/update the comment on. */
3333
issueId: number;
3434
/** The repository to operate on. */
3535
repository: RepositoryNwo;
3636
}
3737

38+
function hiddenMarker(marker: string): string {
39+
return `<!-- ${marker} -->`;
40+
}
41+
3842
/** Parses CLI arguments supplied by the composite action into `Options`. */
3943
export function parseOptions(argv?: string[]): Options {
4044
const { values } = parseArgs({
@@ -72,17 +76,18 @@ export async function findExistingComment(
7276
issueNumber: number,
7377
marker: string,
7478
): Promise<number | undefined> {
79+
const formattedMarker = hiddenMarker(marker);
7580
const comments = await client.paginate(client.rest.issues.listComments, {
7681
...repository,
7782
issue_number: issueNumber,
7883
per_page: 100,
7984
});
8085
const markedComments = comments.filter((comment) =>
81-
comment.body?.startsWith(marker),
86+
comment.body?.startsWith(formattedMarker),
8287
);
8388
if (markedComments.length > 1) {
8489
console.warn(
85-
`Found ${markedComments.length} comments with marker "${marker}". ` +
90+
`Found ${markedComments.length} comments with marker "${formattedMarker}". ` +
8691
"Updating the first one and leaving the others unchanged." +
8792
"Use a unique marker for each distinct managed comment to avoid this situation.",
8893
);
@@ -98,7 +103,7 @@ export async function setComment(
98103
options: Options,
99104
): Promise<void> {
100105
const { issueId, marker, repository } = options;
101-
const body = `${marker}\n${options.body}`;
106+
const body = `${hiddenMarker(marker)}\n${options.body}`;
102107

103108
const existingCommentId = await findExistingComment(
104109
client,

0 commit comments

Comments
 (0)