Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Feat/add api v2 support #5

Merged
merged 3 commits into from
Nov 26, 2020
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ proxy:
Content-Type: 'application/json'
X-Atlassian-Token: 'no-check'
User-Agent: "MY-UA-STRING"
// For Jira Server / Data Center users you also need to set up API version
jira:
apiVersion: 2
```

3. Add plugin to the list of plugins:
Expand Down
24 changes: 20 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@roadiehq/backstage-plugin-jira",
"version": "0.1.0",
"version": "0.2.0",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
Expand All @@ -20,7 +20,7 @@
"clean": "backstage-cli clean"
},
"dependencies": {
"@backstage/catalog-model": "^0.2.0",
"@backstage/catalog-model": "^0.3.0",
"@backstage/core": "^0.3.0",
"@backstage/theme": "^0.2.1",
"@material-ui/core": "^4.11.0",
Expand All @@ -35,7 +35,7 @@
"xml-js": "^1.6.11"
},
"devDependencies": {
"@backstage/cli": "^0.2.0",
"@backstage/cli": "^0.3.1",
"@backstage/dev-utils": "^0.1.3",
"@backstage/test-utils": "^0.1.2",
"@testing-library/jest-dom": "^5.10.1",
Expand All @@ -48,5 +48,21 @@
},
"files": [
"dist"
]
],
"configSchema": {
"$schema": "https://backstage.io/schema/config-v1",
"title": "@roadiehq/backstage-plugin-jira",
"type": "object",
"properties": {
"jira": {
"type": "object",
"properties": {
"apiVersion": {
"type": "number",
"visibility": "frontend"
}
}
}
}
}
}
32 changes: 19 additions & 13 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,37 @@ export const jiraApiRef = createApiRef<JiraAPI>({
});

const DEFAULT_PROXY_PATH = '/jira/api/';
const REST_API = 'rest/api/3/'
const DEFAULT_REST_API_VERSION = 3;

type Options = {
discoveryApi: DiscoveryApi;
/**
* Path to use for requests via the proxy, defaults to /buildkite/api
* Path to use for requests via the proxy, defaults to /jira/api
*/
proxyPath?: string;
apiVersion?: number;
};

export class JiraAPI {
private readonly discoveryApi: DiscoveryApi;
private readonly proxyPath: string;
private readonly apiVersion: number;

constructor(options: Options) {
this.discoveryApi = options.discoveryApi;
this.proxyPath = options.proxyPath ?? DEFAULT_PROXY_PATH;
this.apiVersion = options.apiVersion ?? DEFAULT_REST_API_VERSION;
}

private generateProjectUrl = (url: string) => new URL(url).origin;

private async getApiUrl() {
private async getUrls() {
const proxyUrl = await this.discoveryApi.getBaseUrl('proxy');
return `${proxyUrl}${this.proxyPath}`;
}
return {
apiUrl: `${proxyUrl}${this.proxyPath}/rest/api/${this.apiVersion}/`,
baseUrl: `${proxyUrl}${this.proxyPath}`,
}
};

private convertToString = (arrayElement: Array<string>): string =>
arrayElement
Expand All @@ -74,7 +80,7 @@ export class JiraAPI {
jql,
maxResults: 0,
};
const request = await axios.post(`${apiUrl}${REST_API}search`, data);
const request = await axios.post(`${apiUrl}search`, data);
const response = request.data;
return {
total: response.total,
Expand All @@ -84,8 +90,8 @@ export class JiraAPI {
};

async getProjectDetails(projectKey: string, component: string, statusesNames: Array<string>) {
const apiUrl = await this.getApiUrl();
const request = await axios(`${apiUrl}${REST_API}project/${projectKey}`);
const { apiUrl } = await this.getUrls();
const request = await axios.get(`${apiUrl}project/${projectKey}`);
const project = request.data as Project;

// Generate counters for each issue type
Expand All @@ -109,22 +115,22 @@ export class JiraAPI {
type: project.projectTypeKey,
url: this.generateProjectUrl(project.self),
},
issues: issuesCounterByType.length ? issuesCounterByType.map(status => ({
issues: issuesCounterByType && issuesCounterByType.length ? issuesCounterByType.map(status => ({
...status,
})) : []
};
}

async getActivityStream(size: number) {
const apiUrl = await this.getApiUrl();
const request = await axios(`${apiUrl}activity?maxResults=${size}&os_authType=basic`);
const { baseUrl } = await this.getUrls();
const request = await axios.get(`${baseUrl}activity?maxResults=${size}&os_authType=basic`);
const activityStream = request.data;
return activityStream;
}

async getStatuses() {
const apiUrl = await this.getApiUrl();
const request = await axios(`${apiUrl}${REST_API}status`);
const { apiUrl } = await this.getUrls();
const request = await axios.get(`${apiUrl}status`);
const statuses = request.data as Array<Status>;
const formattedStatuses = statuses.length ? [...new Set(statuses.map((status) => status.name))] : [];
return formattedStatuses;
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useProjectInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useProjectInfo = (

const getProjectDetails = useCallback(async () => {
try {
setTimeout(() => (document.activeElement as HTMLElement).blur());
setTimeout(() => (document.activeElement as HTMLElement).blur(), 0);
return await api.getProjectDetails(projectKey, component, statusesNames);
} catch (err) {
return handleError(err);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
import { AxiosError } from 'axios';

export const handleError = (error: AxiosError) => Promise.reject({
message: error?.response?.data?.errorMessages.length && error.response.data.errorMessages[0] || error.request
message: error?.response?.data?.errorMessages && error.response.data.errorMessages[0].toString() || error?.message || error?.request || error.toString(),
});
10 changes: 8 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
import {
configApiRef,
createPlugin,
createRouteRef,
createApiFactory,
Expand All @@ -31,8 +32,13 @@ export const plugin = createPlugin({
apis: [
createApiFactory({
api: jiraApiRef,
deps: { discoveryApi: discoveryApiRef },
factory: ({ discoveryApi }) => new JiraAPI({ discoveryApi }),
deps: { discoveryApi: discoveryApiRef, configApi: configApiRef },
factory: ({ discoveryApi, configApi }) => {
return new JiraAPI({
discoveryApi,
apiVersion: configApi.getOptionalNumber('jira.apiVersion'),
});
}
}),
],
});