Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 124 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
"@slack/web-api": "^6.7.2",
"axios": "^0.27.2",
"flat": "^5.0.2",
"markup-js": "^1.5.21"
"https-proxy-agent": "^5.0.1",
"markup-js": "^1.5.21",
"whatwg-url": "^11.0.0"
},
"devDependencies": {
"@vercel/ncc": "^0.34.0",
Expand Down
20 changes: 19 additions & 1 deletion src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const axios = require('axios');
const { promises: fs } = require('fs');
const path = require('path');
const markup = require('markup-js');
const HttpsProxyAgent = require('https-proxy-agent');
const { parseURL } = require('whatwg-url');

const SLACK_WEBHOOK_TYPES = {
WORKFLOW_TRIGGER: 'WORKFLOW_TRIGGER',
Expand Down Expand Up @@ -106,8 +108,24 @@ module.exports = async function slackSend(core) {
payload = flatPayload;
}

const axiosOpts = {};
try {
await axios.post(webhookUrl, payload);
if (parseURL(webhookUrl).scheme === 'https') {
const httpsProxy = process.env.HTTPS_PROXY || process.env.https_proxy || '';
if (httpsProxy && parseURL(httpsProxy).scheme === 'http') {
const httpsProxyAgent = new HttpsProxyAgent(httpsProxy);
axiosOpts.httpsAgent = httpsProxyAgent;

// Use configured tunnel above instead of default axios proxy setup from env vars
axiosOpts.proxy = false;
}
}
} catch (err) {
console.log('failed to configure https proxy agent for http proxy. Using default axios configuration');
}

try {
await axios.post(webhookUrl, payload, axiosOpts);
} catch (err) {
console.log('axios post failed, double check the payload being sent includes the keys Slack expects');
console.log(payload);
Expand Down
27 changes: 27 additions & 0 deletions test/slack-send-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,33 @@ describe('slack-send', () => {
await slackSend(fakeCore);
assert(AxiosMock.post.calledWith('https://someurl', payload));
});
describe('proxy config', () => {
beforeEach(() => {
delete process.env.HTTPS_PROXY;
});
it('should use https proxy agent when proxy uses HTTP', async () => {
process.env.HTTPS_PROXY = 'http://test.proxy:8080/';
await slackSend(fakeCore);
assert(AxiosMock.post.calledWith('https://someurl', payload, sinon.match.has('httpsAgent').and(sinon.match.has('proxy'))));
});
it('should use default axios config when no proxy set', async () => {
await slackSend(fakeCore);
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
it('should use default axios config when proxy uses HTTPS', async () => {
process.env.HTTPS_PROXY = 'https://test.proxy:8080/';
await slackSend(fakeCore);
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
it('should use default axios config when proxy URL is invalid', async () => {
process.env.HTTPS_PROXY = 'invalid string';
const consoleSpy = sinon.spy(console, 'log');
await slackSend(fakeCore);

assert(consoleSpy.calledWith('failed to configure https proxy agent for http proxy. Using default axios configuration'));
assert(AxiosMock.post.calledWithExactly('https://someurl', payload, {}));
});
});
});
describe('sad path', () => {
it('should set an error if the POST to the webhook fails without a response', async () => {
Expand Down