From 7959a35cc14d1954ee06fa43806dd4e3b6c5d168 Mon Sep 17 00:00:00 2001 From: Alec Jacobs Date: Wed, 25 May 2022 15:52:34 -0700 Subject: [PATCH] feat(plugins/pagerduty): check for service id annotation for applicable entities Signed-off-by: Alec Jacobs --- .../components/PagerDutyCard/index.test.tsx | 65 ++++++++++++++++++- .../src/components/PagerDutyCard/index.tsx | 7 +- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx b/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx index e0a857ccc7..1195d6e7ee 100644 --- a/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx +++ b/plugins/pagerduty/src/components/PagerDutyCard/index.test.tsx @@ -15,7 +15,7 @@ */ import React from 'react'; import { render, waitFor, fireEvent, act } from '@testing-library/react'; -import { PagerDutyCard } from '../PagerDutyCard'; +import { PagerDutyCard, isPluginApplicableToEntity } from '../PagerDutyCard'; import { Entity } from '@backstage/catalog-model'; import { EntityProvider } from '@backstage/plugin-catalog-react'; import { TestApiRegistry, wrapInTestApp } from '@backstage/test-utils'; @@ -35,6 +35,7 @@ const apis = TestApiRegistry.from( [pagerDutyApiRef, mockPagerDutyApi], [alertApiRef, {}], ); + const entity: Entity = { apiVersion: 'backstage.io/v1alpha1', kind: 'Component', @@ -46,6 +47,38 @@ const entity: Entity = { }, }; +const entityWithoutAnnotations: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'pagerduty-test', + annotations: {}, + }, +}; + +const entityWithServiceId: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'pagerduty-test', + annotations: { + 'pagerduty.com/service-id': 'def456', + }, + }, +}; + +const entityWithAllAnnotations: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'pagerduty-test', + annotations: { + 'pagerduty.com/integration-key': 'abc123', + 'pagerduty.com/service-id': 'def456', + }, + }, +}; + const user: User = { name: 'person1', id: 'p1', @@ -55,7 +88,7 @@ const user: User = { }; const service: Service = { - id: 'abc', + id: 'def456', name: 'pagerduty-name', html_url: 'www.example.com', escalation_policy: { @@ -63,9 +96,35 @@ const service: Service = { user: user, html_url: 'http://a.com/id1', }, - integrationKey: 'abcd', + integrationKey: 'abc123', }; +describe('isPluginApplicableToEntity', () => { + describe('when entity has no annotations', () => { + it('returns false', () => { + expect(isPluginApplicableToEntity(entityWithoutAnnotations)).toBe(false); + }); + }); + + describe('when entity has the pagerduty.com/integration-key annotation', () => { + it('returns true', () => { + expect(isPluginApplicableToEntity(entity)).toBe(true); + }); + }); + + describe('when entity has the pagerduty.com/service-id annotation', () => { + it('returns true', () => { + expect(isPluginApplicableToEntity(entityWithServiceId)).toBe(true); + }); + }); + + describe('when entity has all annotations', () => { + it('returns true', () => { + expect(isPluginApplicableToEntity(entityWithAllAnnotations)).toBe(true); + }); + }); +}); + describe('PageDutyCard', () => { it('Render pagerduty', async () => { mockPagerDutyApi.getServiceByIntegrationKey = jest diff --git a/plugins/pagerduty/src/components/PagerDutyCard/index.tsx b/plugins/pagerduty/src/components/PagerDutyCard/index.tsx index bbade09e51..0938f7288b 100644 --- a/plugins/pagerduty/src/components/PagerDutyCard/index.tsx +++ b/plugins/pagerduty/src/components/PagerDutyCard/index.tsx @@ -26,7 +26,7 @@ import { MissingTokenError } from '../Errors/MissingTokenError'; import WebIcon from '@material-ui/icons/Web'; import DateRangeIcon from '@material-ui/icons/DateRange'; import { usePagerdutyEntity } from '../../hooks'; -import { PAGERDUTY_INTEGRATION_KEY } from '../constants'; +import { PAGERDUTY_INTEGRATION_KEY, PAGERDUTY_SERVICE_ID } from '../constants'; import { TriggerDialog } from '../TriggerDialog'; import { ChangeEvents } from '../ChangeEvents'; @@ -40,7 +40,10 @@ import { } from '@backstage/core-components'; export const isPluginApplicableToEntity = (entity: Entity) => - Boolean(entity.metadata.annotations?.[PAGERDUTY_INTEGRATION_KEY]); + Boolean( + entity.metadata.annotations?.[PAGERDUTY_INTEGRATION_KEY] || + entity.metadata.annotations?.[PAGERDUTY_SERVICE_ID], + ); export const PagerDutyCard = () => { const { integrationKey } = usePagerdutyEntity();