|
| 1 | +import * as github from '@actions/github' |
| 2 | +import axios from 'axios' |
| 3 | +import MockAdapter from 'axios-mock-adapter' |
| 4 | +import {send} from '../src/slack' |
| 5 | +import {readFileSync} from 'fs' |
| 6 | + |
| 7 | +const url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX' |
| 8 | +const jobName = 'Build and Test' |
| 9 | +const jobStatus = 'Success' |
| 10 | +const jobSteps = {} |
| 11 | +const jobMatrix = {} |
| 12 | +const jobInputs = { |
| 13 | + environment: 'staging', |
| 14 | + logLevel: 'warning', |
| 15 | + print_tags: true |
| 16 | +} |
| 17 | +const channel = '@override' |
| 18 | +const message = undefined |
| 19 | + |
| 20 | +// mock github context |
| 21 | +const dump = JSON.parse(readFileSync('./__tests__/fixtures/push.json', 'utf-8')) |
| 22 | + |
| 23 | +github.context.payload = dump.event |
| 24 | +github.context.eventName = dump.event_name |
| 25 | +github.context.sha = dump.sha |
| 26 | +github.context.ref = dump.ref |
| 27 | +github.context.workflow = dump.workflow |
| 28 | +github.context.action = dump.action |
| 29 | +github.context.actor = dump.actor |
| 30 | + |
| 31 | +process.env.CI = 'true' |
| 32 | +process.env.GITHUB_WORKFLOW = 'build-test' |
| 33 | +process.env.GITHUB_RUN_ID = '100143423' |
| 34 | +process.env.GITHUB_RUN_NUMBER = '8' |
| 35 | +process.env.GITHUB_ACTION = 'self2' |
| 36 | +process.env.GITHUB_ACTIONS = 'true' |
| 37 | +process.env.GITHUB_ACTOR = 'satterly' |
| 38 | +process.env.GITHUB_REPOSITORY = 'act10ns/slack' |
| 39 | +process.env.GITHUB_EVENT_NAME = 'push' |
| 40 | +process.env.GITHUB_EVENT_PATH = '/home/runner/work/_temp/_github_workflow/event.json' |
| 41 | +process.env.GITHUB_WORKSPACE = '/home/runner/work/slack/slack' |
| 42 | +process.env.GITHUB_SHA = '68d48876e0794fba714cb331a1624af6b20942d8' |
| 43 | +process.env.GITHUB_REF = 'refs/heads/master' |
| 44 | +process.env.GITHUB_HEAD_REF = '' |
| 45 | +process.env.GITHUB_BASE_REF = '' |
| 46 | +process.env.GITHUB_SERVER_URL = 'https://github.com' |
| 47 | +process.env.GITHUB_API_URL = 'https://github.com' |
| 48 | +process.env.GITHUB_GRAPHQL_URL = 'https://api.github.com/graphql' |
| 49 | + |
| 50 | +test('push event to slack', async () => { |
| 51 | + const mockAxios = new MockAdapter(axios, {delayResponse: 200}) |
| 52 | + |
| 53 | + mockAxios |
| 54 | + .onPost() |
| 55 | + .reply(config => { |
| 56 | + console.log(config.data) |
| 57 | + return [200, {status: 'ok'}] |
| 58 | + }) |
| 59 | + .onAny() |
| 60 | + .reply(500) |
| 61 | + |
| 62 | + const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message) |
| 63 | + await expect(res).toStrictEqual({text: {status: 'ok'}}) |
| 64 | + |
| 65 | + expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({ |
| 66 | + username: 'GitHub Actions', |
| 67 | + icon_url: 'https://octodex.github.com/images/original.png', |
| 68 | + channel: '@override', |
| 69 | + timeout: 0, |
| 70 | + attachments: [ |
| 71 | + { |
| 72 | + fallback: '[GitHub]: [act10ns/slack] build-test push Success', |
| 73 | + color: 'good', |
| 74 | + author_name: 'satterly', |
| 75 | + author_link: 'https://github.com/satterly', |
| 76 | + author_icon: 'https://avatars0.githubusercontent.com/u/615057?v=4', |
| 77 | + mrkdwn_in: ['pretext', 'text', 'fields'], |
| 78 | + pretext: '', |
| 79 | + text: '*<https://github.com/act10ns/slack/actions?query=workflow:%22build-test%22|Workflow _build-test_ job _Build and Test_ triggered by _push_ is _Success_>* for <https://github.com/act10ns/slack/commits/master|`master`>\n<https://github.com/act10ns/slack/compare/db9fe60430a6...68d48876e079|`68d48876`> - 4 commits', |
| 80 | + title: '', |
| 81 | + fields: [ |
| 82 | + { |
| 83 | + title: 'Job Inputs', |
| 84 | + value: 'environment: staging\nlogLevel: warning\nprint_tags: true\n', |
| 85 | + short: false |
| 86 | + } |
| 87 | + ], |
| 88 | + footer: '<https://github.com/act10ns/slack|act10ns/slack> #8', |
| 89 | + footer_icon: 'https://github.githubassets.com/favicon.ico', |
| 90 | + ts: expect.stringMatching(/[0-9]+/) |
| 91 | + } |
| 92 | + ] |
| 93 | + }) |
| 94 | + |
| 95 | + mockAxios.resetHistory() |
| 96 | + mockAxios.reset() |
| 97 | +}) |
0 commit comments