add comment id (#57)

* add comment id

* add output to action.yml
This commit is contained in:
Michael Shick 2022-11-05 08:45:45 -04:00 committed by GitHub
parent d26bdc4f91
commit 2fe3ecef03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 35 deletions

View file

@ -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 }}

View file

@ -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 () => {

View file

@ -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

4
package-lock.json generated
View file

@ -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",

View file

@ -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<void> => {
}
}
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<void> => {
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')
}