Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more_fields #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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.<br>If commit parameter is true, this parameter is required.<br>${{ 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.

Expand Down
5 changes: 4 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
color: 'green'
21 changes: 19 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10036,6 +10036,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 = '';
Expand All @@ -10053,7 +10055,7 @@ function run() {
if (commitFlag) {
commit = yield github.getCommit(token);
}
const payload = slack_1.Slack.generatePayload(jobName, status, mention, mentionCondition, commit);
const payload = slack_1.Slack.generatePayload(jobName, status, mention, mentionCondition, commit, moreFields);
core.debug(`Generated payload for slack: ${JSON.stringify(payload)}`);
yield slack_1.Slack.notify(url, slackOptions, payload);
core.info('Post message to Slack');
Expand Down Expand Up @@ -10147,6 +10149,17 @@ class Block {
}
return field;
}
static getMoreFields(moreFields) {
const fields = [];
for (let key in moreFields) {
const val = moreFields[key];
fields.push({
type: 'mrkdwn',
text: `*${key}*\n${val}`
});
}
return fields;
}
}
exports.Block = Block;
Block.status = {
Expand All @@ -10167,7 +10180,7 @@ class Slack {
static isMention(condition, status) {
return condition === 'always' || condition === status;
}
static generatePayload(jobName, status, mention, mentionCondition, commit) {
static generatePayload(jobName, status, mention, mentionCondition, commit, moreFields) {
const blockStatus = Block.status[status];
const tmpText = `${jobName} ${blockStatus.result}`;
const text = mention && Slack.isMention(mentionCondition, status)
Expand All @@ -10181,6 +10194,10 @@ class Slack {
const commitField = Block.getCommitField(commit);
Array.prototype.push.apply(baseBlock.fields, commitField);
}
if (moreFields) {
const anyDataFields = Block.getMoreFields(moreFields);
Array.prototype.push.apply(baseBlock.fields, anyDataFields);
}
return {
text,
attachments: [
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ async function run() {
const commitFlag = core.getInput('commit') === 'true';
const token = core.getInput('token');

const moreFieldsString: string = core.getInput('more_fields');
const moreFields: object = JSON.parse(moreFieldsString);

if (mention && !isValidCondition(mentionCondition)) {
mention = '';
mentionCondition = '';
Expand All @@ -45,7 +48,8 @@ async function run() {
status,
mention,
mentionCondition,
commit
commit,
moreFields
);
core.debug(`Generated payload for slack: ${JSON.stringify(payload)}`);

Expand Down
25 changes: 24 additions & 1 deletion src/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ export class Block {

return field;
}

public static 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 {
Expand All @@ -82,7 +97,8 @@ export class Slack {
status: string,
mention: string,
mentionCondition: string,
commit?: github.CommitContext
commit?: github.CommitContext,
moreFields?: object
): IncomingWebhookSendArguments {
const blockStatus = Block.status[status];
const tmpText = `${jobName} ${blockStatus.result}`;
Expand All @@ -100,6 +116,13 @@ export class Slack {
Array.prototype.push.apply(baseBlock.fields, commitField);
}

if (moreFields) {
const anyDataFields: MrkdwnElement[] = Block.getMoreFields(
moreFields
);
Array.prototype.push.apply(baseBlock.fields, anyDataFields);
}

return {
text,
attachments: [
Expand Down