Replace --create-if-missing to --update-only

`--update-only` is disabled by default, whereas `--create-if-missing` should've been enabled by default but was not.
This commit is contained in:
Mario Campos
2026-10-30 10:41:05 -05:00
parent 9de5eacd23
commit d71593936c
4 changed files with 24 additions and 27 deletions

View File

@@ -9,12 +9,10 @@ inputs:
body:
description: "Full comment markdown."
required: true
create-if-missing:
description: >
Create a comment if no matching marker exists.
Set to 'false' to update existing comments only.
update-only:
description: Do nothing if no comment with the specified marker exists.
required: false
default: "true"
default: "false"
issue-id:
description: >
The issue or pull request number to post/update the comment on.
@@ -31,13 +29,13 @@ runs:
env:
BODY: ${{ inputs.body }}
MARKER: ${{ inputs.marker }}
CREATE_IF_MISSING: ${{ (inputs.create-if-missing == 'true' && '--create-if-missing') || '' }}
UPDATE_ONLY: ${{ (inputs.update-only == 'true' && '--update-only') || '' }}
GH_TOKEN: ${{ inputs.token }}
ISSUE_ID: ${{ inputs.issue-id || github.event.pull_request.number }}
run: |
npx tsx ./pr-checks/post-comment.ts \
--body "$BODY" \
--marker "$MARKER" \
$CREATE_IF_MISSING \
$UPDATE_ONLY \
--issue-id "$ISSUE_ID" \
--repository "$GITHUB_REPOSITORY"

View File

@@ -307,7 +307,7 @@ jobs:
- name: Mark the CHANGELOG.md concern as resolved
if: >-
needs.other-checks.outputs.changelog-modified != 'true' &&
needs.other-checks.outputs.changelog-modified == 'false' &&
!startsWith(github.event.pull_request.head.ref, 'update-v') &&
!startsWith(github.event.pull_request.head.ref, 'backport-v') &&
!startsWith(github.event.pull_request.head.ref, 'mergeback/') &&
@@ -315,7 +315,7 @@ jobs:
uses: ./.github/actions/post-comment
with:
marker: "post-comment:CHANGELOG.md"
create-if-missing: "false"
update-only: true
body: |
⚠️ `CHANGELOG.md` has been modified in this PR. Please do not modify `CHANGELOG.md` directly.
Instead, create a change-note file in `unreleased-change-notes/`. The `CHANGELOG.md`

View File

@@ -49,15 +49,15 @@ describe("parseOptions", async () => {
assert.deepEqual(options, {
body: "hello",
marker: "marker",
createIfMissing: false,
updateOnly: false,
issueId: 42,
repository: { owner: "github", repo: "codeql-action" },
});
});
await it("enables creation when --create-if-missing is supplied", () => {
const options = parseOptions([...validFlags, "--create-if-missing"]);
assert.equal(options.createIfMissing, true);
await it("enables update-only mode when --update-only is supplied", () => {
const options = parseOptions([...validFlags, "--update-only"]);
assert.equal(options.updateOnly, true);
});
await it("rejects a missing --body", () => {
@@ -212,7 +212,7 @@ describe("findExistingComment", async () => {
const baseOptions: Options = {
body: "hello",
marker: "marker",
createIfMissing: true,
updateOnly: false,
issueId: 42,
repository: { owner: "owner", repo: "repo" },
};
@@ -227,20 +227,20 @@ function assertCommentContent(
}
describe("setComment", async () => {
await it("does not create a resolved comment when no comments exist", async () => {
await it("does not create a comment in update-only mode when none exists", async () => {
const { client, comments } = fakeClient([]);
await setComment(client, { ...baseOptions, createIfMissing: false });
await setComment(client, { ...baseOptions, updateOnly: true });
// Resolution does not introduce a warning that was never posted.
assert.deepEqual(comments, []);
});
await it("leaves unrelated comments unchanged when creation is disabled", async () => {
await it("leaves unrelated comments unchanged in update-only mode", async () => {
const originalComments = [{ id: 7, body: "unrelated" }];
const { client, comments } = fakeClient(originalComments);
await setComment(client, { ...baseOptions, createIfMissing: false });
await setComment(client, { ...baseOptions, updateOnly: true });
// Unrelated comments neither trigger resolution nor get overwritten.
assert.deepEqual(comments, originalComments);
@@ -334,7 +334,7 @@ describe("setComment", async () => {
const body =
"`CHANGELOG.md` is not modified in this PR. No changelog action is needed.";
await setComment(client, { ...baseOptions, body, createIfMissing: false });
await setComment(client, { ...baseOptions, body, updateOnly: true });
// The resolved state replaces the warning on the same marked comment.
assert.equal(comments.length, 1);

View File

@@ -5,12 +5,11 @@ Creates or updates a pull request comment identified by a marker. The marker
is supplied via `--marker` (or the `.github/actions/post-comment` action's `marker` input)
and must be non-empty. Supply a plain-text identifier; the CLI wraps it in a
hidden HTML comment, prepends it to `--body`, and uses it to find the comment on later runs.
Supply `--create-if-missing` to allow creation; omit it to update existing comments only.
Supply `--update-only` to suppress creation; omit it to update or create comments.
Example:
```sh
GH_TOKEN=<token> npx tsx ./pr-checks/post-comment.ts \
--create-if-missing \
--body "The changelog needs updating." \
--marker "my-bot" \
--issue-id 123 \
@@ -31,8 +30,8 @@ export interface Options {
body: string;
/** Plain-text identifier wrapped in a hidden HTML comment. */
marker: string;
/** Whether to create a comment when no matching marker exists. */
createIfMissing: boolean;
/** Whether to only update a comment if a matching marker exists. */
updateOnly: boolean;
/** The issue/pull request number to post/update the comment on. */
issueId: number;
/** The repository to operate on. */
@@ -50,7 +49,7 @@ export function parseOptions(argv?: string[]): Options {
options: {
body: { type: "string" },
marker: { type: "string" },
"create-if-missing": { type: "boolean", default: false },
"update-only": { type: "boolean", default: false },
"issue-id": { type: "string" },
repository: { type: "string" },
},
@@ -80,7 +79,7 @@ export function parseOptions(argv?: string[]): Options {
return {
body: values.body,
marker: values.marker,
createIfMissing: values["create-if-missing"],
updateOnly: values["update-only"],
issueId: Number(values["issue-id"]),
repository: parseRepositoryNwo(values.repository),
};
@@ -131,9 +130,9 @@ export async function setComment(
marker,
);
if (existingCommentId === undefined) {
if (!options.createIfMissing) {
if (options.updateOnly) {
console.info(
"No existing comment found; skipping creation because --create-if-missing wasn't specified.",
"No existing comment found; skipping creation because --update-only was specified.",
);
return;
}