Repository navigation
Add post-comment Action
#4217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add post-comment Action
#4217
Changes from 15 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
b2b77a8
Add CHANGELOG.md modification check and warning comment
mario-campos fbd33ac
Gate comment-post step on successful fetch-base
mario-campos 43ac64c
Use `gh api` instead of third-party Action
mario-campos f7aa9dc
Move post-comment step to `post-pr-comments` job
mario-campos 780e947
Skip CHANGELOG.md modification warning for release and backport PRs
mario-campos ae75d46
Exclude "Mergeback" and "Update default bundle" PRs from CHANGELOG.md…
mario-campos 6e4d3fb
Use a sticky comment to warn about CHANGELOG.md changes
mario-campos d97a656
Update CHANGELOG.md warning message to reference change-notes directory
mario-campos ccdf690
Escape backticks PR comment body
mario-campos 928548a
Refine exclusion conditions for CHANGELOG-modification comment
mario-campos 0707124
Delete CHANGELOG.md-modification comment if CHANGELOG.md no longer mo…
mario-campos 27d37d9
Format all file references as inline Markdown code spans
mario-campos 9fe741e
Simplify `if` condition of CHANGELOG-comment-post step
mario-campos 8c8b082
Add script and Action for managing PR comments
mario-campos 14cefaa
Replace bash/run step with local Action `post-comment`
mario-campos 4d464b0
Improve issue ID parsing and validation logic
mario-campos 3c452a1
Use HTTP link to `unreleased-change-notes/README.md`
mario-campos f1419b8
Reject empty `--marker`
mario-campos e7dadca
Refactor comment handling logic into `performAction` for improved tes…
mario-campos ae9f97b
Make post-comment marker configurable
mario-campos 90d1bc9
Remove bespoke post-comment argument validation
mario-campos ae23865
Move comment branching into workflow steps
mario-campos 9448877
Simplify post-comment to a single set-comment mode
mario-campos 03137fc
Rename changelog outputs to modified
mario-campos 36f220a
Remove unused post-comment step IDs
mario-campos c069d49
Test sticky comments using observable state
mario-campos afaf324
Move `resolveToken` to `pr-checks/api-client.ts`
mario-campos 3fb676f
Use existing `RepositoryNwo` type
mario-campos 56fa196
Use YAML multiline string for the Action's inputs' descriptions
mario-campos 2902eea
Checkout `main` instead of PR's base for the `post-pr-comments` job
mario-campos 87cac24
Avoid sparse checkout
mario-campos 5bb3554
Make input-validation assertions more granular
mario-campos f13bcf3
Warn if more than one marked comment was found
mario-campos 19e1ab5
Update `pr-checks/post-comment.ts` description comment
mario-campos 9359b6b
Wrap `--marker` argument in HTML comment tags automatically
mario-campos e965148
Avoid non-null assertion operators by asserting arguments
mario-campos 94f0998
Avoid creating resolved comments without an existing warning
mario-campos 1304cc4
Potential fix for pull request finding 'CodeQL / Missing space in str…
mario-campos 68a9dcb
Prefer `== true` over `!= false`
mario-campos 1a18e90
Merge branch 'main' into mario-campos/warn-on-changelog-write
mario-campos a587867
Update `actions/setup-node` action to version 7.1.0
mario-campos 0bc39f9
Prefer last comment when there are multiple to choose from
mario-campos b25bc2e
Change `RepositoryNwo` import to `type`
mario-campos 8ba2b06
Use `parseRepositoryNwo` instead of splitting manually
mario-campos eec6442
Make precedence explicit with parens
mario-campos 97a381d
Split long line
mario-campos 9de5eac
Make error a bit clearer and more actionable
mario-campos d715939
Replace `--create-if-missing` to `--update-only`
mario-campos 4094755
Polish `post-comment.ts` usage comment
mario-campos 9d1378f
Use new `case` expression for evaluating conditional `--update-only` …
mario-campos 05870b6
Defer workflow activation to a follow-up PR
mario-campos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| name: "Post comment" | ||
| description: "Create, update, or delete a sticky PR comment identified by an automatically-derived HTML-comment marker. If this action is used more than once within the same job, give each step an explicit 'id:' so the derived marker stays unique." | ||
| inputs: | ||
| body: | ||
| description: "Full comment markdown." | ||
| required: true | ||
| action-condition: | ||
| description: "The business condition ('true' or 'false') that selects which action to perform." | ||
| required: true | ||
| action-if-true: | ||
| description: "Action to perform when action-condition is 'true'. One of: none, insert, upsert, delete." | ||
| required: true | ||
| action-if-false: | ||
| description: "Action to perform when action-condition is 'false'. One of: none, insert, upsert, delete." | ||
| required: true | ||
|
mario-campos marked this conversation as resolved.
Resolved
|
||
| issue-id: | ||
| description: "The issue or pull request number to post/update/delete the comment on. Defaults to the current pull request if not provided." | ||
| required: false | ||
| token: | ||
| description: "The GitHub token to authenticate with." | ||
| required: true | ||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Post comment | ||
| shell: bash | ||
| env: | ||
| BODY: ${{ inputs.body }} | ||
| # Derived from the workflow name, job id, and step id/sequence-number so that repeated | ||
| # runs of the same step find the same comment (for upsert/delete), while distinct steps | ||
| # (even within the same job) get distinct markers. | ||
| MARKER: <!-- post-comment:${{ github.workflow }}:${{ github.job }}:${{ github.action }} --> | ||
|
mario-campos marked this conversation as resolved.
Resolved
|
||
| GH_TOKEN: ${{ inputs.token }} | ||
| ISSUE_ID: ${{ inputs.issue-id || github.event.pull_request.number }} | ||
| ACTION_CONDITION: ${{ inputs.action-condition }} | ||
| ACTION_IF_TRUE: ${{ inputs.action-if-true }} | ||
| ACTION_IF_FALSE: ${{ inputs.action-if-false }} | ||
| run: | | ||
| npx tsx ./pr-checks/post-comment.ts \ | ||
|
mario-campos marked this conversation as resolved.
Resolved
|
||
| --body "$BODY" \ | ||
| --marker "$MARKER" \ | ||
| --action-condition "$ACTION_CONDITION" \ | ||
| --action-if-true "$ACTION_IF_TRUE" \ | ||
| --action-if-false "$ACTION_IF_FALSE" \ | ||
| --issue-id "$ISSUE_ID" \ | ||
| --repository "$GITHUB_REPOSITORY" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| #!/usr/bin/env npx tsx | ||
|
|
||
| /* | ||
| Tests for post-comment.ts. | ||
| */ | ||
|
|
||
| import * as assert from "node:assert/strict"; | ||
| import { describe, it } from "node:test"; | ||
|
|
||
| import { type ApiClient } from "./api-client"; | ||
| import { | ||
| findExistingComment, | ||
| parseOptions, | ||
| resolveToken, | ||
| } from "./post-comment"; | ||
|
|
||
| /** A baseline set of valid CLI flags, as an array, for use in parseOptions tests. */ | ||
| const validFlags = [ | ||
| "--body", | ||
| "hello", | ||
| "--marker", | ||
| "<!-- marker -->", | ||
| "--action-condition", | ||
| "true", | ||
| "--action-if-true", | ||
| "insert", | ||
| "--action-if-false", | ||
| "delete", | ||
| "--issue-id", | ||
| "42", | ||
| "--repository", | ||
| "github/codeql-action", | ||
| ]; | ||
|
|
||
| /** Returns a copy of `validFlags` with the value following `flag` replaced by `value`. */ | ||
| function withFlag(flag: string, value: string): string[] { | ||
| const flags = [...validFlags]; | ||
| const index = flags.indexOf(flag); | ||
| flags[index + 1] = value; | ||
| return flags; | ||
| } | ||
|
|
||
| /** Returns a copy of `validFlags` with `flag` (and its value) removed entirely. */ | ||
| function withoutFlag(flag: string): string[] { | ||
| const flags = [...validFlags]; | ||
| const index = flags.indexOf(flag); | ||
| flags.splice(index, 2); | ||
| return flags; | ||
| } | ||
|
|
||
| describe("parseOptions", async () => { | ||
| await it("parses valid flags", () => { | ||
| const options = parseOptions(validFlags); | ||
| assert.deepEqual(options, { | ||
| body: "hello", | ||
| marker: "<!-- marker -->", | ||
| actionCondition: true, | ||
| actionIfTrue: "insert", | ||
| actionIfFalse: "delete", | ||
| issueId: 42, | ||
| repository: { owner: "github", repo: "codeql-action" }, | ||
| }); | ||
| }); | ||
|
|
||
| await it("parses action-condition=false", () => { | ||
| const options = parseOptions(withFlag("--action-condition", "false")); | ||
| assert.equal(options.actionCondition, false); | ||
| }); | ||
|
|
||
| await it("rejects a missing --body", () => { | ||
| assert.throws( | ||
| () => parseOptions(withoutFlag("--body")), | ||
| /Missing required flag --body/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects a missing --marker", () => { | ||
| assert.throws( | ||
| () => parseOptions(withoutFlag("--marker")), | ||
| /Missing required flag --marker/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects an invalid --action-condition value", () => { | ||
| assert.throws( | ||
| () => parseOptions(withFlag("--action-condition", "yes")), | ||
| /Invalid value 'yes' for --action-condition/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects an invalid --action-if-true value", () => { | ||
| assert.throws( | ||
| () => parseOptions(withFlag("--action-if-true", "bogus")), | ||
| /Invalid value 'bogus' for --action-if-true/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects an invalid --action-if-false value", () => { | ||
| assert.throws( | ||
| () => parseOptions(withFlag("--action-if-false", "bogus")), | ||
| /Invalid value 'bogus' for --action-if-false/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects a missing --issue-id", () => { | ||
| assert.throws( | ||
| () => parseOptions(withoutFlag("--issue-id")), | ||
| /Missing required flag --issue-id/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects a non-numeric --issue-id", () => { | ||
| assert.throws( | ||
| () => parseOptions(withFlag("--issue-id", "not-a-number")), | ||
| /Invalid value 'not-a-number' for --issue-id/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects a missing --repository", () => { | ||
| assert.throws( | ||
| () => parseOptions(withoutFlag("--repository")), | ||
| /Missing required flag --repository/, | ||
| ); | ||
| }); | ||
|
|
||
| await it("rejects a --repository that isn't in the form 'owner/repo'", () => { | ||
| assert.throws( | ||
| () => parseOptions(withFlag("--repository", "not-a-slug")), | ||
| /Invalid value 'not-a-slug' for --repository/, | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveToken", async () => { | ||
| await it("reads the token from GH_TOKEN", () => { | ||
| assert.equal( | ||
| resolveToken({ GH_TOKEN: "gh-token", GITHUB_TOKEN: "gha-token" }), | ||
| "gh-token", | ||
| ); | ||
| }); | ||
|
|
||
| await it("falls back to GITHUB_TOKEN", () => { | ||
| assert.equal(resolveToken({ GITHUB_TOKEN: "gha-token" }), "gha-token"); | ||
| }); | ||
|
|
||
| await it("throws when no token is set", () => { | ||
| assert.throws(() => resolveToken({}), /Missing authentication token/); | ||
| }); | ||
| }); | ||
|
|
||
| /** A minimal fake of the subset of `ApiClient` used by post-comment.ts. */ | ||
| function fakeClient(existingComments: Array<{ id: number; body?: string }>) { | ||
| const calls: { | ||
| createComment: unknown[]; | ||
| updateComment: unknown[]; | ||
| deleteComment: unknown[]; | ||
| } = { createComment: [], updateComment: [], deleteComment: [] }; | ||
|
|
||
| const client = { | ||
| paginate: async () => existingComments, | ||
| rest: { | ||
| issues: { | ||
| listComments: () => {}, | ||
| createComment: async (params: unknown) => { | ||
| calls.createComment.push(params); | ||
| }, | ||
| updateComment: async (params: unknown) => { | ||
| calls.updateComment.push(params); | ||
| }, | ||
| deleteComment: async (params: unknown) => { | ||
| calls.deleteComment.push(params); | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| return { client: client as unknown as ApiClient, calls }; | ||
| } | ||
|
|
||
| describe("findExistingComment", async () => { | ||
| await it("finds the id of the first comment containing the marker", async () => { | ||
| const { client } = fakeClient([ | ||
| { id: 1, body: "unrelated" }, | ||
| { id: 2, body: "<!-- marker -->\nhello" }, | ||
| { id: 3, body: "<!-- marker -->\nanother" }, | ||
| ]); | ||
| const id = await findExistingComment( | ||
| client, | ||
| { owner: "owner", repo: "repo" }, | ||
| 1, | ||
| "<!-- marker -->", | ||
| ); | ||
| assert.equal(id, 2); | ||
| }); | ||
|
|
||
| await it("returns undefined when no comment matches", async () => { | ||
| const { client } = fakeClient([{ id: 1, body: "unrelated" }]); | ||
| const id = await findExistingComment( | ||
| client, | ||
| { owner: "owner", repo: "repo" }, | ||
| 1, | ||
| "<!-- marker -->", | ||
| ); | ||
| assert.equal(id, undefined); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.