Skip to content

Commit

Permalink
Merge pull request #1096 from intuit/link-bug
Browse files Browse the repository at this point in the history
use release URL from response for an accurate URL
  • Loading branch information
hipstersmoothie authored Apr 1, 2020
2 parents ec1e9f4 + 26f635b commit aa923fc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 31 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

0 comments on commit aa923fc

Please sign in to comment.