Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch key from jira #176

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions __tests__/jira_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
73 changes: 49 additions & 24 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,34 +265,59 @@ class JiraClient {
});
}
extractJiraKey(input) {
/**
* 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}-(?<number>\\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}-(?<number>\\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;
});
}
/**
* 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);
}
});
return matchingKey;
}
getIssue(key) {
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;
Expand Down Expand Up @@ -322,7 +347,7 @@ class JiraClient {
});
}
getRestApiUrl(endpoint) {
return `${this.baseUrl}/rest/api/3/${endpoint}`;
return `${this.baseUrl}${endpoint}`;
}
}
exports.JiraClient = JiraClient;
Expand Down Expand Up @@ -464,7 +489,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";
Expand All @@ -481,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;
Expand Down
39 changes: 35 additions & 4 deletions src/common/jira_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ export class JiraClient {
});
}

extractJiraKey(input: string): JiraKey | undefined {
/**
async extractJiraKey(input: string): Promise<JiraKey | undefined> {

// if project keys are not set, fetch it using current credentials
if (!this.projectKey) {
await this.getKeys()
}

/**
* Allows for grabbing of multiple keys when given as the follwoing
* jira-project-key: |-
foo
Expand Down Expand Up @@ -72,10 +78,35 @@ export class JiraClient {

}

/**
* Fetches all project keys from Jira for the current user
* @returns undefined
*/
async getKeys(): Promise<undefined> {

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<JiraIssue | undefined> {
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);
Expand Down Expand Up @@ -111,6 +142,6 @@ export class JiraClient {
}

private getRestApiUrl(endpoint: string): string {
return `${this.baseUrl}/rest/api/3/${endpoint}`;
return `${this.baseUrl}${endpoint}`;
}
}
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
Expand Down
Loading