From 76ad6db4cfe01bf01fe2d79b104ec4a8cc1db82c Mon Sep 17 00:00:00 2001 From: Jacob Raihle kdm951 Date: Tue, 30 May 2023 20:32:08 +0200 Subject: [PATCH 1/2] 204: Handle proxies when using Slack WebClient --- src/slack-send.js | 6 ++++-- src/web-client.js | 20 ++++++++++++++++++++ test/slack-send-test.js | 1 + test/web-client-test.js | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 src/web-client.js create mode 100644 test/web-client-test.js diff --git a/src/slack-send.js b/src/slack-send.js index 86c99ac4..4ef5b26b 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -1,5 +1,4 @@ const github = require('@actions/github'); -const { WebClient } = require('@slack/web-api'); const flatten = require('flat'); const axios = require('axios'); const { promises: fs } = require('fs'); @@ -8,6 +7,8 @@ const markup = require('markup-js'); const HttpsProxyAgent = require('https-proxy-agent'); const { parseURL } = require('whatwg-url'); +const { createWebClient } = require('./web-client'); + const SLACK_WEBHOOK_TYPES = { WORKFLOW_TRIGGER: 'WORKFLOW_TRIGGER', INCOMING_WEBHOOK: 'INCOMING_WEBHOOK', @@ -63,7 +64,8 @@ module.exports = async function slackSend(core) { if (typeof botToken !== 'undefined' && botToken.length > 0) { const message = core.getInput('slack-message') || ''; const channelIds = core.getInput('channel-id') || ''; - const web = new WebClient(botToken); + const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || ''; + const web = createWebClient(botToken, httpsProxy); if (channelIds.length <= 0) { console.log('Channel ID is required to run this action. An empty one has been provided'); diff --git a/src/web-client.js b/src/web-client.js new file mode 100644 index 00000000..6945e6a2 --- /dev/null +++ b/src/web-client.js @@ -0,0 +1,20 @@ +const { WebClient } = require('@slack/web-api'); +const HttpsProxyAgent = require('https-proxy-agent'); + +/** + * + * @param {string} botToken token used to authenticate as the Slack Bot user + * @param {string?} httpsProxy (optional) URL for the proxy to use for HTTPS requests + * @returns { WebClient } a WebClient configured with the bot token and proxy agent (if needed) + */ +function createWebClient(botToken, httpsProxy) { + if (httpsProxy) { + const httpsProxyAgent = new HttpsProxyAgent(httpsProxy); + return new WebClient(botToken, { agent: httpsProxyAgent }); + } + return new WebClient(botToken); +} + +module.exports = { + createWebClient, +}; diff --git a/test/slack-send-test.js b/test/slack-send-test.js index 06d6347a..e47d8d51 100644 --- a/test/slack-send-test.js +++ b/test/slack-send-test.js @@ -189,6 +189,7 @@ describe('slack-send', () => { }); describe('proxy config', () => { beforeEach(() => { + delete process.env.https_proxy; delete process.env.HTTPS_PROXY; }); it('should use https proxy agent when proxy uses HTTP', async () => { diff --git a/test/web-client-test.js b/test/web-client-test.js new file mode 100644 index 00000000..89627d71 --- /dev/null +++ b/test/web-client-test.js @@ -0,0 +1,33 @@ +const { assert } = require('chai'); +const sinon = require('sinon'); +const rewiremock = require('rewiremock/node'); + +/* eslint-disable-next-line global-require */ +rewiremock(() => require('@slack/web-api')).with({ + WebClient: class { + constructor(token, options) { + this.token = token; + this.options = options || {}; + } + }, +}); +rewiremock.enable(); +const { createWebClient } = require('../src/web-client'); + +rewiremock.disable(); + +describe('web-client', () => { + beforeEach(() => { + sinon.reset(); + }); + + it('should create WebClient with an https proxy agent if given a proxy URL', async () => { + const web = createWebClient('xoxb-xxxxx', 'http://test.proxy:8080/'); + assert.equal(typeof web.options.agent, 'object'); + }); + + it('should create WebClient with default settings if not given a proxy URL', async () => { + const web = createWebClient('xoxb-xxxxx'); + assert.equal(web.options.agent, undefined); + }); +}); From 3203bd913446a871012707e237a875ab0eaf0e07 Mon Sep 17 00:00:00 2001 From: Ashley Huynh Date: Wed, 31 May 2023 11:43:22 -0400 Subject: [PATCH 2/2] Update workflow to escape JSON output --- .github/workflows/main.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 98459f28..bbc48707 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -104,7 +104,9 @@ jobs: - uses: actions/checkout@v3 - run: npm ci && npm run build - name: Dump out GitHub Context - run: echo '${{ toJSON(github) }}' + run: echo $JSON + env: + JSON: ${{ toJSON(github) }} - name: Post message to Slack with Payload path id: slackPayloadFile uses: ./