From e5af087fb5917bd58041557a5e7f1a49cbac1395 Mon Sep 17 00:00:00 2001 From: Alec Jacobs Date: Thu, 26 May 2022 10:19:12 -0700 Subject: [PATCH] feat(plugins/pagerduty): add getServiceByServiceId to PagerDutyClient * extract common params to helper constant Signed-off-by: Alec Jacobs --- plugins/pagerduty/src/api/client.ts | 16 +++++++++++++++- plugins/pagerduty/src/api/types.ts | 10 ++++++++++ .../src/components/PagerDutyCard/index.test.tsx | 1 + 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/plugins/pagerduty/src/api/client.ts b/plugins/pagerduty/src/api/client.ts index 43c6165c91..db883e8e35 100644 --- a/plugins/pagerduty/src/api/client.ts +++ b/plugins/pagerduty/src/api/client.ts @@ -19,6 +19,7 @@ import { PagerDutyApi, TriggerAlarmRequest, ServicesResponse, + ServiceResponse, IncidentsResponse, OnCallsResponse, ClientApiConfig, @@ -38,6 +39,9 @@ export const pagerDutyApiRef = createApiRef({ 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 { - 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 { + const params = commonGetServiceParams; + const url = `${await this.config.discoveryApi.getBaseUrl( + 'proxy', + )}/pagerduty/services/${serviceId}?${params}`; + const { service } = await this.getByUrl(url); + + return service; + } + async getIncidentsByServiceId(serviceId: string): Promise { const params = `time_zone=UTC&sort_by=created_at&statuses[]=triggered&statuses[]=acknowledged&service_ids[]=${serviceId}`; const url = `${await this.config.discoveryApi.getBaseUrl( diff --git a/plugins/pagerduty/src/api/types.ts b/plugins/pagerduty/src/api/types.ts index 8acd24f033..e473529980 100644 --- a/plugins/pagerduty/src/api/types.ts +++ b/plugins/pagerduty/src/api/types.ts @@ -31,6 +31,12 @@ export interface PagerDutyApi { */ getServiceByIntegrationKey(integrationKey: string): Promise; + /** + * Fetches the service for the provided service id. + * + */ + getServiceByServiceId(serviceId: string): Promise; + /** * 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[]; }; diff --git a/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx b/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx index 1195d6e7ee..e633dde0c8 100644 --- a/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx +++ b/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx @@ -27,6 +27,7 @@ import { ApiProvider } from '@backstage/core-app-api'; const mockPagerDutyApi: Partial = { getServiceByIntegrationKey: async () => [], + getServiceByServiceId: async () => service, getOnCallByPolicyId: async () => [], getIncidentsByServiceId: async () => [], };