Skip to content

Commit

Permalink
feat: add http proxy to slack plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hborawski committed May 11, 2020
1 parent 6259987 commit a56eae3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions plugins/slack/__tests__/__snapshots__/slack.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ exports[`postToSlack should call slack api 1`] = `"{\\"text\\":\\"@channel: New

exports[`postToSlack should call slack api in env var 1`] = `"{\\"text\\":\\"@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;

exports[`postToSlack should call slack api through http proxy 1`] = `"{\\"text\\":\\"@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;

exports[`postToSlack should call slack api through https proxy 1`] = `"{\\"text\\":\\"@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;

exports[`postToSlack should call slack api with custom atTarget 1`] = `"{\\"text\\":\\"@here: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;

exports[`postToSlack should call slack api with minimal config 1`] = `"{\\"text\\":\\"@channel: New release *<https://git.hub/some/project/releases/v1.0.0|v1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;
42 changes: 42 additions & 0 deletions plugins/slack/__tests__/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,48 @@ describe("postToSlack", () => {
expect(fetchSpy.mock.calls[0][0]).toBe(
"https://custom-slack-url?token=MY_TOKEN"
);
expect(fetchSpy.mock.calls[0][1].agent).toBeUndefined()
expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot();
});

test("should call slack api through http proxy", async () => {
const plugin = new SlackPlugin("https://custom-slack-url");
process.env.SLACK_TOKEN = "MY_TOKEN";
process.env.http_proxy = "http-proxy"

await plugin.postToSlack(
mockAuto,
"1.0.0",
"# My Notes\n- PR [some link](google.com)",
// @ts-ignore
mockResponse
);

expect(fetchSpy).toHaveBeenCalled();
expect(fetchSpy.mock.calls[0][0]).toBe(
"https://custom-slack-url?token=MY_TOKEN"
);
expect(fetchSpy.mock.calls[0][1].agent).not.toBeUndefined()
expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot();
});
test("should call slack api through https proxy", async () => {
const plugin = new SlackPlugin("https://custom-slack-url");
process.env.SLACK_TOKEN = "MY_TOKEN";
process.env.https_proxy = "https-proxy"

await plugin.postToSlack(
mockAuto,
"1.0.0",
"# My Notes\n- PR [some link](google.com)",
// @ts-ignore
mockResponse
);

expect(fetchSpy).toHaveBeenCalled();
expect(fetchSpy.mock.calls[0][0]).toBe(
"https://custom-slack-url?token=MY_TOKEN"
);
expect(fetchSpy.mock.calls[0][1].agent).not.toBeUndefined()
expect(fetchSpy.mock.calls[0][1].body).toMatchSnapshot();
});

Expand Down
1 change: 1 addition & 0 deletions plugins/slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@auto-it/core": "link:../../packages/core",
"@octokit/rest": "16.43.1",
"fp-ts": "^2.5.3",
"https-proxy-agent": "^5.0.0",
"io-ts": "^2.1.2",
"node-fetch": "2.6.0",
"tslib": "1.11.1"
Expand Down
3 changes: 3 additions & 0 deletions plugins/slack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { githubToSlack } from "@atomist/slack-messages";
import { Octokit } from "@octokit/rest";
import createHttpsProxyAgent from "https-proxy-agent";

import {
Auto,
Expand Down Expand Up @@ -144,6 +145,7 @@ export default class SlackPlugin implements IPlugin {

const body = sanitizeMarkdown(releaseNotes);
const token = process.env.SLACK_TOKEN;
const proxyUrl = process.env.https_proxy || process.env.http_proxy;
const atTarget = this.options.atTarget;
const urls = releases.map(
(release) => `*<${release.data.html_url}|${release.data.name}>*`
Expand All @@ -161,6 +163,7 @@ export default class SlackPlugin implements IPlugin {
link_names: 1,
}),
headers: { "Content-Type": "application/json" },
agent: proxyUrl ? createHttpsProxyAgent(proxyUrl) : undefined
});

auto.logger.verbose.info("Posted release notes to slack.");
Expand Down

0 comments on commit a56eae3

Please sign in to comment.