Skip to content

Commit

Permalink
Add additional URL path to Jira links (AxisCommunications#113)
Browse files Browse the repository at this point in the history
* Added url pathname as part of Jira URL

* Add changeset

* Add tests

* Remove v2 from split function

---------

Co-authored-by: Frida Jacobsson <[email protected]>
  • Loading branch information
fridajac and Frida Jacobsson authored Apr 2, 2024
1 parent 603304b commit 5457a70
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/soft-readers-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@axis-backstage/plugin-jira-dashboard': patch
---

Added the url pathname as part of the base URL for all links to Jira. This because Jira Server have additional path in the Jira base URl and links were broken.
60 changes: 60 additions & 0 deletions plugins/jira-dashboard/src/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { getJiraBaseUrl, getProjectUrl, getIssueUrl } from './lib';

const mockedProject = {
name: 'Backstage',
key: 'BS',
description: 'This is our Backstage project',
avatarUrls: {
'48x48': 'https://api.dicebear.com/6.x/open-peeps/svg?seed=Duane',
},
projectTypeKey: 'Software',
projectCategory: {
name: 'Software Portals',
},
lead: {
key: 'fridaja',
displayName: 'Frida Jacobsson',
},
self: 'https://jira.com/rest/api/2/project/123',
};

describe('getJiraBaseUrl', () => {
it('should return the base URL of a Jira instance', () => {
const baseUrl = 'https://jira.com/rest/api/2/project/123';
const expected = 'https://jira.com';
expect(getJiraBaseUrl(baseUrl)).toEqual(expected);
});

it('should handle URLs with additional paths', () => {
const baseUrl = 'https://jira.com/my-path/rest/api/2/project/123';
const expected = 'https://jira.com/my-path';
expect(getJiraBaseUrl(baseUrl)).toEqual(expected);
});
});

describe('getProjectUrl', () => {
it('should return the URL to a Jira project', () => {
const expected = 'https://jira.com/browse/BS';
expect(getProjectUrl(mockedProject)).toEqual(expected);
});
it('should handle URLs with additional paths', () => {
const expected = 'https://jira.com/my-path/browse/BS';
mockedProject.self = 'https://jira.com/my-path/rest/api/2/project/123';
expect(getProjectUrl(mockedProject)).toEqual(expected);
});
});

describe('getIssueUrl', () => {
it('should return the URL to an issue', () => {
const issueUrl = 'https://jira.com/rest/api/2/issue/123';
const issueKey = 'PROJ-123';
const expected = 'https://jira.com/browse/PROJ-123';
expect(getIssueUrl(issueUrl, issueKey)).toEqual(expected);
});
it('should handle URLs with additional paths', () => {
const issueUrl = 'https://jira.com/my-path/rest/api/2/issue/123';
const issueKey = 'PROJ-123';
const expected = 'https://jira.com/my-path/browse/PROJ-123';
expect(getIssueUrl(issueUrl, issueKey)).toEqual(expected);
});
});
12 changes: 8 additions & 4 deletions plugins/jira-dashboard/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { Project } from '@axis-backstage/plugin-jira-dashboard-common';

export const getJiraBaseUrl = (a: string) => {
const url = new URL(a);
const path = url.pathname.split('/rest/api')[0];
return url.origin + path;
};

/**
* Get the URL to a Jira project.
*/
export const getProjectUrl = (project: Project) => {
const url = new URL(project.self);
return `https://${url.host}/browse/${project.key}`;
return `${getJiraBaseUrl(project.self)}/browse/${project.key}`;
};

/**
* Get the URL to a issue.
*/
export const getIssueUrl = (issueUrl: string, issueKey: string) => {
const url = new URL(issueUrl);
return `https://${url.host}/browse/${issueKey}`;
return `${getJiraBaseUrl(issueUrl)}/browse/${issueKey}`;
};

0 comments on commit 5457a70

Please sign in to comment.