Skip to content

Commit

Permalink
use release URL from response for an accurate URL
Browse files Browse the repository at this point in the history
  • Loading branch information
hipstersmoothie committed Apr 1, 2020
1 parent 32be4bf commit 26f635b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 54 deletions.
8 changes: 4 additions & 4 deletions plugins/slack/__tests__/__snapshots__/slack.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`postToSlack should call slack api 1`] = `"{\\"text\\":\\"@channel: New release *<https://github.custom.com/adierkens/test/releases/tag/1.0.0|1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;
exports[`postToSlack should call slack api 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 in env var 1`] = `"{\\"text\\":\\"@channel: New release *<https://github.custom.com/adierkens/test/releases/tag/1.0.0|1.0.0>*\\\\n* My Notes*\\\\n• PR <google.com|some link>\\",\\"link_names\\":1}"`;
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 with custom atTarget 1`] = `"{\\"text\\":\\"@here: New release *<https://github.custom.com/adierkens/test/releases/tag/1.0.0|1.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://github.custom.com/adierkens/test/releases/tag/1.0.0|1.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: 27 additions & 15 deletions plugins/slack/__tests__/slack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ beforeEach(() => {
fetchSpy.mockClear();
});

const mockResponse = [
{
data: {
html_url: "https://git.hub/some/project/releases/v1.0.0",
name: "v1.0.0",
},
},
];

// For the purpose of this test, we use the current branch as the "prerelease" branch to fake being on a "next" branch
const nextBranch = execSync("git rev-parse --abbrev-ref HEAD", {
encoding: "utf8",
}).trim();

const mockGit = {
options: {
owner: "adierkens",
repo: "test",
},
getProject: () => ({
html_url: "https://github.custom.com/adierkens/test",
}),
};
const mockAuto = ({
git: mockGit,
git: {},
logger: dummyLog(),
} as unknown) as Auto;

Expand Down Expand Up @@ -113,7 +113,7 @@ describe("postToSlack", () => {
).rejects.toBeInstanceOf(Error);
});

