From c03114a1087e8f3dcf472f9d5a6b6f82a1c7fafc Mon Sep 17 00:00:00 2001 From: Michael Shick Date: Thu, 21 May 2020 17:30:21 -0400 Subject: [PATCH] ci: adding simple workflow --- .eslintignore | 1 + .github/workflows/ci.yml | 30 ++++++++ __tests__/add-pr-comment.test.ts | 122 ++++++++++++++++++++++++++++--- __tests__/message-windows.txt | 7 ++ __tests__/message.txt | 7 ++ package-lock.json | 18 +++++ package.json | 1 + 7 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 __tests__/message-windows.txt create mode 100644 __tests__/message.txt diff --git a/.eslintignore b/.eslintignore index ee5b393..11f813b 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,3 @@ *.js +*.txt !/.github diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a646a20 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,30 @@ +name: CI + +on: + push: + branches: + - master + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v2 + + - name: Setup node.js + uses: actions/setup-node@v1 + with: + node-version: '12.x' + + - name: Install dependencies + run: | + npm ci + + - name: Lint code + run: | + npm run lint + + - name: Run tests + run: | + npm test diff --git a/__tests__/add-pr-comment.test.ts b/__tests__/add-pr-comment.test.ts index 35151d3..a2da821 100644 --- a/__tests__/add-pr-comment.test.ts +++ b/__tests__/add-pr-comment.test.ts @@ -1,18 +1,33 @@ +import * as fs from 'fs' +import * as path from 'path' import * as core from '@actions/core' import * as github from '@actions/github' import {WebhookPayload} from '@actions/github/lib/interfaces' +import nock from 'nock' import run from '../add-pr-comment' +const repoFullName = 'foo/bar' +const repoToken = '12345' +const simpleMessage = 'hello world' +const multilineMessage = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString() +const multilineMessageWindows = fs.readFileSync(path.resolve(__dirname, './message-windows.txt')).toString() + +const inputs = { + message: '', + 'repo-token': '', + 'allow-repeats': 'false', +} + beforeEach(() => { jest.resetModules() jest.spyOn(core, 'getInput').mockImplementation((name: string): string => { switch (name) { case 'message': - return 'hello world' + return inputs.message case 'repo-token': - return '12345' + return inputs['repo-token'] case 'allow-repeats': - return 'false' + return inputs['allow-repeats'] default: return '' } @@ -20,22 +35,107 @@ beforeEach(() => { // https://developer.github.com/webhooks/event-payloads/#issues github.context.payload = { - action: 'created', - issue: { + pull_request: { number: 1, }, - comment: { - id: 1, - user: { - login: 'monalisa', + repository: { + full_name: repoFullName, + name: 'bar', + owner: { + login: 'bar', }, - body: 'Honk', }, + sha: 'abc123', } as WebhookPayload }) +afterEach(() => { + jest.restoreAllMocks() + expect(nock.pendingMocks()).toEqual([]) + nock.isDone() + nock.cleanAll() +}) + describe('add-pr-comment action', () => { - it('runs', async () => { + it('creates a comment', async () => { + inputs.message = simpleMessage + inputs['repo-token'] = repoToken + inputs['allow-repeats'] = 'true' + + const originalSetOutput = core.setOutput + + jest.spyOn(core, 'setOutput').mockImplementation((key: string, value: string): void => { + if (key === 'comment-created') { + expect(value).toBe('true') + } + + return originalSetOutput(key, value) + }) + + nock('https://api.github.com') + .post(`/repos/${repoFullName}/issues/1/comments`, ({body}) => body === simpleMessage) + .reply(200, { + url: 'https://github.com/#example', + }) + await expect(run()).resolves.not.toThrow() }) + + it('identifies repeat messages and does not create a comment', async () => { + inputs.message = simpleMessage + inputs['repo-token'] = repoToken + inputs['allow-repeats'] = 'false' + + const originalSetOutput = core.setOutput + + jest.spyOn(core, 'setOutput').mockImplementation((key: string, value: string): void => { + if (key === 'comment-created') { + expect(value).toBe('false') + } + + return originalSetOutput(key, value) + }) + + nock('https://api.github.com') + .get(`/repos/${repoFullName}/issues/1/comments`) + .reply(200, [ + { + body: simpleMessage, + user: { + login: 'github-actions[bot]', + }, + }, + ]) + + await run() + }) + + it('matches multiline messages with windows line feeds against api responses with unix linefeeds', async () => { + inputs.message = multilineMessageWindows + inputs['repo-token'] = repoToken + inputs['allow-repeats'] = 'false' + + const originalSetOutput = core.setOutput + + jest.spyOn(core, 'setOutput').mockImplementation((key: string, value: string): void => { + if (key === 'comment-created') { + expect(value).toBe('false') + } + + return originalSetOutput(key, value) + }) + + nock('https://api.github.com') + .get(`/repos/${repoFullName}/issues/1/comments`) + .reply(200, [ + { + body: multilineMessage, + user: { + login: 'github-actions[bot]', + }, + }, + ]) + + await run() + }) }) diff --git a/__tests__/message-windows.txt b/__tests__/message-windows.txt new file mode 100644 index 0000000..e78d97a --- /dev/null +++ b/__tests__/message-windows.txt @@ -0,0 +1,7 @@ +## [Preview link](https://antares-blog-staging-pr-${{ github.event.number }}.azurewebsites.net) + +- Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch. +- The preview site shows any future-dated articles. Don't worry, if you are publishing a future-dated article, it will not show on the production site until the file's specified date. +- The preview link is shareable, but will be deleted when the pull request is merged or closed. + +> *This is an automated message.* diff --git a/__tests__/message.txt b/__tests__/message.txt new file mode 100644 index 0000000..b039b81 --- /dev/null +++ b/__tests__/message.txt @@ -0,0 +1,7 @@ +## [Preview link](https://antares-blog-staging-pr-${{ github.event.number }}.azurewebsites.net) + +- Your changes have been deployed to the preview site. The preview site will update as you add more commits to this branch. +- The preview site shows any future-dated articles. Don't worry, if you are publishing a future-dated article, it will not show on the production site until the file's specified date. +- The preview link is shareable, but will be deleted when the pull request is merged or closed. + +> *This is an automated message.* diff --git a/package-lock.json b/package-lock.json index 0ba80f0..1319b4b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3976,6 +3976,18 @@ "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz", "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==" }, + "nock": { + "version": "12.0.3", + "resolved": "https://registry.npmjs.org/nock/-/nock-12.0.3.tgz", + "integrity": "sha512-QNb/j8kbFnKCiyqi9C5DD0jH/FubFGj5rt9NQFONXwQm3IPB0CULECg/eS3AU1KgZb/6SwUa4/DTRKhVxkGABw==", + "dev": true, + "requires": { + "debug": "^4.1.0", + "json-stringify-safe": "^5.0.1", + "lodash": "^4.17.13", + "propagate": "^2.0.0" + } + }, "node-fetch": { "version": "2.6.0", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", @@ -4352,6 +4364,12 @@ "sisteransi": "^1.0.4" } }, + "propagate": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/propagate/-/propagate-2.0.1.tgz", + "integrity": "sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==", + "dev": true + }, "psl": { "version": "1.8.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.8.0.tgz", diff --git a/package.json b/package.json index 0e3aa5c..7ef7846 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "eslint-config-prettier": "^6.10.1", "eslint-plugin-prettier": "^3.1.2", "jest": "^25.3.0", + "nock": "^12.0.3", "prettier": "^2.0.4", "ts-jest": "^25.3.1", "typescript": "^3.8.3"