refactor api to use token from proxy
This commit is contained in:
@@ -68,7 +68,7 @@ import {
|
||||
} from '@roadiehq/backstage-plugin-github-pull-requests';
|
||||
import {
|
||||
isPluginApplicableToEntity as isPagerDutyAvailable,
|
||||
PagerDutyServiceCard,
|
||||
PagerDutyCard,
|
||||
} from '@backstage/plugin-pagerduty';
|
||||
|
||||
const CICDSwitcher = ({ entity }: { entity: Entity }) => {
|
||||
@@ -137,7 +137,7 @@ const OverviewContent = ({ entity }: { entity: Entity }) => (
|
||||
</Grid>
|
||||
{isPagerDutyAvailable(entity) && (
|
||||
<Grid item md={6}>
|
||||
<PagerDutyServiceCard entity={entity} />
|
||||
<PagerDutyCard entity={entity} />
|
||||
</Grid>
|
||||
)}
|
||||
<RecentCICDRunsSwitcher entity={entity} />
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
"@backstage/theme": "^0.1.1-alpha.25",
|
||||
"@backstage/catalog-model": "^0.1.1-alpha.25",
|
||||
"@backstage/test-utils": "^0.1.1-alpha.25",
|
||||
"@backstage/plugin-catalog": "^0.1.1-alpha.25",
|
||||
"@material-ui/core": "^4.11.0",
|
||||
"@material-ui/icons": "^4.9.1",
|
||||
"@material-ui/lab": "4.0.0-alpha.45",
|
||||
|
||||
+22
-38
@@ -25,55 +25,41 @@ import {
|
||||
OnCallsResponse,
|
||||
OnCall,
|
||||
} from '../components/types';
|
||||
import { PagerDutyClient } from './types';
|
||||
|
||||
export class UnauthorizedError extends Error {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
||||
export const pagerDutyApiRef = createApiRef<PagerDutyClientApi>({
|
||||
id: 'plugin.pagerduty.api',
|
||||
description: 'Used to fetch data from PagerDuty API',
|
||||
});
|
||||
|
||||
interface PagerDutyClient {
|
||||
getServiceByIntegrationKey(integrationKey: string): Promise<Service[]>;
|
||||
getIncidentsByServiceId(serviceId: string): Promise<Incident[]>;
|
||||
getOnCallByPolicyId(policyId: string): Promise<OnCall[]>;
|
||||
}
|
||||
|
||||
export class PagerDutyClientApi implements PagerDutyClient {
|
||||
private API_URL = 'https://api.pagerduty.com';
|
||||
private EVENTS_API_URL = 'https://events.pagerduty.com/v2';
|
||||
|
||||
constructor(private readonly config?: ClientApiConfig) {}
|
||||
constructor(private readonly config: ClientApiConfig) {}
|
||||
|
||||
async getServiceByIntegrationKey(integrationKey: string): Promise<Service[]> {
|
||||
if (!this.config?.token) {
|
||||
throw new Error('Missing token');
|
||||
}
|
||||
|
||||
const params = `include[]=integrations&include[]=escalation_policies&query=${integrationKey}`;
|
||||
const url = `${this.API_URL}/services?${params}`;
|
||||
const url = `${this.config.api_url}/services?${params}`;
|
||||
const { services } = await this.getByUrl<ServicesResponse>(url);
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
async getIncidentsByServiceId(serviceId: string): Promise<Incident[]> {
|
||||
if (!this.config?.token) {
|
||||
throw new Error('Missing token');
|
||||
}
|
||||
|
||||
const params = `service_ids[]=${serviceId}`;
|
||||
const url = `${this.API_URL}/incidents?${params}`;
|
||||
const url = `${this.config.api_url}/incidents?${params}`;
|
||||
const { incidents } = await this.getByUrl<IncidentsResponse>(url);
|
||||
|
||||
return incidents;
|
||||
}
|
||||
|
||||
async getOnCallByPolicyId(policyId: string): Promise<OnCall[]> {
|
||||
if (!this.config?.token) {
|
||||
throw new Error('Missing token');
|
||||
}
|
||||
|
||||
const params = `include[]=users&escalation_policy_ids[]=${policyId}`;
|
||||
const url = `${this.API_URL}/oncalls?${params}`;
|
||||
const url = `${this.config.api_url}/oncalls?${params}`;
|
||||
const { oncalls } = await this.getByUrl<OnCallsResponse>(url);
|
||||
|
||||
return oncalls;
|
||||
@@ -110,14 +96,13 @@ export class PagerDutyClientApi implements PagerDutyClient {
|
||||
body,
|
||||
};
|
||||
|
||||
return this.request(`${this.EVENTS_API_URL}/enqueue`, options);
|
||||
return this.request(`${this.config.events_url}/enqueue`, options);
|
||||
}
|
||||
|
||||
private async getByUrl<T>(url: string): Promise<T> {
|
||||
const options = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Token token=${this.config!.token}`,
|
||||
Accept: 'application/vnd.pagerduty+json;version=2',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
@@ -131,17 +116,16 @@ export class PagerDutyClientApi implements PagerDutyClient {
|
||||
url: string,
|
||||
options: RequestOptions,
|
||||
): Promise<Response> {
|
||||
try {
|
||||
const response = await fetch(url, options);
|
||||
if (!response.ok) {
|
||||
const payload = await response.json();
|
||||
const errors = payload.errors.map((error: string) => error).join(' ');
|
||||
const message = `Request failed with ${response.status}, ${errors}`;
|
||||
throw new Error(message);
|
||||
}
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(error);
|
||||
const response = await fetch(url, options);
|
||||
if (response.status === 401) {
|
||||
throw new UnauthorizedError();
|
||||
}
|
||||
if (!response.ok) {
|
||||
const payload = await response.json();
|
||||
const errors = payload.errors.map((error: string) => error).join(' ');
|
||||
const message = `Request failed with ${response.status}, ${errors}`;
|
||||
throw new Error(message);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
export {
|
||||
PagerDutyClientApi,
|
||||
pagerDutyApiRef,
|
||||
UnauthorizedError,
|
||||
} from './client';
|
||||
export type { PagerDutyClient } from './types';
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2020 Spotify AB
|
||||
*
|
||||
* 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 { Incident, OnCall, Service } from '../components/types';
|
||||
|
||||
export interface PagerDutyClient {
|
||||
/**
|
||||
* Fetches a list of services, filtered by the provided integration key.
|
||||
*
|
||||
*/
|
||||
getServiceByIntegrationKey(integrationKey: string): Promise<Service[]>;
|
||||
|
||||
/**
|
||||
* Fetches a list of incidents a provided service has.
|
||||
*
|
||||
*/
|
||||
getIncidentsByServiceId(serviceId: string): Promise<Incident[]>;
|
||||
|
||||
/**
|
||||
* Fetches the list of users in an escalation policy.
|
||||
*
|
||||
*/
|
||||
getOnCallByPolicyId(policyId: string): Promise<OnCall[]>;
|
||||
}
|
||||
@@ -16,5 +16,5 @@
|
||||
export { plugin } from './plugin';
|
||||
export {
|
||||
isPluginApplicableToEntity,
|
||||
PagerDutyServiceCard,
|
||||
} from './components/PagerDutyServiceCard';
|
||||
PagerDutyCard,
|
||||
} from './components/PagerDutyCard';
|
||||
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
createRouteRef,
|
||||
configApiRef,
|
||||
} from '@backstage/core';
|
||||
import { pagerDutyApiRef, PagerDutyClientApi } from './api/pagerDutyClient';
|
||||
import { pagerDutyApiRef, PagerDutyClientApi } from './api';
|
||||
|
||||
export const rootRouteRef = createRouteRef({
|
||||
path: '/pagerduty',
|
||||
@@ -34,7 +34,10 @@ export const plugin = createPlugin({
|
||||
deps: { configApi: configApiRef },
|
||||
factory: ({ configApi }) =>
|
||||
new PagerDutyClientApi({
|
||||
token: configApi.getOptionalString('pagerduty.api.token'),
|
||||
api_url: `${configApi.getString(
|
||||
'backend.baseUrl',
|
||||
)}/api/proxy/pagerduty`,
|
||||
events_url: 'https://events.pagerduty.com/v2',
|
||||
}),
|
||||
}),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user