test("doesn't post when prelease branch and using default prereleasePublish setting", async () => {
test("doesn't post when prerelease branch and using default prereleasePublish setting", async () => {
// @ts-ignore
const plugin = new SlackPlugin({
url: "https://custom-slack-url",
Expand Down Expand Up @@ -141,7 +141,7 @@ describe("postToSlack", () => {
expect(fetchSpy).not.toHaveBeenCalled();
});

test("doesn't post when prelease branch setting is false", async () => {
test("doesn't post when prerelease branch setting is false", async () => {
// @ts-ignore
const plugin = new SlackPlugin({
url: "https://custom-slack-url",
Expand Down Expand Up @@ -170,7 +170,7 @@ describe("postToSlack", () => {
expect(fetchSpy).not.toHaveBeenCalled();
});

test("posts when prelease branch setting is true", async () => {
test("posts when prerelease branch setting is true", async () => {
// @ts-ignore
const plugin = new SlackPlugin({
url: "https://custom-slack-url",
Expand All @@ -192,6 +192,8 @@ describe("postToSlack", () => {
lastRelease: "0.1.0",
commits: [makeCommitFromMsg("a patch")],
releaseNotes: "# My Notes",
// @ts-ignore
response: mockResponse,
});
expect(plugin.postToSlack).toHaveBeenCalledTimes(1);
});
Expand All @@ -205,7 +207,9 @@ describe("postToSlack", () => {
await plugin.postToSlack(
{ ...mockAuto, logger } as Auto,
"1.0.0",
"# My Notes\n- PR [some link](google.com)"
"# My Notes\n- PR [some link](google.com)",
// @ts-ignore
mockResponse
);

expect(logger.verbose.warn).toHaveBeenCalled();
Expand All @@ -218,7 +222,9 @@ describe("postToSlack", () => {
await plugin.postToSlack(
mockAuto,
"1.0.0",
"# My Notes\n- PR [some link](google.com)"
"# My Notes\n- PR [some link](google.com)",
// @ts-ignore
mockResponse
);

expect(fetchSpy).toHaveBeenCalled();
Expand All @@ -239,6 +245,8 @@ describe("postToSlack", () => {
lastRelease: "0.1.0",
commits: [makeCommitFromMsg("a patch")],
releaseNotes: "# My Notes\n- PR [some link](google.com)",
// @ts-ignore
response: mockResponse,
});

expect(fetchSpy).toHaveBeenCalled();
Expand All @@ -262,6 +270,8 @@ describe("postToSlack", () => {
lastRelease: "0.1.0",
commits: [makeCommitFromMsg("a patch")],
releaseNotes: "# My Notes\n- PR [some link](google.com)",
// @ts-ignore
response: mockResponse,
});

expect(fetchSpy).toHaveBeenCalled();
Expand All @@ -284,6 +294,8 @@ describe("postToSlack", () => {
lastRelease: "0.1.0",
commits: [makeCommitFromMsg("a patch")],
releaseNotes: "# My Notes\n- PR [some link](google.com)",
// @ts-ignore
response: mockResponse,
});

expect(fetchSpy).toHaveBeenCalled();
Expand Down
4 changes: 2 additions & 2 deletions plugins/slack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@
"dependencies": {
"@atomist/slack-messages": "~1.1.0",
"@auto-it/core": "link:../../packages/core",
"@octokit/rest": "16.43.1",
"fp-ts": "^2.5.3",
"io-ts": "^2.1.2",
"node-fetch": "2.6.0",
"tslib": "1.11.1",
"url-join": "^4.0.0"
"tslib": "1.11.1"
},
"devDependencies": {
"@types/node-fetch": "2.5.5",
Expand Down
32 changes: 22 additions & 10 deletions plugins/slack/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { githubToSlack } from "@atomist/slack-messages";
import { Octokit } from "@octokit/rest";

import {
Auto,
IPlugin,
Expand All @@ -7,7 +9,6 @@ import {
validatePluginConfiguration,
} from "@auto-it/core";
import fetch from "node-fetch";
import join from "url-join";
import * as t from "io-ts";

/** Transform markdown into slack friendly text */
Expand Down Expand Up @@ -80,7 +81,7 @@ export default class SlackPlugin implements IPlugin {

auto.hooks.afterRelease.tapPromise(
this.name,
async ({ newVersion, commits, releaseNotes }) => {
async ({ newVersion, commits, releaseNotes, response }) => {
// Avoid publishing on prerelease branches by default, but allow folks to opt in if they care to
const currentBranch = getCurrentBranch();
if (
Expand Down Expand Up @@ -116,24 +117,38 @@ export default class SlackPlugin implements IPlugin {
throw new Error("Slack url must be set to post a message to slack.");
}

await this.postToSlack(auto, newVersion, releaseNotes);
await this.postToSlack(
auto,
newVersion,
releaseNotes,
(Array.isArray(response) && response) ||
(response && [response]) ||
[]
);
}
);
}

/** Post the release notes to slack */
async postToSlack(auto: Auto, newVersion: string, releaseNotes: string) {
async postToSlack(
auto: Auto,
newVersion: string,
releaseNotes: string,
releases: Octokit.Response<Octokit.ReposCreateReleaseResponse>[]
) {
if (!auto.git) {
return;
}

auto.logger.verbose.info("Posting release notes to slack.");

const project = await auto.git.getProject();
const body = sanitizeMarkdown(releaseNotes);
const token = process.env.SLACK_TOKEN;
const releaseUrl = join(project.html_url, "releases/tag", newVersion);
const atTarget = this.options.atTarget;
const urls = releases.map(
(release) => `*<${release.data.html_url}|${release.data.name}>*`
);
const releaseUrl = urls.length ? urls.join(", ") : newVersion;

if (!token) {
auto.logger.verbose.warn("Slack may need a token to send a message");
Expand All @@ -142,10 +157,7 @@ export default class SlackPlugin implements IPlugin {
await fetch(`${this.options.url}${token ? `?token=${token}` : ""}`, {
method: "POST",
body: JSON.stringify({
text: [
`@${atTarget}: New release *<${releaseUrl}|${newVersion}>*`,
body,
].join("\n"),
text: [`@${atTarget}: New release ${releaseUrl}`, body].join("\n"),
link_names: 1,
}),
headers: { "Content-Type": "application/json" },
Expand Down
23 changes: 0 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2747,16 +2747,6 @@
regexpp "^3.0.0"
tsutils "^3.17.1"

"@typescript-eslint/experimental-utils@2.25.0":
version "2.25.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.25.0.tgz#13691c4fe368bd377b1e5b1e4ad660b220bf7714"
integrity sha512-0IZ4ZR5QkFYbaJk+8eJ2kYeA+1tzOE1sBjbwwtSV85oNWYUBep+EyhlZ7DLUCyhMUGuJpcCCFL0fDtYAP1zMZw==
dependencies:
"@types/json-schema" "^7.0.3"
"@typescript-eslint/typescript-estree" "2.25.0"
eslint-scope "^5.0.0"
eslint-utils "^2.0.0"

"@typescript-eslint/experimental-utils@2.26.0", "@typescript-eslint/experimental-utils@^2.5.0":
version "2.26.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/experimental-utils/-/experimental-utils-2.26.0.tgz#063390c404d9980767d76274df386c0aa675d91d"
Expand All @@ -2777,19 +2767,6 @@
"@typescript-eslint/typescript-estree" "2.26.0"
eslint-visitor-keys "^1.1.0"

"@typescript-eslint/typescript-estree@2.25.0":
version "2.25.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.25.0.tgz#b790497556734b7476fa7dd3fa539955a5c79e2c"
integrity sha512-VUksmx5lDxSi6GfmwSK7SSoIKSw9anukWWNitQPqt58LuYrKalzsgeuignbqnB+rK/xxGlSsCy8lYnwFfB6YJg==
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.26.0":
version "2.26.0"
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-2.26.0.tgz#d8132cf1ee8a72234f996519a47d8a9118b57d56"
Expand Down

0 comments on commit 26f635b

Please sign in to comment.