Skip to content

Commit

Permalink
Merge pull request #1036 from aburgel/main
Browse files Browse the repository at this point in the history
Output created or found comment IDs
  • Loading branch information
marocchino authored Jul 19, 2023
2 parents f7a1435 + dd4c8a0 commit 3a69964
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
10 changes: 5 additions & 5 deletions __tests__/comment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ describe("createComment", () => {
beforeEach(() => {
jest
.spyOn<any, string>(octokit.rest.issues, "createComment")
.mockResolvedValue("")
.mockResolvedValue("<return value>")
})

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(
"<return value>"
)
expect(octokit.rest.issues.createComment).toBeCalledWith({
issue_number: 456,
owner: "marocchino",
Expand All @@ -174,7 +174,7 @@ describe("createComment", () => {
})
expect(
await createComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined()
).toEqual("<return value>")
expect(octokit.rest.issues.createComment).toBeCalledWith({
issue_number: 456,
owner: "marocchino",
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
16 changes: 11 additions & 5 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import {
} from "@octokit/graphql-schema"
import {GitHub} from "@actions/github/lib/utils"

type CreateCommentResponse = Awaited<
ReturnType<InstanceType<typeof GitHub>["rest"]["issues"]["createComment"]>
>

function headerComment(header: String): string {
return `<!-- Sticky Pull Request Comment${header} -->`
}
Expand Down Expand Up @@ -111,11 +115,13 @@ export async function createComment(
body: string,
header: string,
previousBody?: string
): Promise<void> {
if (!body && !previousBody)
return core.warning("Comment body cannot be blank")
): Promise<CreateCommentResponse | undefined> {
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
Expand Down Expand Up @@ -145,7 +151,7 @@ export async function minimizeComment(
): Promise<void> {
await octokit.graphql(
`
mutation($input: MinimizeCommentInput!) {
mutation($input: MinimizeCommentInput!) {
minimizeComment(input: $input) {
clientMutationId
}
Expand Down
23 changes: 20 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ async function run(): Promise<undefined> {
header
)

core.setOutput("previous_comment_id", previous?.id)

if (deleteOldComment) {
if (previous) {
await deleteComment(octokit, previous.id)
Expand All @@ -75,7 +77,14 @@ async function run(): Promise<undefined> {
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
}

Expand All @@ -93,20 +102,28 @@ async function run(): Promise<undefined> {
const previousBody = getBodyOf(previous, append, hideDetails)
if (recreate) {
await deleteComment(octokit, previous.id)
await createComment(
const created = await createComment(
octokit,
repo,
pullRequestNumber,
body,
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
}

Expand Down

0 comments on commit 3a69964

Please sign in to comment.