From 7c2621631d3d0a28ec3e0afe085959797778c3e4 Mon Sep 17 00:00:00 2001 From: Steve Gill Date: Thu, 13 May 2021 16:44:21 -0700 Subject: [PATCH] added ability for json payload to be passed in --- .github/workflows/customJson.yml | 19 +++++++++++++++++++ index.js | 29 +++++++++++++++++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 .github/workflows/customJson.yml diff --git a/.github/workflows/customJson.yml b/.github/workflows/customJson.yml new file mode 100644 index 0000000..bf58f2c --- /dev/null +++ b/.github/workflows/customJson.yml @@ -0,0 +1,19 @@ +on: [push] + +jobs: + new_push_job: + runs-on: ubuntu-latest + name: New push to repo + steps: + - name: Publish to slack + id: slack + uses: slackapi/slack-github-action@v1.4.0 + with: + payload: '{key: "value", foo: "bar"}' + env: + SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + + # Use the output from the `hello` step + - name: Get the output time + run: echo "The time was ${{ steps.hello.outputs.time }}" diff --git a/index.js b/index.js index 9cf4e95..3fdd0c7 100644 --- a/index.js +++ b/index.js @@ -12,19 +12,36 @@ try { console.log('webhookUrl', webhookUrl, typeof webhookUrl) - // Get the JSON webhook payload for the event that triggered the workflow - const payload = github.context.payload; + let payload = core.getInput('payload'); + console.log(payload) + + if (payload === undefined) { + console.log('undefined payload'); + console.log('no payload passed in, using payload that triggered the GitHub Action') + // Get the JSON webhook payload for the event that triggered the workflow + payload = github.context.payload; + } else { + try { + // confirm it is valid json + payload = JSON.parse(payload); + } catch (e) { + // passed in payload wasn't valid json + console.error("passed in payload was invalid") + throw 'Need to provide valid json 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.length > 0) { + const channelId = core.getInput('channel-id'); + console.log('channelId', channelId, typeof channelId) + if (botToken.length > 0 && channelId !== undefined) { const message = core.getInput('slack-message'); - const channelId = core.getInput('channel-id'); console.log('message', message, typeof message) - console.log('channelId', channelId, typeof channelId) const web = new WebClient(botToken); @@ -56,4 +73,4 @@ try { } catch (error) { core.setFailed(error.message); -} \ No newline at end of file +}