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(message): add custom message to action #35

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ The ID of a previous Slack message to update instead of posting a new message. T
message_id: ${{ steps.<your_first_slack_step_id>.outputs.message_id }}
```

### `message`

A custom message.

## Outputs

### `message_id`
Expand Down
30 changes: 30 additions & 0 deletions __tests__/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ describe('Utils', () => {
short: true,
});
});

it('show message', () => {
const attachments = buildSlackAttachments({
status: 'STARTED',
color: 'good',
github: GITHUB_PUSH_EVENT,
message: 'message',
});

expect(attachments[0].fields.find(a => a.title === 'Message')).toEqual({
title: 'Message',
value: 'message',
short: false,
});
});
});

describe('for PR events', () => {
Expand Down Expand Up @@ -91,6 +106,21 @@ describe('Utils', () => {
short: true,
});
});

it('show message', () => {
const attachments = buildSlackAttachments({
status: 'STARTED',
color: 'good',
github: GITHUB_PR_EVENT,
message: 'message',
});

expect(attachments[0].fields.find(a => a.title === 'Message')).toEqual({
title: 'Message',
value: 'message',
short: false,
});
});
});
});
});
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ inputs:
description: 'The color of the Slack attachment.'
required: true
default: '#cccccc'
message:
description: 'A custom message to show.'
required: false
message_id:
description: 'The ID of the existing Slack message to update.'
required: false
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { buildSlackAttachments, formatChannelName } = require('./src/utils');
const channel = core.getInput('channel');
const status = core.getInput('status');
const color = core.getInput('color');
const message = core.getInput('message');
const messageId = core.getInput('message_id');
const token = process.env.SLACK_BOT_TOKEN;
const slack = new WebClient(token);
Expand All @@ -17,7 +18,7 @@ const { buildSlackAttachments, formatChannelName } = require('./src/utils');
return;
}

const attachments = buildSlackAttachments({ status, color, github });
const attachments = buildSlackAttachments({ status, color, github, message });
const channelId = core.getInput('channel_id') || (await lookUpChannelId({ slack, channel }));

if (!channelId) {
Expand Down
61 changes: 34 additions & 27 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { context } = require('@actions/github');

function buildSlackAttachments({ status, color, github }) {
function buildSlackAttachments({ status, color, github, message }) {
const { payload, ref, workflow, eventName } = github.context;
const { owner, repo } = context.repo;
const event = eventName;
Expand All @@ -21,32 +21,39 @@ function buildSlackAttachments({ status, color, github }) {
short: true,
};

return [
{
color,
fields: [
{
title: 'Action',
value: `<https://github.com/${owner}/${repo}/commit/${sha}/checks | ${workflow}>`,
short: true,
},
{
title: 'Status',
value: status,
short: true,
},
referenceLink,
{
title: 'Event',
value: event,
short: true,
},
],
footer_icon: 'https://github.githubassets.com/favicon.ico',
footer: `<https://github.com/${owner}/${repo} | ${owner}/${repo}>`,
ts: Math.floor(Date.now() / 1000),
},
];
var attachment = {
color,
fields: [
{
title: 'Action',
value: `<https://github.com/${owner}/${repo}/commit/${sha}/checks | ${workflow}>`,
short: true,
},
{
title: 'Status',
value: status,
short: true,
},
referenceLink,
{
title: 'Event',
value: event,
short: true,
},
],
footer_icon: 'https://github.githubassets.com/favicon.ico',
footer: `<https://github.com/${owner}/${repo} | ${owner}/${repo}>`,
ts: Math.floor(Date.now() / 1000),
};

if (message)
attachment['fields'].push({
title: 'Message',
value: message,
short: false,
});

return [attachment];
}

module.exports.buildSlackAttachments = buildSlackAttachments;
Expand Down