Merge pull request #116 from 8398a7/issue/115

Issue/115
This commit is contained in:
839 2020-10-03 17:06:13 +09:00 коммит произвёл GitHub
Родитель 4a48f57ad0 3c40a28e64
Коммит b4a838f545
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 73 добавлений и 95 удалений

16
.github/workflows/slack-pre.yml поставляемый
Просмотреть файл

@ -13,21 +13,18 @@ jobs:
with:
status: Success
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: Failed
uses: 8398a7/action-slack@pre
with:
status: Failure
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: Canceled
uses: 8398a7/action-slack@pre
with:
status: Cancelled
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: Legacy Webhook URL
uses: 8398a7/action-slack@pre
@ -36,7 +33,6 @@ jobs:
username: legacy user
icon_emoji: ":octocat:"
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_LEGACY_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: Custom Field
uses: 8398a7/action-slack@pre
@ -71,7 +67,6 @@ jobs:
}]
}
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: Custom Field2
uses: 8398a7/action-slack@pre
@ -88,14 +83,7 @@ jobs:
}]
}
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_LEGACY_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: No GITHUB_TOKEN
uses: 8398a7/action-slack@pre
with:
status: ${{ job.status }}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: if_mention
uses: 8398a7/action-slack@pre
with:
@ -103,7 +91,6 @@ jobs:
mention: here
if_mention: always
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- name: Fields Check
uses: 8398a7/action-slack@pre
@ -111,14 +98,12 @@ jobs:
status: ${{ job.status }}
fields: repo,message,commit,author,job,eventName,ref,workflow,took
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
- uses: 8398a7/action-slack@pre
with:
status: ${{ job.status }}
fields: repo,message,job,took
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
matrix-notification:
strategy:
@ -131,7 +116,6 @@ jobs:
status: ${{ job.status }}
fields: job,took
env:
GITHUB_TOKEN: ${{ github.token }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_FOR_INTEGRATION_TEST }}
MATRIX_CONTEXT: ${{ toJson(matrix) }}
if: always()

Просмотреть файл

