improved logic for webhook
main.yml / A job to say hello (push) Failing after 2s
main.yml / A job to say hello (push) Failing after 2s
This commit is contained in:
@@ -9,7 +9,6 @@ jobs:
|
||||
id: hello
|
||||
uses: stevengill/[email protected]
|
||||
with:
|
||||
who-to-greet: 'Steve'
|
||||
channel-id: 'CTAAHRA79'
|
||||
slack-message: 'posting from a github action!'
|
||||
env:
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
name: 'Hello World'
|
||||
description: 'Greet someone and record the time'
|
||||
inputs:
|
||||
who-to-greet: # id of input
|
||||
description: 'Who to greet'
|
||||
required: true
|
||||
default: 'World'
|
||||
channel-id: # channel id to post message when using bot token
|
||||
description: 'Slack channel ID where message will be posted. Needed if using bot token'
|
||||
required: false
|
||||
|
||||
@@ -4,12 +4,7 @@ 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;
|
||||
|
||||
@@ -20,6 +15,10 @@ try {
|
||||
const payload = github.context.payload;
|
||||
// console.log(`The event payload: ${JSON.stringify(payload, undefined, 2)}`);
|
||||
|
||||
if (botToken === undefined && webhookUrl === undefined) {
|
||||
throw 'Need to provide at least one botToken or webhookUrl'
|
||||
}
|
||||
|
||||
if (botToken) {
|
||||
const message = core.getInput('slack-message');
|
||||
const channelId = core.getInput('channel-id');
|
||||
@@ -28,14 +27,15 @@ try {
|
||||
|
||||
const web = new WebClient(botToken);
|
||||
|
||||
if(channelId === undefined) {
|
||||
console.log('no channel ID error')
|
||||
throw 'no channel Id supplied';
|
||||
if(channelId !== undefined && message !== undefined) {
|
||||
// post message
|
||||
web.chat.postMessage({text: message, channel: channelId});
|
||||
} else {
|
||||
console.log('missing either channel-id or slack-message! Did not send a message via chat.postMessage with botToken');
|
||||
}
|
||||
|
||||
// post message
|
||||
web.chat.postMessage({text: message, channel: channelId})
|
||||
} else if (webhookUrl) {
|
||||
}
|
||||
|
||||
if (webhookUrl) {
|
||||
// send flat payload to webhookUrl
|
||||
const flatPayload = flatten(payload);
|
||||
|
||||
@@ -45,18 +45,11 @@ try {
|
||||
flatPayload[key] = '' + flatPayload[key];
|
||||
})
|
||||
|
||||
// console.log(flatPayload);
|
||||
// console.log(req.path);
|
||||
console.log(flatPayload);
|
||||
|
||||
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user