From 0e8dc756bc2942452843dddc9a33fc7d8e1e8993 Mon Sep 17 00:00:00 2001 From: Michael Shick Date: Mon, 24 Apr 2023 08:16:55 -0400 Subject: [PATCH] update tests --- __tests__/add-pr-comment.test.ts | 37 ++++++++++++++++++++++++-------- src/main.ts | 9 ++++++-- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/__tests__/add-pr-comment.test.ts b/__tests__/add-pr-comment.test.ts index f6a0279..dc25207 100644 --- a/__tests__/add-pr-comment.test.ts +++ b/__tests__/add-pr-comment.test.ts @@ -8,7 +8,6 @@ import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } import run from '../src/main' import apiResponse from './sample-pulls-api-response.json' -const defaultRepoFullName = 'foo/bar' const repoToken = '12345' const commitSha = 'abc123' const simpleMessage = 'hello world' @@ -36,7 +35,6 @@ const defaultInputs: Inputs = { const defaultIssueNumber = 1 -let repoFullName = defaultRepoFullName let inputs = defaultInputs let issueNumber = defaultIssueNumber let getCommitPullsResponse @@ -54,29 +52,29 @@ let messagePayload: MessagePayload | undefined vi.mock('@actions/core') -export const handlers = [ +const handlers = [ rest.post( - `https://api.github.com/repos/${repoFullName}/issues/:issueNumber/comments`, + `https://api.github.com/repos/:repoUser/:repoName/issues/:issueNumber/comments`, async (req, res, ctx) => { messagePayload = await req.json() return res(ctx.status(200), ctx.json(postIssueCommentsResponse)) }, ), rest.patch( - `https://api.github.com/repos/${repoFullName}/issues/comments/:commentId`, + `https://api.github.com/repos/:repoUser/:repoName/issues/comments/:commentId`, async (req, res, ctx) => { messagePayload = await req.json() return res(ctx.status(200), ctx.json(postIssueCommentsResponse)) }, ), rest.get( - `https://api.github.com/repos/${repoFullName}/issues/:issueNumber/comments`, + `https://api.github.com/repos/:repoUser/:repoName/issues/:issueNumber/comments`, (req, res, ctx) => { return res(ctx.status(200), ctx.json(getIssueCommentsResponse)) }, ), rest.get( - `https://api.github.com/repos/${repoFullName}/commits/:commitSha/pulls`, + `https://api.github.com/repos/:repoUser/:repoName/commits/:commitSha/pulls`, (req, res, ctx) => { return res(ctx.status(200), ctx.json(getCommitPullsResponse)) }, @@ -92,7 +90,6 @@ describe('add-pr-comment action', () => { beforeEach(() => { inputs = { ...defaultInputs } issueNumber = defaultIssueNumber - repoFullName = defaultRepoFullName vi.resetModules() @@ -104,7 +101,7 @@ describe('add-pr-comment action', () => { number: issueNumber, }, repository: { - full_name: repoFullName, + full_name: `${inputs['repo-owner']}/${inputs['repo-name']}`, name: 'bar', owner: { login: 'bar', @@ -175,6 +172,28 @@ describe('add-pr-comment action', () => { expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true') }) + it.only('creates a comment in another repo', async () => { + inputs.message = simpleMessage + inputs['repo-owner'] = 'my-owner' + inputs['repo-name'] = 'my-repo' + inputs['allow-repeats'] = 'true' + + github.context.payload = { + ...github.context.payload, + pull_request: { + number: 0, + }, + } as WebhookPayload + + issueNumber = apiResponse.result[0].number + + getCommitPullsResponse = apiResponse.result + + await expect(run()).resolves.not.toThrow() + expect(core.setOutput).toHaveBeenCalledWith('comment-created', 'true') + expect(core.setOutput).toHaveBeenCalledWith('comment-id', postIssueCommentsResponse.id) + }) + it('safely exits when no issue can be found [using GITHUB_TOKEN in env]', async () => { process.env['GITHUB_TOKEN'] = repoToken diff --git a/src/main.ts b/src/main.ts index a266f49..22f9a8d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,11 @@ import * as core from '@actions/core' import * as github from '@actions/github' import { - createComment, CreateIssueCommentResponseData, + createComment, + deleteComment, getExistingCommentId, updateComment, - deleteComment, } from './comments' import { getInputs } from './config' import { getIssueNumberFromCommitPullsList } from './issues' @@ -95,6 +95,11 @@ const run = async (): Promise => { core.setOutput('comment-updated', 'false') } } catch (err) { + if (process.env.NODE_ENV === 'test') { + // eslint-disable-next-line no-console + console.log(err) + } + if (err instanceof Error) { core.setFailed(err.message) }