Merge pull request #1036 from aburgel/main

Output created or found comment IDs
This commit is contained in:
marocchino 2023-07-20 05:55:06 +09:00 committed by GitHub
commit 3a69964ff4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 13 deletions

View file

@ -159,13 +159,13 @@ describe("createComment", () => {
beforeEach(() => { beforeEach(() => {
jest jest
.spyOn<any, string>(octokit.rest.issues, "createComment") .spyOn<any, string>(octokit.rest.issues, "createComment")
.mockResolvedValue("") .mockResolvedValue("<return value>")
}) })
it("with comment body or previousBody", async () => { it("with comment body or previousBody", async () => {
expect( expect(await createComment(octokit, repo, 456, "hello there", "")).toEqual(
await createComment(octokit, repo, 456, "hello there", "") "<return value>"
).toBeUndefined() )
expect(octokit.rest.issues.createComment).toBeCalledWith({ expect(octokit.rest.issues.createComment).toBeCalledWith({
issue_number: 456, issue_number: 456,
owner: "marocchino", owner: "marocchino",
@ -174,7 +174,7 @@ describe("createComment", () => {
}) })
expect( expect(
await createComment(octokit, repo, 456, "hello there", "TypeA") await createComment(octokit, repo, 456, "hello there", "TypeA")
).toBeUndefined() ).toEqual("<return value>")
expect(octokit.rest.issues.createComment).toBeCalledWith({ expect(octokit.rest.issues.createComment).toBeCalledWith({
issue_number: 456, issue_number: 456,
owner: "marocchino", owner: "marocchino",

View file

@ -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 }}." 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 }}" default: "${{ github.token }}"
required: false 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: runs:
using: "node16" using: "node16"
main: "dist/index.js" main: "dist/index.js"

View file

@ -7,6 +7,10 @@ import {
} from "@octokit/graphql-schema" } from "@octokit/graphql-schema"
import {GitHub} from "@actions/github/lib/utils" import {GitHub} from "@actions/github/lib/utils"
type CreateCommentResponse = Awaited<
ReturnType<InstanceType<typeof GitHub>["rest"]["issues"]["createComment"]>
>
function headerComment(header: String): string { function headerComment(header: String): string {
return `<!-- Sticky Pull Request Comment${header} -->` return `<!-- Sticky Pull Request Comment${header} -->`
} }
@ -111,11 +115,13 @@ export async function createComment(
body: string, body: string,
header: string, header: string,
previousBody?: string previousBody?: string
): Promise<void> { ): Promise<CreateCommentResponse | undefined> {
if (!body && !previousBody) if (!body && !previousBody) {
return core.warning("Comment body cannot be blank") core.warning("Comment body cannot be blank")
return
}
await octokit.rest.issues.createComment({ return await octokit.rest.issues.createComment({
...repo, ...repo,
issue_number, issue_number,
body: previousBody body: previousBody
@ -145,7 +151,7 @@ export async function minimizeComment(
): Promise<void> { ): Promise<void> {
await octokit.graphql( await octokit.graphql(
` `
mutation($input: MinimizeCommentInput!) { mutation($input: MinimizeCommentInput!) {
minimizeComment(input: $input) { minimizeComment(input: $input) {
clientMutationId clientMutationId
} }

View file

@ -64,6 +64,8 @@ async function run(): Promise<undefined> {
header header
) )
core.setOutput("previous_comment_id", previous?.id)
if (deleteOldComment) { if (deleteOldComment) {
if (previous) { if (previous) {
await deleteComment(octokit, previous.id) await deleteComment(octokit, previous.id)
@ -75,7 +77,14 @@ async function run(): Promise<undefined> {
if (onlyUpdateComment) { if (onlyUpdateComment) {
return 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 return
} }
@ -93,7 +102,7 @@ async function run(): Promise<undefined> {
const previousBody = getBodyOf(previous, append, hideDetails) const previousBody = getBodyOf(previous, append, hideDetails)
if (recreate) { if (recreate) {
await deleteComment(octokit, previous.id) await deleteComment(octokit, previous.id)
await createComment( const created = await createComment(
octokit, octokit,
repo, repo,
pullRequestNumber, pullRequestNumber,
@ -101,12 +110,20 @@ async function run(): Promise<undefined> {
header, header,
previousBody previousBody
) )
core.setOutput("created_comment_id", created?.data.id)
return return
} }
if (hideAndRecreate) { if (hideAndRecreate) {
await minimizeComment(octokit, previous.id, hideClassify) 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 return
} }