Skip to content

Commit 94f0998

Browse files
mario-camposCopilot
andcommitted
Avoid creating resolved comments without an existing warning
Use a boolean --create-if-missing flag, supplied by the composite action by default and omitted for resolution updates. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent e965148 commit 94f0998

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ inputs:
99
body:
1010
description: "Full comment markdown."
1111
required: true
12+
create-if-missing:
13+
description: "Create a comment if no matching marker exists. Set to 'false' to update existing comments only."
14+
required: false
15+
default: "true"
1216
issue-id:
1317
description: >
1418
The issue or pull request number to post/update the comment on.
@@ -25,11 +29,13 @@ runs:
2529
env:
2630
BODY: ${{ inputs.body }}
2731
MARKER: ${{ inputs.marker }}
32+
CREATE_IF_MISSING: ${{ inputs.create-if-missing != 'false' && '--create-if-missing' || '' }}
2833
GH_TOKEN: ${{ inputs.token }}
2934
ISSUE_ID: ${{ inputs.issue-id || github.event.pull_request.number }}
3035
run: |
3136
npx tsx ./pr-checks/post-comment.ts \
3237
--body "$BODY" \
3338
--marker "$MARKER" \
39+
$CREATE_IF_MISSING \
3440
--issue-id "$ISSUE_ID" \
3541
--repository "$GITHUB_REPOSITORY"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ jobs:
313313
uses: ./.github/actions/post-comment
314314
with:
315315
marker: "post-comment:CHANGELOG.md"
316+
create-if-missing: "false"
316317
body: |
317318
⚠️ `CHANGELOG.md` has been modified in this PR. Please do not modify `CHANGELOG.md` directly.
318319
Instead, create a change-note file in `unreleased-change-notes/`. The `CHANGELOG.md`

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ describe("parseOptions", async () => {
4949
assert.deepEqual(options, {
5050
body: "hello",
5151
marker: "marker",
52+
createIfMissing: false,
5253
issueId: 42,
5354
repository: { owner: "github", repo: "codeql-action" },
5455
});
5556
});
5657

58+
await it("enables creation when --create-if-missing is supplied", () => {
59+
const options = parseOptions([...validFlags, "--create-if-missing"]);
60+
assert.equal(options.createIfMissing, true);
61+
});
62+
5763
await it("rejects a missing --body", () => {
5864
assert.throws(
5965
() => parseOptions(withoutFlag("--body")),
@@ -206,6 +212,7 @@ describe("findExistingComment", async () => {
206212
const baseOptions: Options = {
207213
body: "hello",
208214
marker: "marker",
215+
createIfMissing: true,
209216
issueId: 42,
210217
repository: { owner: "owner", repo: "repo" },
211218
};
@@ -220,6 +227,25 @@ function assertCommentContent(
220227
}
221228

222229
describe("setComment", async () => {
230+
await it("does not create a resolved comment when no comments exist", async () => {
231+
const { client, comments } = fakeClient([]);
232+
233+
await setComment(client, { ...baseOptions, createIfMissing: false });
234+
235+
// Resolution does not introduce a warning that was never posted.
236+
assert.deepEqual(comments, []);
237+
});
238+
239+
await it("leaves unrelated comments unchanged when creation is disabled", async () => {
240+
const originalComments = [{ id: 7, body: "unrelated" }];
241+
const { client, comments } = fakeClient(originalComments);
242+
243+
await setComment(client, { ...baseOptions, createIfMissing: false });
244+
245+
// Unrelated comments neither trigger resolution nor get overwritten.
246+
assert.deepEqual(comments, originalComments);
247+
});
248+
223249
await it("uses the supplied marker without affecting other comments", async () => {
224250
const marker = "custom-warning";
225251
const { client, comments } = fakeClient([
@@ -308,7 +334,7 @@ describe("setComment", async () => {
308334
const body =
309335
"`CHANGELOG.md` is not modified in this PR. No changelog action is needed.";
310336

311-
await setComment(client, { ...baseOptions, body });
337+
await setComment(client, { ...baseOptions, body, createIfMissing: false });
312338

313339
// The resolved state replaces the warning on the same marked comment.
314340
assert.equal(comments.length, 1);

‎pr-checks/post-comment.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ 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)
66
and must be non-empty. Supply a plain-text identifier; the CLI wraps it in a
77
hidden HTML comment, prepends it to `--body`, and uses it to find the comment on later runs.
8+
Supply `--create-if-missing` to allow creation; omit it to update existing comments only.
89
910
Example:
1011
```sh
1112
GH_TOKEN=<token> npx tsx ./pr-checks/post-comment.ts \
13+
--create-if-missing \
1214
--body "The changelog needs updating." \
1315
--marker "my-bot" \
1416
--issue-id 123 \
@@ -29,6 +31,8 @@ export interface Options {
2931
body: string;
3032
/** Plain-text identifier wrapped in a hidden HTML comment. */
3133
marker: string;
34+
/** Whether to create a comment when no matching marker exists. */
35+
createIfMissing: boolean;
3236
/** The issue/pull request number to post/update the comment on. */
3337
issueId: number;
3438
/** The repository to operate on. */
@@ -46,6 +50,7 @@ export function parseOptions(argv?: string[]): Options {
4650
options: {
4751
body: { type: "string" },
4852
marker: { type: "string" },
53+
"create-if-missing": { type: "boolean", default: false },
4954
"issue-id": { type: "string" },
5055
repository: { type: "string" },
5156
},
@@ -76,6 +81,7 @@ export function parseOptions(argv?: string[]): Options {
7681
return {
7782
body: values.body,
7883
marker: values.marker,
84+
createIfMissing: values["create-if-missing"],
7985
issueId: Number(values["issue-id"]),
8086
repository: { owner, repo },
8187
};
@@ -126,6 +132,10 @@ export async function setComment(
126132
marker,
127133
);
128134
if (existingCommentId === undefined) {
135+
if (!options.createIfMissing) {
136+
console.info("No existing comment found; skipping creation.");
137+
return;
138+
}
129139
console.info("No existing comment found; creating a new comment.");
130140
await client.rest.issues.createComment({
131141
...repository,

0 commit comments

Comments
 (0)