diff --git a/__tests__/comment.test.ts b/__tests__/comment.test.ts index 8149e8f..8c140dd 100644 --- a/__tests__/comment.test.ts +++ b/__tests__/comment.test.ts @@ -159,13 +159,13 @@ describe("createComment", () => { beforeEach(() => { jest .spyOn(octokit.rest.issues, "createComment") - .mockResolvedValue("") + .mockResolvedValue("") }) it("with comment body or previousBody", async () => { - expect( - await createComment(octokit, repo, 456, "hello there", "") - ).toBeUndefined() + expect(await createComment(octokit, repo, 456, "hello there", "")).toEqual( + "" + ) expect(octokit.rest.issues.createComment).toBeCalledWith({ issue_number: 456, owner: "marocchino", @@ -174,7 +174,7 @@ describe("createComment", () => { }) expect( await createComment(octokit, repo, 456, "hello there", "TypeA") - ).toBeUndefined() + ).toEqual("") expect(octokit.rest.issues.createComment).toBeCalledWith({ issue_number: 456, owner: "marocchino", diff --git a/action.yml b/action.yml index 6c9d264..6b335c3 100644 --- a/action.yml +++ b/action.yml @@ -78,6 +78,11 @@ inputs: description: "The GitHub access token (e.g. secrets.GITHUB_TOKEN) used to create or update the comment. This defaults to {{ github.token }}." default: "${{ github.token }}" required: false +outputs: + previous_comment_id: + description: "ID of previous comment, if found" + created_comment_id: + description: "ID of newly created comment, if any" runs: using: "node16" main: "dist/index.js" diff --git a/src/comment.ts b/src/comment.ts index da70951..4c7a148 100644 --- a/src/comment.ts +++ b/src/comment.ts @@ -7,6 +7,10 @@ import { } from "@octokit/graphql-schema" import {GitHub} from "@actions/github/lib/utils" +type CreateCommentResponse = Awaited< + ReturnType["rest"]["issues"]["createComment"]> +> + function headerComment(header: String): string { return `` } @@ -111,11 +115,13 @@ export async function createComment( body: string, header: string, previousBody?: string -): Promise { - if (!body && !previousBody) - return core.warning("Comment body cannot be blank") +): Promise { + if (!body && !previousBody) { + core.warning("Comment body cannot be blank") + return + } - await octokit.rest.issues.createComment({ + return await octokit.rest.issues.createComment({ ...repo, issue_number, body: previousBody @@ -145,7 +151,7 @@ export async function minimizeComment( ): Promise { await octokit.graphql( ` - mutation($input: MinimizeCommentInput!) { + mutation($input: MinimizeCommentInput!) { minimizeComment(input: $input) { clientMutationId } diff --git a/src/main.ts b/src/main.ts index 3232fce..34f792e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -64,6 +64,8 @@ async function run(): Promise { header ) + core.setOutput("previous_comment_id", previous?.id) + if (deleteOldComment) { if (previous) { await deleteComment(octokit, previous.id) @@ -75,7 +77,14 @@ async function run(): Promise { if (onlyUpdateComment) { return } - await createComment(octokit, repo, pullRequestNumber, body, header) + const created = await createComment( + octokit, + repo, + pullRequestNumber, + body, + header + ) + core.setOutput("created_comment_id", created?.data.id) return } @@ -93,7 +102,7 @@ async function run(): Promise { const previousBody = getBodyOf(previous, append, hideDetails) if (recreate) { await deleteComment(octokit, previous.id) - await createComment( + const created = await createComment( octokit, repo, pullRequestNumber, @@ -101,12 +110,20 @@ async function run(): Promise { header, previousBody ) + core.setOutput("created_comment_id", created?.data.id) return } if (hideAndRecreate) { await minimizeComment(octokit, previous.id, hideClassify) - await createComment(octokit, repo, pullRequestNumber, body, header) + const created = await createComment( + octokit, + repo, + pullRequestNumber, + body, + header + ) + core.setOutput("created_comment_id", created?.data.id) return }