ci: adding simple workflow

This commit is contained in:
Michael Shick 2020-05-21 17:30:21 -04:00
parent 81c7cad365
commit c03114a108
No known key found for this signature in database
GPG key ID: ADF5BC9704BB4A61
7 changed files with 175 additions and 11 deletions

View file

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

View file

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

7
__tests__/message.txt Normal file
View file

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