From d77e4925ee941a1ef5cddae91e696d4c9a1ac168 Mon Sep 17 00:00:00 2001 From: Johnathan Saunders Date: Tue, 14 Jan 2025 09:57:45 -0500 Subject: [PATCH 1/5] inital test for supporting fetching keys from jira if none are supplied --- src/common/jira_client.ts | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/common/jira_client.ts b/src/common/jira_client.ts index d8ce154..12e4c44 100644 --- a/src/common/jira_client.ts +++ b/src/common/jira_client.ts @@ -43,7 +43,13 @@ export class JiraClient { } extractJiraKey(input: string): JiraKey | undefined { - /** + + // if project keys are not set, fetch it using current credentials + if (!this.projectKey) { + this.getKeys() + } + + /** * Allows for grabbing of multiple keys when given as the follwoing * jira-project-key: |- foo @@ -72,6 +78,31 @@ export class JiraClient { } + /** + * Fetches all project keys from Jira for the current user + * @returns undefined + */ + async getKeys(): Promise { + + try { + const res = await this.client.get( + this.getRestApiUrl(`/rest/api/3/project`), + ); + + const body: string = await res.readBody(); + const projects = JSON.parse(body); + + projects.map((project: { key: string }) => { + this.projectKey += `${project.key}\r\n`; // added as string with \r\n to be split out to an array later + }); + + + } catch (error) { + console.error("Failed to fetch projects:", error); + } + } + + async getIssue(key: JiraKey): Promise { try { const res = await this.client.get( From 09ae7985922c98633edcb2fd405cf75f520d87bf Mon Sep 17 00:00:00 2001 From: Johnathan Saunders Date: Tue, 14 Jan 2025 10:10:14 -0500 Subject: [PATCH 2/5] updated to not require jira keys --- action.yml | 4 ++-- dist/index.js | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/action.yml b/action.yml index ffa450d..eb68290 100644 --- a/action.yml +++ b/action.yml @@ -13,8 +13,8 @@ inputs: description: 'The subdomain of JIRA cloud that you use to access it. Ex: "https://your-domain.atlassian.net"' required: true jira-project-key: - description: "Key of project in jira. First part of issue key" - required: true + description: "Key of project in jira. First part of issue key, will grab project keys using provided jira credentials if not provided." + required: false default: "" add-label-with-issue-type: description: "If set to true, a label with the issue type from Jira will be added to the pull request" diff --git a/dist/index.js b/dist/index.js index 906c7c1..3b52ce9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -265,14 +265,18 @@ class JiraClient { }); } extractJiraKey(input) { + // if project keys are not set, fetch it using current credentials + if (!this.projectKey) { + this.getKeys(); + } /** - * Allows for grabbing of multiple keys when given as the follwoing - * jira-project-key: |- - foo - bar - * or 1 key if given only as - jira-project-key: foo - */ + * Allows for grabbing of multiple keys when given as the follwoing + * jira-project-key: |- + foo + bar + * or 1 key if given only as + jira-project-key: foo + */ const keys = this.projectKey .split(/[\r\n]/) .map(input => input.trim()) @@ -288,6 +292,25 @@ class JiraClient { }); return matchingKey; } + /** + * Fetches all project keys from Jira for the current user + * @returns undefined + */ + getKeys() { + return __awaiter(this, void 0, void 0, function* () { + try { + const res = yield this.client.get(this.getRestApiUrl(`/rest/api/3/project`)); + const body = yield res.readBody(); + const projects = JSON.parse(body); + projects.map((project) => { + this.projectKey += `${project.key}\r\n`; // added as string with \r\n to be split out to an array later + }); + } + catch (error) { + console.error("Failed to fetch projects:", error); + } + }); + } getIssue(key) { return __awaiter(this, void 0, void 0, function* () { var _a; From 08ffa5ff6933c85a73bb8ac76d07f924cc49f46f Mon Sep 17 00:00:00 2001 From: Johnathan Saunders Date: Tue, 14 Jan 2025 10:13:12 -0500 Subject: [PATCH 3/5] updated to not require jira keys --- dist/index.js | 2 +- src/main.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 3b52ce9..760834b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -487,7 +487,7 @@ function run() { const jiraBaseUrl = core.getInput("jira-base-url", { required: true }); const jiraUsername = core.getInput("jira-username", { required: true }); const jiraToken = core.getInput("jira-token", { required: true }); - const jiraProjectKey = core.getInput("jira-project-key", { required: true }); + const jiraProjectKey = core.getInput("jira-project-key", { required: false }); const addLabelWithIssueType = core.getBooleanInput("add-label-with-issue-type"); const issueTypeLabelColor = core.getInput("issue-type-label-color") || "FBCA04"; const issueTypeLabelDescription = core.getInput("issue-type-label-description") || "Jira Issue Type"; diff --git a/src/main.ts b/src/main.ts index ea5968f..ecbe1bd 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,7 +15,7 @@ export async function run() { const jiraBaseUrl = core.getInput("jira-base-url", { required: true }); const jiraUsername = core.getInput("jira-username", { required: true }); const jiraToken = core.getInput("jira-token", { required: true }); - const jiraProjectKey = core.getInput("jira-project-key", { required: true }); + const jiraProjectKey = core.getInput("jira-project-key", { required: false }); const addLabelWithIssueType = core.getBooleanInput( "add-label-with-issue-type", From 0f7a6b7994d46d1baa3250af646d1822afa7dead Mon Sep 17 00:00:00 2001 From: Johnathan Saunders Date: Tue, 14 Jan 2025 10:28:52 -0500 Subject: [PATCH 4/5] awaiting getKeys --- __tests__/jira_client.test.ts | 44 ++++++++++++++--------------- dist/index.js | 52 ++++++++++++++++++----------------- src/common/jira_client.ts | 4 +-- src/main.ts | 2 +- 4 files changed, 52 insertions(+), 50 deletions(-) diff --git a/__tests__/jira_client.test.ts b/__tests__/jira_client.test.ts index 876e418..6b586b6 100644 --- a/__tests__/jira_client.test.ts +++ b/__tests__/jira_client.test.ts @@ -77,36 +77,36 @@ describe("extract jira key", () => { client = new JiraClient("base-url", "username", "token", "PRJ"); }); - it("extracts the jira key if present", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key if present", async () => { + const jiraKey = await client.extractJiraKey( "PRJ-3721_actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("PRJ-3721"); }); - it("extracts the jira key if present without underscore", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key if present without underscore", async () => { + const jiraKey = await client.extractJiraKey( "PRJ-3721-actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("PRJ-3721"); }); - it("extracts the jira key from a feature branch if present", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key from a feature branch if present", async () => { + const jiraKey = await client.extractJiraKey( "feature/PRJ-3721_actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("PRJ-3721"); }); - it("extracts the jira key case insensitive", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key case insensitive", async () => { + const jiraKey = await client.extractJiraKey( "PRJ-3721_actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("PRJ-3721"); }); - it("returns undefined if not present", () => { - const jiraKey = client.extractJiraKey( + it("returns undefined if not present", async () => { + const jiraKey = await client.extractJiraKey( "prj3721_actions-workflow-improvements", ); expect(jiraKey).toBeUndefined(); @@ -120,43 +120,43 @@ describe("extract jira key when given multiple keys", () => { client = new JiraClient("base-url", "username", "token", "PRJ\n FOO\n BAR\n"); }); - it("extracts the jira key if present", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key if present", async () => { + const jiraKey = await client.extractJiraKey( "PRJ-3721_actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("PRJ-3721"); }); - it("extracts the jira key if present", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key if present", async () => { + const jiraKey = await client.extractJiraKey( "FOO-3721_actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("FOO-3721"); }); - it("extracts the jira key if present without underscore", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key if present without underscore", async () => { + const jiraKey = await client.extractJiraKey( "PRJ-3721-actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("PRJ-3721"); }); - it("extracts the jira key from a feature branch if present", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key from a feature branch if present", async () => { + const jiraKey = await client.extractJiraKey( "feature/BAR-3721_actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("BAR-3721"); }); - it("extracts the jira key case insensitive", () => { - const jiraKey = client.extractJiraKey( + it("extracts the jira key case insensitive", async () => { + const jiraKey = await client.extractJiraKey( "PRJ-3721_actions-workflow-improvements", ); expect(jiraKey?.toString()).toBe("PRJ-3721"); }); - it("returns undefined if not present", () => { - const jiraKey = client.extractJiraKey( + it("returns undefined if not present", async () => { + const jiraKey = await client.extractJiraKey( "prj3721_actions-workflow-improvements", ); expect(jiraKey).toBeUndefined(); diff --git a/dist/index.js b/dist/index.js index 760834b..752f4e4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -265,32 +265,34 @@ class JiraClient { }); } extractJiraKey(input) { - // if project keys are not set, fetch it using current credentials - if (!this.projectKey) { - this.getKeys(); - } - /** - * Allows for grabbing of multiple keys when given as the follwoing - * jira-project-key: |- - foo - bar - * or 1 key if given only as - jira-project-key: foo - */ - const keys = this.projectKey - .split(/[\r\n]/) - .map(input => input.trim()) - .filter(input => input !== ''); // grab 1 or many project keys - let matchingKey = undefined; - keys.forEach(projectKey => { - var _a, _b; - const regex = new RegExp(`${projectKey}-(?\\d+)`, "i"); - const match = input.match(regex); - if ((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.number) { - matchingKey = new JiraKey(projectKey, (_b = match === null || match === void 0 ? void 0 : match.groups) === null || _b === void 0 ? void 0 : _b.number); + return __awaiter(this, void 0, void 0, function* () { + // if project keys are not set, fetch it using current credentials + if (!this.projectKey) { + yield this.getKeys(); } + /** + * Allows for grabbing of multiple keys when given as the follwoing + * jira-project-key: |- + foo + bar + * or 1 key if given only as + jira-project-key: foo + */ + const keys = this.projectKey + .split(/[\r\n]/) + .map(input => input.trim()) + .filter(input => input !== ''); // grab 1 or many project keys + let matchingKey = undefined; + keys.forEach(projectKey => { + var _a, _b; + const regex = new RegExp(`${projectKey}-(?\\d+)`, "i"); + const match = input.match(regex); + if ((_a = match === null || match === void 0 ? void 0 : match.groups) === null || _a === void 0 ? void 0 : _a.number) { + matchingKey = new JiraKey(projectKey, (_b = match === null || match === void 0 ? void 0 : match.groups) === null || _b === void 0 ? void 0 : _b.number); + } + }); + return matchingKey; }); - return matchingKey; } /** * Fetches all project keys from Jira for the current user @@ -504,7 +506,7 @@ function run() { core.info(`🚨 Dependabot, ignoring`); return; } - const jiraKey = jiraClient.extractJiraKey(branchName); + const jiraKey = yield jiraClient.extractJiraKey(branchName); if (!jiraKey) { core.warning("⚠️ No Jira key found in branch name, exiting"); return; diff --git a/src/common/jira_client.ts b/src/common/jira_client.ts index 12e4c44..617d742 100644 --- a/src/common/jira_client.ts +++ b/src/common/jira_client.ts @@ -42,11 +42,11 @@ export class JiraClient { }); } - extractJiraKey(input: string): JiraKey | undefined { + async extractJiraKey(input: string): Promise { // if project keys are not set, fetch it using current credentials if (!this.projectKey) { - this.getKeys() + await this.getKeys() } /** diff --git a/src/main.ts b/src/main.ts index ecbe1bd..9589db1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -49,7 +49,7 @@ export async function run() { return; } - const jiraKey = jiraClient.extractJiraKey(branchName); + const jiraKey = await jiraClient.extractJiraKey(branchName); if (!jiraKey) { core.warning("⚠️ No Jira key found in branch name, exiting"); From 098586ba94c3cc9f870581be997bb01c304aa5e3 Mon Sep 17 00:00:00 2001 From: Johnathan Saunders Date: Tue, 14 Jan 2025 10:37:32 -0500 Subject: [PATCH 5/5] removed hardcode jira path in api method, moved it to specified call --- dist/index.js | 4 ++-- src/common/jira_client.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 752f4e4..e8428e5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -317,7 +317,7 @@ class JiraClient { return __awaiter(this, void 0, void 0, function* () { var _a; try { - const res = yield this.client.get(this.getRestApiUrl(`issue/${key}?fields=issuetype,summary,fixVersions`)); + const res = yield this.client.get(this.getRestApiUrl(`/rest/api/3/issue/${key}?fields=issuetype,summary,fixVersions`)); const body = yield res.readBody(); const obj = JSON.parse(body); var issuetype = undefined; @@ -347,7 +347,7 @@ class JiraClient { }); } getRestApiUrl(endpoint) { - return `${this.baseUrl}/rest/api/3/${endpoint}`; + return `${this.baseUrl}${endpoint}`; } } exports.JiraClient = JiraClient; diff --git a/src/common/jira_client.ts b/src/common/jira_client.ts index 617d742..96c92e9 100644 --- a/src/common/jira_client.ts +++ b/src/common/jira_client.ts @@ -106,7 +106,7 @@ export class JiraClient { async getIssue(key: JiraKey): Promise { try { const res = await this.client.get( - this.getRestApiUrl(`issue/${key}?fields=issuetype,summary,fixVersions`), + this.getRestApiUrl(`/rest/api/3/issue/${key}?fields=issuetype,summary,fixVersions`), ); const body: string = await res.readBody(); const obj = JSON.parse(body); @@ -142,6 +142,6 @@ export class JiraClient { } private getRestApiUrl(endpoint: string): string { - return `${this.baseUrl}/rest/api/3/${endpoint}`; + return `${this.baseUrl}${endpoint}`; } }