feat(plugins/pagerduty): add getServiceByServiceId to PagerDutyClient
* extract common params to helper constant Signed-off-by: Alec Jacobs <cajacobs5401@gmail.com>
This commit is contained in:
@@ -19,6 +19,7 @@ import {
|
||||
PagerDutyApi,
|
||||
TriggerAlarmRequest,
|
||||
ServicesResponse,
|
||||
ServiceResponse,
|
||||
IncidentsResponse,
|
||||
OnCallsResponse,
|
||||
ClientApiConfig,
|
||||
@@ -38,6 +39,9 @@ export const pagerDutyApiRef = createApiRef<PagerDutyApi>({
|
||||
id: 'plugin.pagerduty.api',
|
||||
});
|
||||
|
||||
const commonGetServiceParams =
|
||||
'time_zone=UTC&include[]=integrations&include[]=escalation_policies';
|
||||
|
||||
export class PagerDutyClient implements PagerDutyApi {
|
||||
static fromConfig(
|
||||
configApi: ConfigApi,
|
||||
@@ -56,7 +60,7 @@ export class PagerDutyClient implements PagerDutyApi {
|
||||
constructor(private readonly config: ClientApiConfig) {}
|
||||
|
||||
async getServiceByIntegrationKey(integrationKey: string): Promise<Service[]> {
|
||||
const params = `time_zone=UTC&include[]=integrations&include[]=escalation_policies&query=${integrationKey}`;
|
||||
const params = `${commonGetServiceParams}&query=${integrationKey}`;
|
||||
const url = `${await this.config.discoveryApi.getBaseUrl(
|
||||
'proxy',
|
||||
)}/pagerduty/services?${params}`;
|
||||
@@ -65,6 +69,16 @@ export class PagerDutyClient implements PagerDutyApi {
|
||||
return services;
|
||||
}
|
||||
|
||||
async getServiceByServiceId(serviceId: string): Promise<Service> {
|
||||
const params = commonGetServiceParams;
|
||||
const url = `${await this.config.discoveryApi.getBaseUrl(
|
||||
'proxy',
|
||||
)}/pagerduty/services/${serviceId}?${params}`;
|
||||
const { service } = await this.getByUrl<ServiceResponse>(url);
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
async getIncidentsByServiceId(serviceId: string): Promise<Incident[]> {
|
||||
const params = `time_zone=UTC&sort_by=created_at&statuses[]=triggered&statuses[]=acknowledged&service_ids[]=${serviceId}`;
|
||||
const url = `${await this.config.discoveryApi.getBaseUrl(
|
||||
|
||||
@@ -31,6 +31,12 @@ export interface PagerDutyApi {
|
||||
*/
|
||||
getServiceByIntegrationKey(integrationKey: string): Promise<Service[]>;
|
||||
|
||||
/**
|
||||
* Fetches the service for the provided service id.
|
||||
*
|
||||
*/
|
||||
getServiceByServiceId(serviceId: string): Promise<Service>;
|
||||
|
||||
/**
|
||||
* Fetches a list of incidents a provided service has.
|
||||
*
|
||||
@@ -59,6 +65,10 @@ export type ServicesResponse = {
|
||||
services: Service[];
|
||||
};
|
||||
|
||||
export type ServiceResponse = {
|
||||
service: Service;
|
||||
};
|
||||
|
||||
export type IncidentsResponse = {
|
||||
incidents: Incident[];
|
||||
};
|
||||
|
||||
@@ -27,6 +27,7 @@ import { ApiProvider } from '@backstage/core-app-api';
|
||||
|
||||
const mockPagerDutyApi: Partial<PagerDutyClient> = {
|
||||
getServiceByIntegrationKey: async () => [],
|
||||
getServiceByServiceId: async () => service,
|
||||
getOnCallByPolicyId: async () => [],
|
||||
getIncidentsByServiceId: async () => [],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user