@ -8,6 +8,7 @@ import {
getTemplate,
getApiFixture,
newWith,
githubToken,
} from './helper';
import {
@ -45,7 +46,7 @@ describe('8398a7/action-slack', () => {
fields:
'repo,message,commit,author,job,action,eventName,ref,workflow,took',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const payload = getTemplate(withParams.fields, successMsg);
payload.attachments[0].color = 'good';
expect(await client.prepare('')).toStrictEqual(payload);
@ -58,7 +59,7 @@ describe('8398a7/action-slack', () => {
...newWith(),
status: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const payload = getTemplate(withParams.fields, successMsg);
payload.attachments[0].color = 'good';
expect(await client.prepare('')).toStrictEqual(payload);
@ -68,7 +69,7 @@ describe('8398a7/action-slack', () => {
...newWith(),
status: Failure,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const payload = getTemplate(withParams.fields, failMsg);
payload.attachments[0].color = 'danger';
expect(await client.prepare('')).toStrictEqual(payload);
@ -78,7 +79,7 @@ describe('8398a7/action-slack', () => {
...newWith(),
status: Cancelled,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const payload = getTemplate(withParams.fields, cancelMsg);
payload.attachments[0].color = 'warning';
expect(await client.prepare('')).toStrictEqual(payload);
@ -90,7 +91,7 @@ describe('8398a7/action-slack', () => {
...newWith(),
status: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, msg);
payload.attachments[0].color = 'good';
@ -104,7 +105,7 @@ describe('8398a7/action-slack', () => {
mention: 'here',
if_mention: Failure,
};
let client = new Client(withParams, process.env.GITHUB_TOKEN, '');
let client = new Client(withParams, githubToken, '');
const msg = 'mention test';
let payload = getTemplate(withParams.fields, msg);
payload.attachments[0].color = 'good';
@ -112,7 +113,7 @@ describe('8398a7/action-slack', () => {
withParams.mention = '';
withParams.status = Failure;
client = new Client(withParams, process.env.GITHUB_TOKEN, '');
client = new Client(withParams, githubToken, '');
payload = getTemplate(withParams.fields, msg);
payload.attachments[0].color = 'danger';
expect(await client.prepare(msg)).toStrictEqual(payload);
@ -125,7 +126,7 @@ describe('8398a7/action-slack', () => {
mention: 'here',
if_mention: `${Failure},${Success}`,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'good';
@ -139,7 +140,7 @@ describe('8398a7/action-slack', () => {
mention: 'here',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'good';
@ -153,7 +154,7 @@ describe('8398a7/action-slack', () => {
mention: 'here',
if_mention: Failure,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'danger';
@ -167,7 +168,7 @@ describe('8398a7/action-slack', () => {
mention: 'here',
if_mention: Cancelled,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'warning';
@ -181,7 +182,7 @@ describe('8398a7/action-slack', () => {
mention: 'here',
if_mention: Always,
};
let client = new Client(withParams, process.env.GITHUB_TOKEN, '');
let client = new Client(withParams, githubToken, '');
const msg = 'mention test';
let payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'good';
@ -190,13 +191,13 @@ describe('8398a7/action-slack', () => {
payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'danger';
withParams.status = Failure;
client = new Client(withParams, process.env.GITHUB_TOKEN, '');
client = new Client(withParams, githubToken, '');
expect(await client.prepare(msg)).toStrictEqual(payload);
payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'warning';
withParams.status = Cancelled;
client = new Client(withParams, process.env.GITHUB_TOKEN, '');
client = new Client(withParams, githubToken, '');
expect(await client.prepare(msg)).toStrictEqual(payload);
});
@ -207,7 +208,7 @@ describe('8398a7/action-slack', () => {
mention: 'user_id',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<@user_id> ${msg}`);
payload.attachments[0].color = 'good';
@ -221,7 +222,7 @@ describe('8398a7/action-slack', () => {
mention: 'here',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<!here> ${msg}`);
payload.attachments[0].color = 'good';
@ -235,7 +236,7 @@ describe('8398a7/action-slack', () => {
mention: 'channel',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<!channel> ${msg}`);
payload.attachments[0].color = 'good';
@ -249,7 +250,7 @@ describe('8398a7/action-slack', () => {
mention: 'subteam^user_group_id',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(
withParams.fields,
@ -266,7 +267,7 @@ describe('8398a7/action-slack', () => {
mention: 'subteam^user_group_id,subteam^user_group_id2',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(
withParams.fields,
@ -283,7 +284,7 @@ describe('8398a7/action-slack', () => {
mention: 'user_id,user_id2',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(
withParams.fields,
@ -300,7 +301,7 @@ describe('8398a7/action-slack', () => {
mention: 'user_id,subteam^user_group_id',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(
withParams.fields,
@ -317,7 +318,7 @@ describe('8398a7/action-slack', () => {
mention: 'user_id, user_id2',
if_mention: Success,
};
let client = new Client(withParams, process.env.GITHUB_TOKEN, '');
let client = new Client(withParams, githubToken, '');
const msg = 'hello';
let payload = getTemplate(
@ -333,7 +334,7 @@ describe('8398a7/action-slack', () => {
...newWith(),
status: Success,
};
let client = new Client(withParams, process.env.GITHUB_TOKEN, '');
let client = new Client(withParams, githubToken, '');
const msg = 'hello';
// for success
@ -343,14 +344,14 @@ describe('8398a7/action-slack', () => {
// for cancel
withParams.status = Cancelled;
client = new Client(withParams, process.env.GITHUB_TOKEN, '');
client = new Client(withParams, githubToken, '');
payload = getTemplate(withParams.fields, msg);
payload.attachments[0].color = 'warning';
expect(await client.prepare(msg)).toStrictEqual(payload);
// for fail
withParams.status = Failure;
client = new Client(withParams, process.env.GITHUB_TOKEN, '');
client = new Client(withParams, githubToken, '');
payload = getTemplate(withParams.fields, msg);
payload.attachments[0].color = 'danger';
expect(await client.prepare(msg)).toStrictEqual(payload);
@ -358,7 +359,7 @@ describe('8398a7/action-slack', () => {
it('throws error', () => {
const withParams = newWith();
expect(() => new Client(withParams, undefined)).toThrow(
expect(() => new Client(withParams, githubToken)).toThrow(
'Specify secrets.SLACK_WEBHOOK_URL',
);
});
@ -377,7 +378,7 @@ describe('8398a7/action-slack', () => {
.reply(200, () => getApiFixture('repos.commits.get'));
const withParams = newWith();
const client = new Client(withParams, undefined, mockSlackWebhookUrl);
const client = new Client(withParams, githubToken, mockSlackWebhookUrl);
await client.send('payload');
@ -390,7 +391,7 @@ describe('8398a7/action-slack', () => {
status: 'custom',
fields: 'all',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
expect(
await client.custom(`{
text: \`\${process.env.AS_WORKFLOW}
@ -408,7 +409,7 @@ describe('8398a7/action-slack', () => {
...newWith(),
status: 'custom',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
expect(() => client.injectColor()).toThrow();
});
});
@ -418,7 +419,7 @@ describe('8398a7/action-slack', () => {
...newWith(),
status: 'custom',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
expect(() => client.injectText('')).toThrow();
});
});
@ -430,7 +431,7 @@ describe('8398a7/action-slack', () => {
mention: 'test1,test2, here',
if_mention: Success,
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
expect(client.mentionText(Success)).toStrictEqual(
'<@test1> <@test2> <!here> ',
);

Просмотреть файл

@ -3,7 +3,13 @@ import nock from 'nock';
process.env.GITHUB_RUN_ID = '2';
process.env.MATRIX_CONTEXT = '{"os": "ubuntu-18.04"}';
import { newWith, setupNockCommit, setupNockJobs, successMsg } from './helper';
import {
githubToken,
newWith,
setupNockCommit,
setupNockJobs,
successMsg,
} from './helper';
import { Client, With, Success } from '../src/client';
beforeAll(() => {
@ -34,7 +40,7 @@ describe('job_name', () => {
fields: 'job,took',
job_name: 'Custom Job',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
expect(await client.prepare('')).toStrictEqual({
text: successMsg,
attachments: [

Просмотреть файл

@ -3,6 +3,9 @@ import { readFileSync } from 'fs';
import { resolve } from 'path';
import { Field, With } from '../src/client';
import { FieldFactory } from '../src/fields';
import { getOctokit } from '@actions/github';
export const githubToken = 'github-token';
export const getTemplate: any = (
fields: string,
@ -69,7 +72,11 @@ export const newWith = (): With => {
};
export const fixedFields = (fields: string, sha?: string) => {
const ff = new FieldFactory(fields, process.env.GITHUB_JOB as string);
const ff = new FieldFactory(
fields,
process.env.GITHUB_JOB as string,
getOctokit(githubToken),
);
return ff.filterField(
[
ff.includes('repo') ? repo() : undefined,

Просмотреть файл

@ -3,7 +3,13 @@ import nock from 'nock';
process.env.GITHUB_RUN_ID = '2';
process.env.MATRIX_CONTEXT = '{}';
import { newWith, setupNockCommit, setupNockJobs, successMsg } from './helper';
import {
githubToken,
newWith,
setupNockCommit,
setupNockJobs,
successMsg,
} from './helper';
import { Client, With, Success } from '../src/client';
beforeAll(() => {
@ -33,7 +39,7 @@ describe('MATRIX_CONTEXT', () => {
status: Success,
fields: 'job,took',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
expect(await client.prepare('')).toStrictEqual({
text: successMsg,
attachments: [

Просмотреть файл

@ -3,7 +3,13 @@ import nock from 'nock';
process.env.GITHUB_RUN_ID = '2';
process.env.MATRIX_CONTEXT = '{"os": "ubuntu-18.04"}';
import { newWith, setupNockCommit, setupNockJobs, successMsg } from './helper';
import {
githubToken,
newWith,
setupNockCommit,
setupNockJobs,
successMsg,
} from './helper';
import { Client, With, Success } from '../src/client';
beforeAll(() => {
@ -33,7 +39,7 @@ describe('MATRIX_CONTEXT', () => {
status: Success,
fields: 'job,took',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
expect(await client.prepare('')).toStrictEqual({
text: successMsg,
attachments: [

Просмотреть файл

@ -2,7 +2,7 @@ import nock from 'nock';
process.env.GITHUB_EVENT_NAME = 'pull_request';
import { setupNockCommit, getTemplate, newWith } from './helper';
import { setupNockCommit, getTemplate, newWith, githubToken } from './helper';
import { Client, With, Success } from '../src/client';
beforeAll(() => {
@ -33,7 +33,7 @@ describe('pull request event', () => {
if_mention: Success,
fields: 'action',
};
const client = new Client(withParams, process.env.GITHUB_TOKEN, '');
const client = new Client(withParams, githubToken, '');
const msg = 'mention test';
const payload = getTemplate(withParams.fields, `<@user_id> ${msg}`, sha);
payload.attachments[0].color = 'good';

Просмотреть файл

@ -4,7 +4,6 @@ module.exports = async () => {
process.env.GITHUB_SHA = 'b24f03a32e093fe8d55e23cfd0bb314069633b2f';
process.env.GITHUB_REF = 'refs/heads/feature/19';
process.env.GITHUB_EVENT_NAME = 'push';
process.env.GITHUB_TOKEN = 'test-token';
process.env.GITHUB_RUN_ID = '1';
process.env.GITHUB_JOB = 'notification';
};

Просмотреть файл

@ -59,7 +59,7 @@ inputs:
default: ""
required: false
github_token:
description: Use this if you wish to use a different Github token than the one provided by the workflow.
description: Use this if you wish to use a different GitHub token than the one provided by the workflow.
required: true
default: ${{ github.token }}
runs:

Просмотреть файл

@ -29,7 +29,6 @@ steps:
status: ${{ job.status }}
fields: repo,message,commit,author,action,eventName,ref,workflow
env:
GITHUB_TOKEN: ${{ github.token }} # optional
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
```
@ -82,7 +81,6 @@ steps:
mention: here
if_mention: always
env:
GITHUB_TOKEN: ${{ github.token }} # optional
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
```
@ -111,7 +109,6 @@ steps:
mention: here
if_mention: failure
env:
GITHUB_TOKEN: ${{ github.token }} # optional
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
```
@ -196,6 +193,5 @@ steps:
}]
}
env:
GITHUB_TOKEN: ${{ github.token }} # optional
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} # required
```

Просмотреть файл

@ -41,16 +41,14 @@ const subteamMention = 'subteam^';
export class Client {
private fieldFactory: FieldFactory;
private webhook: IncomingWebhook;
private octokit?: Octokit;
private octokit: Octokit;
private with: With;
constructor(props: With, token?: string, webhookUrl?: string) {
constructor(props: With, token: string, webhookUrl?: string) {
this.with = props;
if (this.with.fields === '') this.with.fields = 'repo,commit';
if (token !== undefined) {
this.octokit = getOctokit(token);
}
this.octokit = getOctokit(token);
if (webhookUrl === undefined) {
throw new Error('Specify secrets.SLACK_WEBHOOK_URL');

Просмотреть файл

@ -8,11 +8,11 @@ export interface Field {
}
export class FieldFactory {
private octokit?: Octokit;
private octokit: Octokit;
private fields: string[];
private jobName: string;
constructor(fields: string, jobName: string, octokit?: Octokit) {
constructor(fields: string, jobName: string, octokit: Octokit) {
this.fields = fields.replace(/ /g, '').split(',');
this.jobName = jobName;
this.octokit = octokit;
@ -71,11 +71,6 @@ export class FieldFactory {
}
private async message(): Promise<string> {
if (this.octokit === undefined) {
process.env.AS_MESSAGE = this.githubTokenIsNotSet;
return this.githubTokenIsNotSet;
}
const resp = await this.getCommit(this.octokit);
const value = `<${resp.data.html_url}|${
@ -86,11 +81,6 @@ export class FieldFactory {
}
private async author(): Promise<string> {
if (this.octokit === undefined) {
process.env.AS_AUTHOR = this.githubTokenIsNotSet;
return this.githubTokenIsNotSet;
}
const resp = await this.getCommit(this.octokit);
const author = resp.data.commit.author;
@ -100,11 +90,6 @@ export class FieldFactory {
}
private async took(): Promise<string> {
if (this.octokit === undefined) {
process.env.AS_JOB = this.githubTokenIsNotSet;
return this.githubTokenIsNotSet;
}
const resp = await this.octokit?.actions.listJobsForWorkflowRun({
owner: context.repo.owner,
repo: context.repo.repo,
@ -139,11 +124,6 @@ export class FieldFactory {
}
private async job(): Promise<string> {
if (this.octokit === undefined) {
process.env.AS_JOB = this.githubTokenIsNotSet;
return this.githubTokenIsNotSet;
}
const { owner } = context.repo;
const resp = await this.octokit?.actions.listJobsForWorkflowRun({
owner,
@ -219,10 +199,6 @@ export class FieldFactory {
return await octokit.repos.getCommit({ owner, repo, ref });
}
private get githubTokenIsNotSet() {
return 'GitHub Token is not set.';
}
private get jobIsNotFound() {
return 'Job is not found.\nCheck <https://action-slack.netlify.app/fields|the matrix> or <https://action-slack.netlify.app/with#job_name|job name>.';
}

Просмотреть файл

@ -16,8 +16,7 @@ async function run(): Promise<void> {
const payload = core.getInput('payload');
const fields = core.getInput('fields');
const job_name = core.getInput('job_name');
const github_token =
process.env.GITHUB_TOKEN ?? core.getInput('github_token');
const github_token = core.getInput('github_token');
core.debug(`status: ${status}`);
core.debug(`mention: ${mention}`);