From b7ebd39e1fdac3309ea82c9a4b0e984327ada6da Mon Sep 17 00:00:00 2001 From: "Gustavo T. Rudiger" Date: Fri, 20 Oct 2023 19:01:59 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20get=20jira=20issue=20fro?= =?UTF-8?q?m=20pr=20title=20or=20pr=20branch=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/index.test.ts | 67 ++++++++++++++++++++++++++++++++++++++--------- src/index.ts | 3 ++- yarn.lock | 9 +++---- 4 files changed, 61 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 3e2954a..cfd306f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.test.ts b/src/index.test.ts index 66018ed..1ed6ef8 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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(); @@ -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", + ); }); }); diff --git a/src/index.ts b/src/index.ts index 63c5a7d..5967d36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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"); diff --git a/yarn.lock b/yarn.lock index 456e46a..359fe66 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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: