feat(plugins/pagerduty): check for service id annotation for applicable entities

Signed-off-by: Alec Jacobs <cajacobs5401@gmail.com>
This commit is contained in:
Alec Jacobs
2022-05-25 15:52:34 -07:00
parent 0e8fc43492
commit 7959a35cc1
2 changed files with 67 additions and 5 deletions
@@ -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
@@ -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();