Skip to content

Commit 70da3d1

Browse files
cknittclaude
andauthored
Comment on pull requests with an action of our own (#9275)
thollander/actions-comment-pull-request declares Node 20, so every run that comments shows a deprecation warning, and it hasn't been released since 2024. The benchmarks, semantic diff and playground preview comments now use .github/actions/upsert-comment, on actions/github-script, which finds the workflow's comment by a marker of its own and edits or creates it. Review: What happens to the existing comments? They carry thollander's marker, which the new action doesn't look for, so each open pull request gets one new comment per kind the next time it runs; the old ones stay. Claude-Session: https://claude.ai/code/session_01Kbf3gGPsF3fHZn9rBxA7Hm Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b82d944 commit 70da3d1

8 files changed

Lines changed: 295 additions & 12 deletions

File tree

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
name: Upsert pull request comment
2+
description: >-
3+
Keep one comment of the workflow's on a pull request, found by its tag:
4+
edit it in place, or create it.
5+
6+
inputs:
7+
pr-number:
8+
description: The pull request to comment on.
9+
required: true
10+
tag:
11+
description: >-
12+
Names the comment, so that each run edits the same one. Letters, digits,
13+
`_`, `.` and `-`.
14+
required: true
15+
body:
16+
description: The comment's Markdown. Give either this or `body-path`.
17+
required: false
18+
body-path:
19+
description: A file with the comment's Markdown.
20+
required: false
21+
22+
runs:
23+
using: composite
24+
steps:
25+
- name: Upsert the comment
26+
uses: actions/github-script@v9
27+
# The inputs reach the script as environment variables, never as part
28+
# of its source: a fork's pull request may have written the body.
29+
env:
30+
UPSERT_ACTION_PATH: ${{ github.action_path }}
31+
UPSERT_PR_NUMBER: ${{ inputs.pr-number }}
32+
UPSERT_TAG: ${{ inputs.tag }}
33+
UPSERT_BODY: ${{ inputs.body }}
34+
UPSERT_BODY_PATH: ${{ inputs.body-path }}
35+
with:
36+
script: |
37+
const fs = require("node:fs");
38+
const path = require("node:path");
39+
const { upsertComment } = require(
40+
path.join(process.env.UPSERT_ACTION_PATH, "upsertComment.cjs"),
41+
);
42+
const { UPSERT_BODY: body, UPSERT_BODY_PATH: bodyPath } = process.env;
43+
if (Boolean(body) === Boolean(bodyPath)) {
44+
throw new Error("Give either body or body-path.");
45+
}
46+
const text = bodyPath ? fs.readFileSync(bodyPath, "utf8") : body;
47+
const result = await upsertComment(
48+
{
49+
listComments: params =>
50+
github.paginate(github.rest.issues.listComments, {
51+
...params,
52+
per_page: 100,
53+
}),
54+
createComment: params => github.rest.issues.createComment(params),
55+
updateComment: params => github.rest.issues.updateComment(params),
56+
},
57+
{
58+
owner: context.repo.owner,
59+
repo: context.repo.repo,
60+
issueNumber: Number(process.env.UPSERT_PR_NUMBER),
61+
tag: process.env.UPSERT_TAG,
62+
text,
63+
// The GITHUB_TOKEN comments as this user.
64+
author: "github-actions[bot]",
65+
},
66+
);
67+
core.info(`Comment ${process.env.UPSERT_TAG}: ${result}.`);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// @ts-check
2+
3+
// Keeps one comment of the workflow's on a pull request, found by a hidden
4+
// marker with its tag: edits it in place when its body changed, or creates
5+
// it. Edits notify no one, so a workflow can update its comment on every
6+
// push. Run by the upsert-comment action through actions/github-script.
7+
8+
/**
9+
* The marker that ends a comment with this tag.
10+
*
11+
* @param {string} tag
12+
*/
13+
const markerOf = tag =>
14+
`<!-- rescript-lang/rescript upsert-comment "${tag}" -->`;
15+
16+
/**
17+
* The comment's whole body: the text, then the marker on a line of its own.
18+
*
19+
* @param {string} text
20+
* @param {string} tag
21+
*/
22+
const bodyOf = (text, tag) => `${text.trimEnd()}\n\n${markerOf(tag)}\n`;
23+
24+
/**
25+
* @typedef {{ id: number, body?: string, user: { login: string } | null }} Comment
26+
* @typedef {{
27+
* listComments: (params: { owner: string, repo: string, issue_number: number }) => Promise<Comment[]>,
28+
* createComment: (params: { owner: string, repo: string, issue_number: number, body: string }) => Promise<unknown>,
29+
* updateComment: (params: { owner: string, repo: string, comment_id: number, body: string }) => Promise<unknown>,
30+
* }} Client
31+
*/
32+
33+
/**
34+
* Creates or updates the pull request's comment with this tag. Only comments
35+
* by `author` count, so that a person quoting the marker doesn't get their
36+
* comment overwritten. Returns what it did.
37+
*
38+
* @param {Client} client
39+
* @param {{
40+
* owner: string,
41+
* repo: string,
42+
* issueNumber: number,
43+
* tag: string,
44+
* text: string,
45+
* author: string,
46+
* }} options
47+
* @returns {Promise<"created" | "updated" | "unchanged">}
48+
*/
49+
async function upsertComment(
50+
client,
51+
{ owner, repo, issueNumber, tag, text, author },
52+
) {
53+
if (!/^[\w.-]+$/.test(tag)) {
54+
throw new Error(`The comment tag ${JSON.stringify(tag)} isn't a word.`);
55+
}
56+
if (!Number.isInteger(issueNumber) || issueNumber <= 0) {
57+
throw new Error(`${issueNumber} isn't a pull request number.`);
58+
}
59+
const marker = markerOf(tag);
60+
const body = bodyOf(text, tag);
61+
const comments = await client.listComments({
62+
owner,
63+
repo,
64+
issue_number: issueNumber,
65+
});
66+
const existing = comments.find(
67+
comment =>
68+
comment.user?.login === author &&
69+
(comment.body ?? "").trimEnd().endsWith(marker),
70+
);
71+
if (!existing) {
72+
await client.createComment({
73+
owner,
74+
repo,
75+
issue_number: issueNumber,
76+
body,
77+
});
78+
return "created";
79+
}
80+
if (existing.body === body) return "unchanged";
81+
await client.updateComment({ owner, repo, comment_id: existing.id, body });
82+
return "updated";
83+
}
84+
85+
module.exports = { upsertComment, markerOf, bodyOf };
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
import { bodyOf, markerOf, upsertComment } from "./upsertComment.cjs";
4+
5+
const bot = "github-actions[bot]";
6+
7+
/**
8+
* A fake of the comments API that records the calls that change comments.
9+
*
10+
* @param {{ id: number, body?: string, user: { login: string } | null }[]} comments
11+
*/
12+
function fakeClient(comments) {
13+
/** @type {unknown[]} */
14+
const calls = [];
15+
return {
16+
calls,
17+
listComments: async () => comments,
18+
createComment: async (/** @type {any} */ params) => {
19+
calls.push({ create: params });
20+
},
21+
updateComment: async (/** @type {any} */ params) => {
22+
calls.push({ update: params });
23+
},
24+
};
25+
}
26+
27+
const options = {
28+
owner: "rescript-lang",
29+
repo: "rescript",
30+
issueNumber: 42,
31+
tag: "compiler-benchmarks",
32+
text: "## Report\n\nAll good.\n\n",
33+
author: bot,
34+
};
35+
36+
test("creates the comment when there is none", async () => {
37+
const client = fakeClient([
38+
{ id: 1, body: "Looks good to me.", user: { login: "someone" } },
39+
]);
40+
assert.equal(await upsertComment(client, options), "created");
41+
assert.deepEqual(client.calls, [
42+
{
43+
create: {
44+
owner: "rescript-lang",
45+
repo: "rescript",
46+
issue_number: 42,
47+
body: `## Report\n\nAll good.\n\n${markerOf("compiler-benchmarks")}\n`,
48+
},
49+
},
50+
]);
51+
});
52+
53+
test("updates the workflow's comment with the same tag", async () => {
54+
const client = fakeClient([
55+
{ id: 1, body: bodyOf("Old", "semantic-diff"), user: { login: bot } },
56+
{ id: 2, body: bodyOf("Old", "compiler-benchmarks"), user: { login: bot } },
57+
]);
58+
assert.equal(await upsertComment(client, options), "updated");
59+
assert.deepEqual(client.calls, [
60+
{
61+
update: {
62+
owner: "rescript-lang",
63+
repo: "rescript",
64+
comment_id: 2,
65+
body: bodyOf(options.text, "compiler-benchmarks"),
66+
},
67+
},
68+
]);
69+
});
70+
71+
test("leaves an unchanged comment alone", async () => {
72+
const client = fakeClient([
73+
{
74+
id: 2,
75+
body: bodyOf(options.text, "compiler-benchmarks"),
76+
user: { login: bot },
77+
},
78+
]);
79+
assert.equal(await upsertComment(client, options), "unchanged");
80+
assert.deepEqual(client.calls, []);
81+
});
82+
83+
test("ignores a person's comment that quotes the marker", async () => {
84+
const client = fakeClient([
85+
{ id: 3, body: bodyOf("Quoted", "compiler-benchmarks"), user: null },
86+
{
87+
id: 4,
88+
body: bodyOf("Quoted", "compiler-benchmarks"),
89+
user: { login: "someone" },
90+
},
91+
]);
92+
assert.equal(await upsertComment(client, options), "created");
93+
});
94+
95+
test("ignores a tag that only starts like this one", async () => {
96+
const client = fakeClient([
97+
{
98+
id: 5,
99+
body: bodyOf("Old", "compiler-benchmarks-old"),
100+
user: { login: bot },
101+
},
102+
]);
103+
assert.equal(await upsertComment(client, options), "created");
104+
});
105+
106+
test("rejects tags that aren't words and missing pull request numbers", async () => {
107+
const client = fakeClient([]);
108+
await assert.rejects(
109+
upsertComment(client, { ...options, tag: 'a" -->' }),
110+
/isn't a word/,
111+
);
112+
await assert.rejects(
113+
upsertComment(client, { ...options, issueNumber: Number("") }),
114+
/isn't a pull request number/,
115+
);
116+
assert.deepEqual(client.calls, []);
117+
});

‎.github/workflows/ci.yml‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ jobs:
184184
if: matrix.test_commit_records
185185
run: yarn test-commit-records
186186

187+
- name: Test the pull request comment action
188+
if: matrix.test_commit_records
189+
run: yarn test-upsert-comment
190+
187191
- name: Run tests
188192
run: node scripts/test.js -all
189193

@@ -496,12 +500,11 @@ jobs:
496500
if: >-
497501
!cancelled() &&
498502
github.event.pull_request.head.repo.full_name == github.repository
499-
uses: thollander/actions-comment-pull-request@v3
503+
uses: ./.github/actions/upsert-comment
500504
with:
501505
pr-number: ${{ github.event.pull_request.number }}
502-
comment-tag: compiler-benchmarks
503-
mode: upsert
504-
file-path: ${{ runner.temp }}/benchmarks-report.md
506+
tag: compiler-benchmarks
507+
body-path: ${{ runner.temp }}/benchmarks-report.md
505508

506509
dev-playground:
507510
needs:

‎.github/workflows/playground_preview_upload.yml‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ jobs:
1616
env:
1717
PLAYGROUND_PREVIEW_ID: pr-${{ github.event.workflow_run.pull_requests[0].number }}
1818
steps:
19+
# For the comment action only; on workflow_run this is the default
20+
# branch, not the pull request. First, since a checkout empties the
21+
# directory.
22+
- name: Checkout comment action
23+
uses: actions/checkout@v7
24+
with:
25+
sparse-checkout: .github/actions
26+
persist-credentials: false
27+
1928
- name: Download preview bundle artifact
2029
uses: actions/download-artifact@v8
2130
with:
@@ -52,9 +61,9 @@ jobs:
5261
"rescript:cdn-assets/dev-playground-bundles/${PLAYGROUND_PREVIEW_ID}/bundle"
5362
5463
- name: Comment playground preview URL
55-
uses: thollander/actions-comment-pull-request@v3
64+
uses: ./.github/actions/upsert-comment
5665
with:
5766
pr-number: ${{ github.event.workflow_run.pull_requests[0].number }}
58-
comment-tag: dev-playground-preview
59-
message: |
67+
tag: dev-playground-preview
68+
body: |
6069
Developer playground preview: https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/dev-playground/?version=pr-${{ github.event.workflow_run.pull_requests[0].number }}

‎.github/workflows/semantic_diff.yml‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,13 @@ jobs:
101101
run: rclone copyto --s3-no-check-bucket semantic-diff/index.html "rescript:cdn-assets/semantic-diff/pr-${PR_NUMBER}/index.html"
102102

103103
- name: Comment semantic diff
104-
uses: thollander/actions-comment-pull-request@v3
104+
uses: ./.github/actions/upsert-comment
105105
with:
106106
pr-number: ${{ github.event.pull_request.number }}
107107
# The pull request's one comment, edited in place on each push;
108108
# edits notify no one.
109-
comment-tag: semantic-diff
110-
mode: upsert
111-
file-path: semantic-diff/comment.md
109+
tag: semantic-diff
110+
body-path: semantic-diff/comment.md
112111

113112
# Last, so that the page and the comment show the records either way.
114113
- name: Check commit descriptions

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@
6363
"changelog:release": "node scripts/changelog.js release",
6464
"changelog:open": "node scripts/changelog.js open",
6565
"test-changelog": "node --test scripts/changelog.test.js scripts/magicNumbers.test.js",
66-
"test-commit-records": "node --test scripts/commitRecords.test.js scripts/recordMarkdown.test.js"
66+
"test-commit-records": "node --test scripts/commitRecords.test.js scripts/recordMarkdown.test.js",
67+
"test-upsert-comment": "node --test .github/actions/upsert-comment/upsertComment.test.js"
6768
},
6869
"files": [
6970
"CHANGELOG.md",

‎tsconfig.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"packages/**/*",
2020
"tests/**/input.js",
2121
".github/workflows/*.js",
22+
".github/actions/**/*.js",
23+
".github/actions/**/*.cjs",
2224
"yarn.config.cjs",
2325
"package.json"
2426
],

0 commit comments

Comments
 (0)