Skip to content

Commit

Permalink
Merge pull request #4 from craftech-io/validation
Browse files Browse the repository at this point in the history
Checking if webhook_url variable exists
  • Loading branch information
ayalaluquez authored May 21, 2020
2 parents 97a1359 + f0a6660 commit fb1d4e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
![Notification](https://github.com/craftech-io/slack-action/workflows/Notification/badge.svg)
[![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/)

# Slack Action

Expand All @@ -14,7 +15,7 @@ You can customize the following parameters:

| With Parameter | Required/Optional | Description |
| --------------------- | ----------------- | ------------|
| `SLACK_WEBHOOK_URL` | **Required** | The Slack Incoming Webhooks URL. <br>[Please specify this key or SLACK_WEBHOOK_URL environment secret](https://help.github.com/es/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets)
| `SLACK_WEBHOOK_URL` | **Required** | The Slack Incoming Webhooks URL. <br>Please specify the [environment secret](https://help.github.com/es/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) for SLACK_WEBHOOK_URL
| `STATUS` | ***Optional*** | The result of GitHub Actions job<br>This parameter value must contain the following word:<br>- `success`<br>- `failure`<br>- `cancelled`<br> default is using ${{ job.status }}
| `SLACK_CHANNEL` | ***Optional*** | Override the default incoming Webhook Slack settings
| `SLACK_USERNAME` | ***Optional*** | Override the default incoming Webhook Slack settings
Expand Down
33 changes: 15 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ const sucess_color = '#00C0C7';
const cancelled_color = '#FFA900';
const failure_color = '#FF614E';


function post(slackMessage) {
const slack_webhook_url = core.getInput("slack_webhook_url");
fetch(slack_webhook_url, {
method: 'POST',
body: JSON.stringify(slackMessage),
headers: { 'Content-Type': 'application/json' },
}).catch(console.error);
}

if (!core.getInput("slack_webhook_url")) {
try {
throw new Error(`[Error] Missing Slack Incoming Webhooks URL
Please configure "SLACK_WEBHOOK" as environment variable or
specify the key called "slack_webhook_url" in "with" section`);
}
catch (error) {
console.error(error.message);
}
}
}

function getColor(status) {

Expand All @@ -25,19 +35,13 @@ function getColor(status) {
if (status.toLowerCase() === 'cancelled') {
return cancelled_color;
}

if (status.toLowerCase() === 'failure') {
return failure_color;
}

return start_color;
}



function getText(status) {
const token = core.getInput("repoToken");
const {owner, repo} = github.context.repo;
const actor = github.context.actor;
const workflow = github.context.workflow;
started = `<http://github.com/${actor}|${actor}>` + ' has *started* the "' + `${workflow}` + '"' + ' workflow ';
Expand All @@ -51,15 +55,12 @@ function getText(status) {
if (status.toLowerCase() === 'cancelled') {
return cancelled;
}

if (status.toLowerCase() === 'failure') {
return failure;
}

if (status.toLowerCase() === 'started') {
return started;
}

return 'status no valido';
}

Expand All @@ -69,8 +70,6 @@ function generateSlackMessage(text) {
const status = core.getInput("status");
const channel = core.getInput("slack_channel");
const username = core.getInput("slack_username");
const workflow = github.context.workflow;

return {
channel,
username,
Expand All @@ -92,7 +91,6 @@ function generateSlackMessage(text) {
"title": "Ref",
"value": github.context.ref,
"short": true

},
],
"actions": [
Expand All @@ -111,9 +109,8 @@ function generateSlackMessage(text) {
]
};
}

try {
post(generateSlackMessage(' '));
post(generateSlackMessage('Sending message'));
} catch (error) {
core.setFailed(error.message);
}
core.setFailed(`[Error] There was an error when sending the slack notification`);
}

0 comments on commit fb1d4e5

Please sign in to comment.