Skip to content

Commit

Permalink
feat: 🎸 get jira issue from pr title or pr branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavotr committed Oct 20, 2023
1 parent fb2dd96 commit b7ebd39
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^14.0.1",
"nock": "^13.3.4",
"nock": "^13.3.6",
"prettier": "^3.0.3",
"semantic-release": "^22.0.5",
"ts-jest": "^29.1.1",
Expand Down
67 changes: 54 additions & 13 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import nock from "nock";

import jiraPrValidation from "./index";

declare const global: any;

describe("jiraPrValidation()", () => {
const baseUrl = "https://some.jira.net";
const key = "PRJ";
const issue = "001";

beforeEach(() => {
global.danger = {
github: {
pr: {
title: `${key}-${issue} - My Test Title`,
base: { ref: "pr-base" },
head: { ref: "pr-head" },
},
},
};
global.warn = jest.fn();
global.message = jest.fn();
global.fail = jest.fn();
Expand All @@ -17,21 +32,47 @@ describe("jiraPrValidation()", () => {
global.markdown = undefined;
});

it("Checks for a that message has been called", async () => {
global.danger = {
github: {
pr: {
title: "My Test Title",
base: { ref: "pr-base" },
head: { ref: "pr-head" },
it("should not call fail message if Jira issue has no fixVersions", async () => {
nock(baseUrl)
.get(
`/rest/api/3/issue/${key}-${issue}?fields=issuetype,summary,fixVersions`,
)
.reply(200, {
fields: {
fixVersions: [],
issuetype: { name: "issue" },
summary: "title",
},
},
};
});

await jiraPrValidation(baseUrl, "username", "token", key);

expect(global.message).toHaveBeenCalledWith(
"Jira issue: PRJ-001 | issue | title",
);
expect(global.fail).not.toHaveBeenCalled();
});

it("should call fail message if Jira issue has fixVersions that doesn`t match base branch", async () => {
nock(baseUrl)
.get(
`/rest/api/3/issue/${key}-${issue}?fields=issuetype,summary,fixVersions`,
)
.reply(200, {
fields: {
fixVersions: [{ name: "v1" }],
issuetype: { name: "issue" },
summary: "title",
},
});

await jiraPrValidation("baseUrl", "username", "token", "projectKey");
await jiraPrValidation(baseUrl, "username", "token", key);

expect(global.message).toHaveBeenCalledWith("PR Title: My Test Title");
expect(global.message).toHaveBeenCalledWith("PR Base: pr-base");
expect(global.message).toHaveBeenCalledWith("PR Head: pr-head");
expect(global.message).toHaveBeenCalledWith(
"Jira issue: PRJ-001 | issue | title\nFix versions: v1",
);
expect(global.fail).toHaveBeenCalledWith(
"🚨 Base branch doesn't match Jira fixVersion",
);
});
});
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ export default async function jiraPrValidation(
projectKey: string,
level: "fail" | "warn" = "fail",
) {
const title = danger.github.pr.title;
const base = danger.github.pr.base.ref;
const head = danger.github.pr.head.ref;

const jiraClient = new JiraClient(baseUrl, username, token, projectKey);

const jiraKey = jiraClient.extractJiraKey(head);
const jiraKey = jiraClient.extractJiraKey(title + head);

if (!jiraKey) {
warn("⚠️ No Jira key found in branch name, exiting");
Expand Down
9 changes: 4 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5359,14 +5359,13 @@ nerf-dart@^1.0.0:
resolved "https://registry.yarnpkg.com/nerf-dart/-/nerf-dart-1.0.0.tgz#e6dab7febf5ad816ea81cf5c629c5a0ebde72c1a"
integrity sha512-EZSPZB70jiVsivaBLYDCyntd5eH8NTSMOn3rB+HxwdmKThGELLdYv8qVIMWvZEFy9w8ZZpW9h9OB32l1rGtj7g==

nock@^13.3.4:
version "13.3.4"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.3.4.tgz#4ed3ed1465a75c87833044a881dbdd6546337e8d"
integrity sha512-DDpmn5oLEdCTclEqweOT4U7bEpuoifBMFUXem9sA4turDAZ5tlbrEoWqCorwXey8CaAw44mst5JOQeVNiwtkhw==
nock@^13.3.6:
version "13.3.6"
resolved "https://registry.yarnpkg.com/nock/-/nock-13.3.6.tgz#b279968ec8d076c2393810a6c9bf2d4d5b3a1071"
integrity sha512-lT6YuktKroUFM+27mubf2uqQZVy2Jf+pfGzuh9N6VwdHlFoZqvi4zyxFTVR1w/ChPqGY6yxGehHp6C3wqCASCw==
dependencies:
debug "^4.1.0"
json-stringify-safe "^5.0.1"
lodash "^4.17.21"
propagate "^2.0.0"

node-cleanup@^2.1.2:
Expand Down

0 comments on commit b7ebd39

Please sign in to comment.