Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-babichenko committed Jul 4, 2021
0 parents commit 3cda28d
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
31 changes: 31 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"rules": {
"no-tabs": [
"error",
{
"allowIndentationTabs": true
}
],
"indent": [
"error",
"tab"
],
"max-len": [
"error",
{
"code": 100
}
]
},
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 12
},
"extends": [
"google"
],
"env": {
"es2021": true,
"browser": true
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Yevhenii Babichenko <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
72 changes: 72 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Send Slack notification
description: Send a Slack message with the workflow data.
runs:
using: node12
main: dist/index.js
inputs:
webhook:
description: URL of Slack incoming webhook.
required: true
status:
description: |
The status to be used for the notification. A status of a step can be used.
Must be on of:
- `success`
- `failure`
- `cancelled`
required: true
text:
description: |
The text of the notification. You can use Slack message markup.
By default this is set based on the provided `status`.
required: false
default: ''
color:
description: |
The color this message will be highlighted with.
By default this is set based on the provided `status`.
required: false
default: ''
username:
description: |
Change the displayed name of the app for this message.
This requires your app to have
[`chat:write.customize`](https://api.slack.com/scopes/chat:write.customize)
scope enabled. Otherwise this field will not have any effect. See
(Slack API docs)[https://api.slack.com/messaging/sending#impersonation] for
more details.
required: false
default: ''
icon_url:
description: |
Change the displayed icon of the app.
This requires your app to have
[`chat:write.customize`](https://api.slack.com/scopes/chat:write.customize)
scope enabled. Otherwise this field will not have any effect. See
(Slack API docs)[https://api.slack.com/messaging/sending#impersonation] for
more details.
required: false
default: ''
icon_emoji:
description: |
Display an emoji as the app icon.
This requires your app to have
[`chat:write.customize`](https://api.slack.com/scopes/chat:write.customize)
scope enabled. Otherwise this field will not have any effect. See
(Slack API docs)[https://api.slack.com/messaging/sending#impersonation] for
more details.
required: false
default: ''
custom_fields:
description: |
A JSON array of additional fields to be displayed along with the default
ones. See
(Field objects)[https://api.slack.com/reference/messaging/attachments#field_objects]
documentation for the details.
required: false
default: '[]'
1 change: 1 addition & 0 deletions dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions format-json.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash
set -euo pipefail

jq -M --tab . "$1" | sponge "$1"
88 changes: 88 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const core = require('@actions/core');
const {IncomingWebhook} = require('@slack/webhook');

const webhookUrl = core.getInput('webhook', {required: true});
const webhook = new IncomingWebhook(webhookUrl);

const status = core.getInput('status');

// 1. message text
let text = core.getInput('text');
if (text === '') {
if (status === 'success') {
text = ':rocket| Action ran successfully.';
} else if (status === 'failure') {
text = ':x: Action failed.';
} else if (status === 'cancelled') {
text = ':heavy_multiplication_x: Step was cancelled.';
} else {
core.warning('cannot create message: got an invalid status string');
}
}

// 2. attachments color
let color = core.getInput('color');
if (color === '') {
if (status === 'success') {
color = 'good';
} else if (status === 'failure') {
color = 'danger';
} else if (status === 'cancelled') {
color = '#808080';
} else {
core.warning('assigning default color: got an invalid status string');
color = 'good';
}
}

// 3. build message
const actor = process.env.GITHUB_ACTOR;
const server = process.env.GITHUB_SERVER_URL;
const repo = process.env.GITHUB_REPOSITORY;
const runId = process.env.GITHUB_RUN_ID;
const workflow = process.env.GITHUB_WORKFLOW;
const commitSha = process.env.GITHUB_SHA;

const fields = [
{
title: 'Ref',
value: process.env.GITHUB_REF,
short: true,
},
{
title: 'Event',
value: process.env.GITHUB_EVENT,
short: true,
},
{
title: 'Actions URL',
value: '<' + server + '/' + repo + '/actions/runs/' + runId + '|' + workflow + '>',
short: true,
},
{
title: 'Commit',
value: '<' + server + '/' + repo + '/commit/' + commitSha + '|' + commitSha + '>',
short: true,
},
];

const customFields = JSON.parse(core.getInput('custom_fields'));

const message = {
username: core.getInput('username'),
icon_url: core.getInput('icon_url'),
icon_emoji: core.getInput('icon_emoji'),
text: text,
attachments: [{
color: color,
author_name: actor,
author_link: server + '/' + actor,
author_icon: server + '/' + actor + '.png?size=32',
fields: fields.concat(customFields),
}],
};

// 4. send message
(async () => {
await webhook.send(message);
})();
52 changes: 52 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "slack-notifier",
"author": "Yevhenii Babichenko <[email protected]>",
"description": "Slack notification sender for GitHub Actions",
"version": "1.0.0",
"license": "MIT",
"main": "index.js",
"dependencies": {
"@actions/core": "^1.3.0",
"@slack/webhook": "^6.0.0"
},
"devDependencies": {
"@vercel/ncc": "^0.28.6"
},
"scripts": {
"build": "ncc build index.js -m -o dist"
}
}

0 comments on commit 3cda28d

Please sign in to comment.