From dfc85800f99f741e27fd1110b1f6b24c1889da3b Mon Sep 17 00:00:00 2001 From: whywaita Date: Tue, 11 May 2021 00:42:18 +0900 Subject: [PATCH] feat: add any fields from GitHub Actions --- README.md | 1 + action.yml | 5 ++++- dist/index.js | 26 ++++++++++++++++++++++++-- src/index.ts | 6 +++++- src/slack.ts | 30 +++++++++++++++++++++++++++++- 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5473783d..c219c6cf 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ You can customize the following parameters: |channel|optional|Use Slack Incoming Webhook configuration|Slack channel name| |commit|optional|false|If true, slack notification includes the latest commit message and author.| |token|case by case|N/A|This token is used to get commit data.
If commit parameter is true, this parameter is required.
${{ secrets.GITHUB_TOKEN }} is recommended.| +|more_fields|optional|N/A|Any data fields, example: `{"key1": "value1", "key2": "value2"}`| Please refer to [action.yml](./action.yml) for more details. diff --git a/action.yml b/action.yml index 5b2d87a7..6e4f41b2 100644 --- a/action.yml +++ b/action.yml @@ -33,9 +33,12 @@ inputs: token: description: 'need to get commit data' required: false + more_fields: + description: 'any data fields' + required: false runs: using: 'node12' main: 'dist/index.js' branding: icon: 'bell' - color: 'green' \ No newline at end of file + color: 'green' diff --git a/dist/index.js b/dist/index.js index e2092501..69212e52 100644 --- a/dist/index.js +++ b/dist/index.js @@ -27921,6 +27921,8 @@ function run() { }; const commitFlag = core.getInput('commit') === 'true'; const token = core.getInput('token'); + const moreFieldsString = core.getInput('more_fields'); + const moreFields = JSON.parse(moreFieldsString); if (mention && !utils_1.isValidCondition(mentionCondition)) { mention = ''; mentionCondition = ''; @@ -27936,7 +27938,7 @@ function run() { `); } const slack = new slack_1.Slack(); - const payload = yield slack.generatePayload(jobName, status, mention, mentionCondition, commitFlag, token); + const payload = yield slack.generatePayload(jobName, status, mention, mentionCondition, commitFlag, token, moreFields); console.info(`Generated payload for slack: ${JSON.stringify(payload)}`); yield slack.notify(url, slackOptions, payload); console.info('Sent message to Slack'); @@ -28086,6 +28088,22 @@ class Block { return fields; }); } + /** + * Get MakdwnElement fields including any data + * @param {object} fields + * @returns {Promise} + */ + getMoreFields(moreFields) { + const fields = []; + for (let key in moreFields) { + const val = moreFields[key]; + fields.push({ + type: 'mrkdwn', + text: `*${key}*\n${val}` + }); + } + return fields; + } } class Slack { /** @@ -28105,7 +28123,7 @@ class Slack { * @param {string} mentionCondition * @returns {IncomingWebhookSendArguments} */ - generatePayload(jobName, status, mention, mentionCondition, commitFlag, token) { + generatePayload(jobName, status, mention, mentionCondition, commitFlag, token, moreFields) { return __awaiter(this, void 0, void 0, function* () { const slackBlockUI = new Block(); const notificationType = slackBlockUI[status]; @@ -28121,6 +28139,10 @@ class Slack { const commitFields = yield slackBlockUI.getCommitFields(token); Array.prototype.push.apply(baseBlock.fields, commitFields); } + if (moreFields) { + const anyDataFields = slackBlockUI.getMoreFields(moreFields); + Array.prototype.push.apply(baseBlock.fields, anyDataFields); + } const attachments = { color: notificationType.color, blocks: [baseBlock] diff --git a/src/index.ts b/src/index.ts index 6c196774..5bd0c27e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,6 +21,9 @@ async function run() { const commitFlag: boolean = core.getInput('commit') === 'true'; const token: string = core.getInput('token'); + const moreFieldsString: string = core.getInput('more_fields'); + const moreFields: object = JSON.parse(moreFieldsString); + if (mention && !isValidCondition(mentionCondition)) { mention = ''; mentionCondition = ''; @@ -44,7 +47,8 @@ async function run() { mention, mentionCondition, commitFlag, - token + token, + moreFields ); console.info(`Generated payload for slack: ${JSON.stringify(payload)}`); diff --git a/src/slack.ts b/src/slack.ts index a62e9d7f..13c149f8 100644 --- a/src/slack.ts +++ b/src/slack.ts @@ -116,6 +116,26 @@ class Block { } return fields; } + + /** + * Get MakdwnElement fields including any data + * @param {object} fields + * @returns {Promise} + */ + public getMoreFields(moreFields: object): MrkdwnElement[] { + const fields: MrkdwnElement[] = []; + + for (let key in moreFields) { + const val: string = moreFields[key]; + + fields.push({ + type: 'mrkdwn', + text: `*${key}*\n${val}` + }); + } + + return fields; + } } export class Slack { @@ -143,7 +163,8 @@ export class Slack { mention: string, mentionCondition: string, commitFlag: boolean, - token?: string + token?: string, + moreFields?: object ): Promise { const slackBlockUI = new Block(); const notificationType: Accessory = slackBlockUI[status]; @@ -164,6 +185,13 @@ export class Slack { Array.prototype.push.apply(baseBlock.fields, commitFields); } + if (moreFields) { + const anyDataFields: MrkdwnElement[] = slackBlockUI.getMoreFields( + moreFields + ); + Array.prototype.push.apply(baseBlock.fields, anyDataFields); + } + const attachments: MessageAttachment = { color: notificationType.color, blocks: [baseBlock]