diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4b24fad..2e866b6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,6 @@ jobs: id: hello uses: stevengill/slack-github-action@v1.1.0 with: - who-to-greet: 'Steve' channel-id: 'CTAAHRA79' slack-message: 'posting from a github action!' env: diff --git a/action.yml b/action.yml index d01c362..a7e38a4 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/index.js b/index.js index a599459..9875a28 100644 --- a/index.js +++ b/index.js @@ -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);