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
|
id: hello
|
||||||
uses: stevengill/[email protected]
|
uses: stevengill/[email protected]
|
||||||
with:
|
with:
|
||||||
who-to-greet: 'Steve'
|
|
||||||
channel-id: 'CTAAHRA79'
|
channel-id: 'CTAAHRA79'
|
||||||
slack-message: 'posting from a github action!'
|
slack-message: 'posting from a github action!'
|
||||||
env:
|
env:
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
name: 'Hello World'
|
name: 'Hello World'
|
||||||
description: 'Greet someone and record the time'
|
description: 'Greet someone and record the time'
|
||||||
inputs:
|
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
|
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'
|
description: 'Slack channel ID where message will be posted. Needed if using bot token'
|
||||||
required: false
|
required: false
|
||||||
|
|||||||
@@ -4,12 +4,7 @@ const { WebClient } = require('@slack/web-api');
|
|||||||
const flatten = require('flat');
|
const flatten = require('flat');
|
||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
try {
|
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 botToken = process.env.SLACK_BOT_TOKEN;
|
||||||
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
|
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
|
||||||
|
|
||||||
@@ -20,6 +15,10 @@ try {
|
|||||||
const payload = github.context.payload;
|
const payload = github.context.payload;
|
||||||
// console.log(`The event payload: ${JSON.stringify(payload, undefined, 2)}`);
|
// 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) {
|
if (botToken) {
|
||||||
const message = core.getInput('slack-message');
|
const message = core.getInput('slack-message');
|
||||||
const channelId = core.getInput('channel-id');
|
const channelId = core.getInput('channel-id');
|
||||||
@@ -28,14 +27,15 @@ try {
|
|||||||
|
|
||||||
const web = new WebClient(botToken);
|
const web = new WebClient(botToken);
|
||||||
|
|
||||||
if(channelId === undefined) {
|
if(channelId !== undefined && message !== undefined) {
|
||||||
console.log('no channel ID error')
|
// post message
|
||||||
throw 'no channel Id supplied';
|
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})
|
if (webhookUrl) {
|
||||||
} else if (webhookUrl) {
|
|
||||||
// send flat payload to webhookUrl
|
// send flat payload to webhookUrl
|
||||||
const flatPayload = flatten(payload);
|
const flatPayload = flatten(payload);
|
||||||
|
|
||||||
@@ -45,18 +45,11 @@ try {
|
|||||||
flatPayload[key] = '' + flatPayload[key];
|
flatPayload[key] = '' + flatPayload[key];
|
||||||
})
|
})
|
||||||
|
|
||||||
// console.log(flatPayload);
|
console.log(flatPayload);
|
||||||
// console.log(req.path);
|
|
||||||
|
|
||||||
axios.post(webhookUrl, flatPayload)
|
axios.post(webhookUrl, flatPayload)
|
||||||
} else {
|
|
||||||
console.log('should throw error');
|
|
||||||
throw 'could not post';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
console.log(`Hello ${nameToGreet}!`);
|
|
||||||
const time = (new Date()).toTimeString();
|
const time = (new Date()).toTimeString();
|
||||||
core.setOutput("time", time);
|
core.setOutput("time", time);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user