From 2fe3ecef035e64bad8862d12a81a135bc6aa88f1 Mon Sep 17 00:00:00 2001 From: Michael Shick Date: Sat, 5 Nov 2022 08:45:45 -0400 Subject: [PATCH] add comment id (#57) * add comment id * add output to action.yml --- .github/workflows/integration.yml | 47 +++++++++++++++---------------- __tests__/add-pr-comment.test.ts | 11 ++++---- action.yml | 7 +++++ package-lock.json | 4 +-- src/main.ts | 11 ++++++-- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index bba8132..60f5482 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -32,31 +32,30 @@ jobs: run: | npm test - # dogfood: - # name: Dogfood - # runs-on: ubuntu-latest - # steps: - # - name: Checkout repo - # uses: actions/checkout@v3 + dogfood: + name: Dogfood + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v3 - # - name: Setup node.js - # uses: actions/setup-node@v3 - # with: - # node-version: 16 + - name: Setup node.js + uses: actions/setup-node@v3 + with: + node-version: 16 - # - name: Install dependencies - # run: | - # npm ci + - name: Install dependencies + run: | + npm ci - # - name: Build action - # run: | - # npm run build + - name: Build action + run: | + npm run build - # - name: Add Comment - # uses: ./ - # with: - # message: | - # **It works!** - # proxy-url: https://add-pr-comment-proxy-73luvmwygq-uc.a.run.app - # repo-token: ${{ secrets.GITHUB_TOKEN }} - # repo-token-user-login: "github-actions[bot]" + - uses: mshick/add-pr-comment@v2 + with: + message: | + **Hello** + 🌏 + ! + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/__tests__/add-pr-comment.test.ts b/__tests__/add-pr-comment.test.ts index fae1141..b28274b 100644 --- a/__tests__/add-pr-comment.test.ts +++ b/__tests__/add-pr-comment.test.ts @@ -31,6 +31,9 @@ const inputs = { let issueNumber = 1 let getCommitPullsResponse let getIssueCommentsResponse +const postIssueCommentsResponse = { + id: 42, +} vi.mock('@actions/core') @@ -38,12 +41,7 @@ export const handlers = [ rest.post( `https://api.github.com/repos/${repoFullName}/issues/:issueNumber/comments`, (req, res, ctx) => { - return res( - ctx.status(200), - ctx.json({ - url: 'https://github.com/#example', - }), - ) + return res(ctx.status(200), ctx.json(postIssueCommentsResponse)) }, ), rest.get( @@ -112,6 +110,7 @@ describe('add-pr-comment action', () => { await expect(run()).resolves.not.toThrow() expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true') + expect(core.setOutput).toHaveBeenCalledWith('comment-id', postIssueCommentsResponse.id) }) it('creates a comment in an existing PR', async () => { diff --git a/action.yml b/action.yml index bc9cba3..815615a 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,13 @@ inputs: proxy-url: description: "Proxy URL for comment creation" required: false +outputs: + comment-created: + description: "Whether a comment was created." + required: true + comment-id: + description: "If a comment was created, the id of it." + required: true branding: icon: message-circle color: purple diff --git a/package-lock.json b/package-lock.json index 8d3c3d2..f644d9e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@mshick/add-pr-comment", - "version": "1.0.0", + "version": "2.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@mshick/add-pr-comment", - "version": "1.0.0", + "version": "2.0.0", "license": "MIT", "dependencies": { "@actions/core": "^1.10.0", diff --git a/src/main.ts b/src/main.ts index ddc33bc..9d20bf1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,7 +9,6 @@ type CreateIssueCommentResponseData = Endpoints['POST /repos/{owner}/{repo}/issues/{issue_number}/comments']['response']['data'] type IssuesListCommentsResponseData = Endpoints['GET /repos/{owner}/{repo}/issues/comments']['response']['data'] - interface ListCommitPullsParams { repoToken: string owner: string @@ -174,9 +173,11 @@ const run = async (): Promise => { } } + let createdCommentData: CreateIssueCommentResponseData | null | undefined + if (shouldCreateComment) { if (proxyUrl) { - await createCommentProxy({ + createdCommentData = await createCommentProxy({ owner, repo, issueNumber, @@ -185,15 +186,19 @@ const run = async (): Promise => { proxyUrl, }) } else { - await octokit.rest.issues.createComment({ + const createdComment = await octokit.rest.issues.createComment({ owner, repo, issue_number: issueNumber, body: message, }) + createdCommentData = createdComment.data } + } + if (createdCommentData) { core.setOutput('comment-created', 'true') + core.setOutput('comment-id', createdCommentData.id) } else { core.setOutput('comment-created', 'false') }