Skip to content

Commit b7ebd39

Browse files
committed
feat: 🎸 get jira issue from pr title or pr branch name
1 parent fb2dd96 commit b7ebd39

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"husky": "^8.0.3",
5151
"jest": "^29.7.0",
5252
"lint-staged": "^14.0.1",
53-
"nock": "^13.3.4",
53+
"nock": "^13.3.6",
5454
"prettier": "^3.0.3",
5555
"semantic-release": "^22.0.5",
5656
"ts-jest": "^29.1.1",

src/index.test.ts

+54-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
import nock from "nock";
2+
13
import jiraPrValidation from "./index";
24

35
declare const global: any;
46

57
describe("jiraPrValidation()", () => {
8+
const baseUrl = "https://some.jira.net";
9+
const key = "PRJ";
10+
const issue = "001";
11+
612
beforeEach(() => {
13+
global.danger = {
14+
github: {
15+
pr: {
16+
title: `${key}-${issue} - My Test Title`,
17+
base: { ref: "pr-base" },
18+
head: { ref: "pr-head" },
19+
},
20+
},
21+
};
722
global.warn = jest.fn();
823
global.message = jest.fn();
924
global.fail = jest.fn();
@@ -17,21 +32,47 @@ describe("jiraPrValidation()", () => {
1732
global.markdown = undefined;
1833
});
1934

20-
it("Checks for a that message has been called", async () => {
21-
global.danger = {
22-
github: {
23-
pr: {
24-
title: "My Test Title",
25-
base: { ref: "pr-base" },
26-
head: { ref: "pr-head" },
35+
it("should not call fail message if Jira issue has no fixVersions", async () => {
36+
nock(baseUrl)
37+
.get(
38+
`/rest/api/3/issue/${key}-${issue}?fields=issuetype,summary,fixVersions`,
39+
)
40+
.reply(200, {
41+
fields: {
42+
fixVersions: [],
43+
issuetype: { name: "issue" },
44+
summary: "title",
2745
},
28-
},
29-
};
46+
});
47+
48+
await jiraPrValidation(baseUrl, "username", "token", key);
49+
50+
expect(global.message).toHaveBeenCalledWith(
51+
"Jira issue: PRJ-001 | issue | title",
52+
);
53+
expect(global.fail).not.toHaveBeenCalled();
54+
});
55+
56+
it("should call fail message if Jira issue has fixVersions that doesn`t match base branch", async () => {
57+
nock(baseUrl)
58+
.get(
59+
`/rest/api/3/issue/${key}-${issue}?fields=issuetype,summary,fixVersions`,
60+
)
61+
.reply(200, {
62+
fields: {
63+
fixVersions: [{ name: "v1" }],
64+
issuetype: { name: "issue" },
65+
summary: "title",
66+
},
67+
});
3068

31-
await jiraPrValidation("baseUrl", "username", "token", "projectKey");
69+
await jiraPrValidation(baseUrl, "username", "token", key);
3270

33-
expect(global.message).toHaveBeenCalledWith("PR Title: My Test Title");
34-
expect(global.message).toHaveBeenCalledWith("PR Base: pr-base");
35-
expect(global.message).toHaveBeenCalledWith("PR Head: pr-head");
71+
expect(global.message).toHaveBeenCalledWith(
72+
"Jira issue: PRJ-001 | issue | title\nFix versions: v1",
73+
);
74+
expect(global.fail).toHaveBeenCalledWith(
75+
"🚨 Base branch doesn't match Jira fixVersion",
76+
);
3677
});
3778
});

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ export default async function jiraPrValidation(
1616
projectKey: string,
1717
level: "fail" | "warn" = "fail",
1818
) {
19+
const title = danger.github.pr.title;
1920
const base = danger.github.pr.base.ref;
2021
const head = danger.github.pr.head.ref;
2122

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

24-
const jiraKey = jiraClient.extractJiraKey(head);
25+
const jiraKey = jiraClient.extractJiraKey(title + head);
2526

2627
if (!jiraKey) {
2728
warn("⚠️ No Jira key found in branch name, exiting");

yarn.lock

+4-5
Original file line numberDiff line numberDiff line change
@@ -5359,14 +5359,13 @@ nerf-dart@^1.0.0:
53595359
resolved "https://registry.yarnpkg.com/nerf-dart/-/nerf-dart-1.0.0.tgz#e6dab7febf5ad816ea81cf5c629c5a0ebde72c1a"
53605360
integrity sha512-EZSPZB70jiVsivaBLYDCyntd5eH8NTSMOn3rB+HxwdmKThGELLdYv8qVIMWvZEFy9w8ZZpW9h9OB32l1rGtj7g==
53615361

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

53725371
node-cleanup@^2.1.2:

0 commit comments

Comments
 (0)