Skip to content

Commit

Permalink
Merge pull request #1210 from hborawski/slack-proxy
Browse files Browse the repository at this point in the history
Slack Plugin HTTP Proxy support
  • Loading branch information
hipstersmoothie authored May 11, 2020
2 parents 6259987 + 075c52b commit cfa6d01
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 27 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
31 changes: 4 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
integrity sha512-m2OzlTDbhvzK4hgswH9Lx0cdpq1AntXu53Klz5RGkFfuoDvKsnlU7tmtVfNWtUiP0zZbGtrb/BZYH7aB6TRnMA==

"@auto-it/bot-list@link:packages/bot-list":
version "9.31.0"
version "9.31.2"

"@auto-it/core@link:packages/core":
version "9.31.0"
version "9.31.2"
dependencies:
"@auto-it/bot-list" "link:packages/bot-list"
"@octokit/core" "2.4.2"
Expand Down Expand Up @@ -50,7 +50,7 @@
url-join "^4.0.0"

"@auto-it/npm@link:plugins/npm":
version "9.31.0"
version "9.31.2"
dependencies:
"@auto-it/core" "link:packages/core"
await-to-js "^2.1.1"
Expand All @@ -68,7 +68,7 @@
user-home "^2.0.0"

"@auto-it/released@link:plugins/released":
version "9.31.0"
version "9.31.2"
dependencies:
"@auto-it/core" "link:packages/core"
deepmerge "^4.0.0"
Expand Down Expand Up @@ -2876,16 +2876,6 @@
regexpp "^3.0.0"
tsutils "^3.17.1"

"@typescript-eslint/experimental-utils@2.27.0":
version "2.27.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.27.0.tgz#801a952c10b58e486c9a0b36cf21e2aab1e9e01a"
integrity sha512-vOsYzjwJlY6E0NJRXPTeCGqjv5OHgRU1kzxHKWJVPjDYGbPgLudBXjIlc+OD1hDBZ4l1DLbOc5VjofKahsu9Jw==
dependencies:
"@types/json-schema" "^7.0.3"
"@typescript-eslint/typescript-estree" "2.27.0"
eslint-scope "^5.0.0"
eslint-utils "^2.0.0"

"@typescript-eslint/experimental-utils@2.31.0", "@typescript-eslint/experimental-utils@^2.5.0":
version "2.31.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.31.0.tgz#a9ec514bf7fd5e5e82bc10dcb6a86d58baae9508"
Expand All @@ -2906,19 +2896,6 @@
"@typescript-eslint/typescript-estree" "2.31.0"
eslint-visitor-keys "^1.1.0"

"@typescript-eslint/typescript-estree@2.27.0":
version "2.27.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.27.0.tgz#a288e54605412da8b81f1660b56c8b2e42966ce8"
integrity sha512-t2miCCJIb/FU8yArjAvxllxbTiyNqaXJag7UOpB5DVoM3+xnjeOngtqlJkLRnMtzaRcJhe3CIR9RmL40omubhg==
dependencies:
debug "^4.1.1"
eslint-visitor-keys "^1.1.0"
glob "^7.1.6"
is-glob "^4.0.1"
lodash "^4.17.15"
semver "^6.3.0"
tsutils "^3.17.1"

"@typescript-eslint/typescript-estree@2.31.0":
version "2.31.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.31.0.tgz#ac536c2d46672aa1f27ba0ec2140d53670635cfd"
Expand Down

0 comments on commit cfa6d01

Please sign in to comment.