Skip to content

Commit

Permalink
[#3] Merge branch 'master' into v3
Browse files Browse the repository at this point in the history
  • Loading branch information
8398a7 committed Jul 5, 2020
2 parents 523352d + 3d4daca commit b3f6a5b
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 161 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/slack-mainline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,18 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
matrix-notification:
strategy:
matrix:
os: [ubuntu-18.04, ubuntu-20.04]
runs-on: ${{ matrix.os }}
steps:
- uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
fields: job,took
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
MATRIX_CONTEXT: ${{ toJson(matrix) }}
if: always()
15 changes: 15 additions & 0 deletions .github/workflows/slack-pre.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,18 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
matrix-notification:
strategy:
matrix:
os: [ubuntu-18.04, ubuntu-20.04]
runs-on: ${{ matrix.os }}
steps:
- uses: 8398a7/action-slack@pre
with:
status: ${{ job.status }}
fields: job,took
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
MATRIX_CONTEXT: ${{ toJson(matrix) }}
if: always()
167 changes: 16 additions & 151 deletions __tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import nock from 'nock';
import { readFileSync } from 'fs';
import { resolve } from 'path';

process.env.GITHUB_WORKFLOW = 'PR Checks';
process.env.GITHUB_SHA = 'b24f03a32e093fe8d55e23cfd0bb314069633b2f';
Expand All @@ -10,161 +8,28 @@ process.env.GITHUB_TOKEN = 'test-token';
process.env.GITHUB_RUN_ID = '1';
process.env.GITHUB_JOB = 'notification';

import {
setupNockCommit,
setupNockJobs,
successMsg,
failMsg,
cancelMsg,
getTemplate,
getApiFixture,
} from './helper.test';
import {
Client,
With,
Success,
Failure,
Cancelled,
Always,
Field,
GitHub,
} from '../src/client';
import { FieldFactory } from '../src/fields';

const repo = (): Field => {
return {
short: true,
title: 'repo',
value: '<https://github.com/8398a7/action-slack|8398a7/action-slack>',
};
};

const message = (): Field => {
const obj: any = getApiFixture('repos.commits.get');
return {
short: true,
title: 'message',
value: `<${obj.html_url}|[#19] support for multiple user mentions>`,
};
};

const commit = (): Field => {
return {
short: true,
title: 'commit',
value: `<https://github.com/8398a7/action-slack/commit/${
process.env.GITHUB_SHA
}|${process.env.GITHUB_SHA?.slice(0, 8)}>`,
};
};

const author = (): Field => {
return { short: true, title: 'author', value: '839<8398a7@gmail.com>' };
};

const eventName = (): Field => {
return {
short: true,
title: 'eventName',
value: process.env.GITHUB_EVENT_NAME as string,
};
};

const ref = (): Field => {
return { short: true, title: 'ref', value: process.env.GITHUB_REF as string };
};

const workflow = (sha?: string): Field => {
return {
short: true,
title: 'workflow',
value: `<https://github.com/8398a7/action-slack/commit/${
sha ?? process.env.GITHUB_SHA
}/checks|${process.env.GITHUB_WORKFLOW as string}>`,
};
};

const action = (sha?: string): Field => {
return {
short: true,
title: 'action',
value: `<https://github.com/8398a7/action-slack/commit/${
sha ?? process.env.GITHUB_SHA
}/checks|action>`,
};
};

const job = (): Field => {
return {
short: true,
title: 'job',
value: `<https://github.com/8398a7/action-slack/runs/762195612|${process.env.GITHUB_JOB}>`,
};
};

const took = (): Field => {
return {
short: true,
title: 'took',
value: '1 hour 1 min 1 sec',
};
};

const fixedFields = (fields: string, sha?: string) => {
const ff = new FieldFactory(fields);
return ff.filterField(
[
ff.includes('repo') ? repo() : undefined,
ff.includes('message') ? message() : undefined,
ff.includes('commit') ? commit() : undefined,
ff.includes('author') ? author() : undefined,
ff.includes('action') ? action(sha) : undefined,
ff.includes('job') ? job() : undefined,
ff.includes('took') ? took() : undefined,
ff.includes('eventName') ? eventName() : undefined,
ff.includes('ref') ? ref() : undefined,
ff.includes('workflow') ? workflow(sha) : undefined,
],
undefined,
);
};

const getTemplate: any = (fields: string, text: string, sha?: string) => {
return {
text,
attachments: [
{
author_name: '',
color: '',
fields: fixedFields(fields, sha),
},
],
username: '',
icon_emoji: '',
icon_url: '',
channel: '',
};
};

const successMsg = ':white_check_mark: Succeeded GitHub Actions';
const cancelMsg = ':warning: Canceled GitHub Actions';
const failMsg = ':no_entry: Failed GitHub Actions';
const getApiFixture = (name: string): any =>
JSON.parse(
readFileSync(resolve(__dirname, 'fixtures', `${name}.json`)).toString(),
);

beforeAll(() => {
nock.disableNetConnect();
nock('https://api.github.com')
.persist()
.get(`/repos/8398a7/action-slack/commits/${process.env.GITHUB_SHA}`)
.reply(200, () => getApiFixture('repos.commits.get'));
nock('https://api.github.com')
.persist()
.get(
`/repos/8398a7/action-slack/actions/runs/${process.env.GITHUB_RUN_ID}/jobs`,
)
.reply(200, () => {
const obj = getApiFixture('actions.runs.jobs');
const now = new Date();
now.setHours(now.getHours() - 1);
now.setMinutes(now.getMinutes() - 1);
now.setSeconds(now.getSeconds() - 1);
obj.jobs[0].started_at = now.toISOString();
return obj;
});
setupNockCommit(process.env.GITHUB_SHA as string);
setupNockJobs(process.env.GITHUB_RUN_ID as string, 'actions.runs.jobs');
});
afterAll(() => {
nock.cleanAll();
Expand Down Expand Up @@ -194,7 +59,7 @@ describe('8398a7/action-slack', () => {
'repo,message,commit,author,job,action,eventName,ref,workflow,took',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const payload = getTemplate(withParams.fields, `${successMsg}\n`);
const payload = getTemplate(withParams.fields, successMsg);
payload.attachments[0].color = 'good';
expect(await client.success('')).toStrictEqual(payload);
});
Expand All @@ -214,7 +79,7 @@ describe('8398a7/action-slack', () => {
fields: '',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const payload = getTemplate(withParams.fields, `${successMsg}\n`);
const payload = getTemplate(withParams.fields, successMsg);
payload.attachments[0].color = 'good';
expect(await client.success('')).toStrictEqual(payload);
});
Expand All @@ -231,7 +96,7 @@ describe('8398a7/action-slack', () => {
fields: '',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const payload = getTemplate(withParams.fields, `${failMsg}\n`);
const payload = getTemplate(withParams.fields, failMsg);
payload.attachments[0].color = 'danger';
expect(await client.fail('')).toStrictEqual(payload);
});
Expand All @@ -248,7 +113,7 @@ describe('8398a7/action-slack', () => {
fields: '',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const payload = getTemplate(withParams.fields, `${cancelMsg}\n`);
const payload = getTemplate(withParams.fields, cancelMsg);
payload.attachments[0].color = 'warning';
expect(await client.cancel('')).toStrictEqual(payload);
});
Expand Down Expand Up @@ -613,7 +478,7 @@ describe('8398a7/action-slack', () => {
fields: 'message,author',
};
const client = new Client(withParams, undefined, '');
const payload = getTemplate(withParams.fields, `${successMsg}\n`);
const payload = getTemplate(withParams.fields, successMsg);
payload.attachments[0].color = 'good';
payload.attachments[0].fields = payload.attachments[0].fields.filter(
(field: any) => !['message', 'author'].includes(field.title),
Expand Down
30 changes: 30 additions & 0 deletions __tests__/fixtures/actions.matrix-runs.jobs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"total_count": 1,
"jobs": [
{
"id": 762195612,
"run_id": 132308092,
"run_url": "https://api.github.com/repos/8398a7/action-slack/actions/runs/132308092",
"node_id": "MDg6Q2hlY2tSdW43NjIxOTU2MTI=",
"head_sha": "cd15a66fc57d465564195faeb5308a63b4317cde",
"url": "https://api.github.com/repos/8398a7/action-slack/actions/jobs/762195612",
"html_url": "https://github.com/8398a7/action-slack/runs/762195612",
"status": "in_progress",
"conclusion": null,
"started_at": "2020-06-11T15:05:49Z",
"completed_at": null,
"name": "notification (ubuntu-18.04)",
"steps": [
{
"name": "Set up job",
"status": "in_progress",
"conclusion": null,
"number": 1,
"started_at": "2020-06-11T15:05:49.000Z",
"completed_at": null
}
],
"check_run_url": "https://api.github.com/repos/8398a7/action-slack/check-runs/762195612"
}
]
}
Loading

0 comments on commit b3f6a5b

Please sign in to comment.