This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
132 lines (116 loc) · 4.05 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* Copyright 2020 RoadieHQ
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createApiRef, DiscoveryApi } from '@backstage/core';
import axios from 'axios';
import { IssuesCounter, IssueType, Project, Status } from '../types';
export const jiraApiRef = createApiRef<JiraAPI>({
id: 'plugin.jira.service',
description: 'Used by the Jira plugin to make requests',
});
const DEFAULT_PROXY_PATH = '/jira/api/';
const REST_API = 'rest/api/3/'
type Options = {
discoveryApi: DiscoveryApi;
/**
* Path to use for requests via the proxy, defaults to /buildkite/api
*/
proxyPath?: string;
};
export class JiraAPI {
private readonly discoveryApi: DiscoveryApi;
private readonly proxyPath: string;
constructor(options: Options) {
this.discoveryApi = options.discoveryApi;
this.proxyPath = options.proxyPath ?? DEFAULT_PROXY_PATH;
}
private generateProjectUrl = (url: string) => new URL(url).origin;
private async getApiUrl() {
const proxyUrl = await this.discoveryApi.getBaseUrl('proxy');
return `${proxyUrl}${this.proxyPath}`;
}
private convertToString = (arrayElement: Array<string>): string =>
arrayElement
.filter(Boolean)
.map(i => `'${i}'`).join(',');
private async getIssuesCountByType(
apiUrl: string,
projectKey: string,
component: string,
statusesNames: Array<string>,
issueType: string,
issueIcon: string
) {
const statusesString = this.convertToString(statusesNames);
const jql = `
project = "${projectKey}"
AND issuetype = "${issueType}"
${statusesString ? `AND status in (${statusesString})` : ''}
${component ? `AND component = ${component}` : ''}
`;
const data = {
jql,
maxResults: 0,
};
const request = await axios.post(`${apiUrl}${REST_API}search`, data);
const response = request.data;
return {
total: response.total,
name: issueType,
iconUrl: issueIcon,
} as IssuesCounter;
};
async getProjectDetails(projectKey: string, component: string, statusesNames: Array<string>) {
const apiUrl = await this.getApiUrl();
const request = await axios(`${apiUrl}${REST_API}project/${projectKey}`);
const project = request.data as Project;
// Generate counters for each issue type
const issuesTypes = project.issueTypes.map((status: IssueType) => ({
name: status.name,
iconUrl: status.iconUrl,
}));
const issuesCounterByType = await Promise.all(
issuesTypes.map(issue => {
const issueType = issue.name;
const issueIcon = issue.iconUrl;
return this.getIssuesCountByType(apiUrl, projectKey, component, statusesNames, issueType, issueIcon)
})
);
return {
project: {
name: project.name,
iconUrl: project.avatarUrls['16x16'],
type: project.projectTypeKey,
url: this.generateProjectUrl(project.self),
},
issues: 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 activityStream = request.data;
return activityStream;
}
async getStatuses() {
const apiUrl = await this.getApiUrl();
const request = await axios(`${apiUrl}${REST_API}status`);
const statuses = request.data as Array<Status>;
const formattedStatuses = statuses.length ? [...new Set(statuses.map((status) => status.name))] : [];
return formattedStatuses;
}
}