Files
slack-github-action/index.js
T
Steve Gill 3131f81f0b
main.yml / A job to say hello (push) Failing after 2s
updated action to work with slack
2020-10-28 16:44:33 -07:00

65 lines
1.9 KiB
JavaScript

const core = require('@actions/core');
const github = require('@actions/github');
const { WebClient } = require('@slack/web-api');
const flatten = require('flat');
const axios = require('axios');
try {
// `who-to-greet` input defined in action metadata file
const nameToGreet = core.getInput('who-to-greet');
const botToken = process.env.SLACK_BOT_TOKEN;
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
console.log('botToken', botToken)
console.log('webhookUrl', webhookUrl)
// Get the JSON webhook payload for the event that triggered the workflow
const payload = github.context.payload;
console.log(`The event payload: ${JSON.stringify(payload, undefined, 2)}`);
if (botToken) {
const message = core.getInput('slack-message');
const channelId = core.getInput('channel-id');
console.log('message', message)
console.log('channelId', channelId)
const web = new WebClient(botToken);
if(channelId === undefined) {
console.log('no channel ID error')
throw 'no channel Id supplied';
}
// post message
web.chat.postMessage({text: message, channel: channelId})
} else if (webhookUrl) {
// send flat payload to webhookUrl
const flatPayload = flatten(payload);
// workflow builder requires values to be strings
// iterate over every value and convert it to string
Object.keys(flatPayload).forEach((key) => {
flatPayload[key] = '' + flatPayload[key];
})
// console.log(flatPayload);
// console.log(req.path);
axios.post(webhookUrl, flatPayload)
} else {
console.log('should throw error');
throw 'could not post';
}
console.log(`Hello ${nameToGreet}!`);
const time = (new Date()).toTimeString();
core.setOutput("time", time);
} catch (error) {
core.setFailed(error.message);
}