From ce89b1801ef25fd6052135d68b4e175a9b0499e2 Mon Sep 17 00:00:00 2001 From: Alec Jacobs Date: Fri, 17 Jun 2022 09:28:19 -0700 Subject: [PATCH] feat(plugins/pagerduty): add pagerDutyEntity helper * parse a PagerDutyEntity from a Backstage Entity Signed-off-by: Alec Jacobs --- .../src/components/pagerDutyEntity.test.ts | 98 +++++++++++++++++++ .../src/components/pagerDutyEntity.ts | 29 ++++++ 2 files changed, 127 insertions(+) create mode 100644 plugins/pagerduty/src/components/pagerDutyEntity.test.ts create mode 100644 plugins/pagerduty/src/components/pagerDutyEntity.ts diff --git a/plugins/pagerduty/src/components/pagerDutyEntity.test.ts b/plugins/pagerduty/src/components/pagerDutyEntity.test.ts new file mode 100644 index 0000000000..cf551d3d6e --- /dev/null +++ b/plugins/pagerduty/src/components/pagerDutyEntity.test.ts @@ -0,0 +1,98 @@ +/* + * Copyright 2020 The Backstage Authors + * + * 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 { Entity } from '@backstage/catalog-model'; +import { getPagerDutyEntity } from './pagerDutyEntity'; + +const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'pagerduty-test', + annotations: { + 'pagerduty.com/integration-key': 'abc123', + }, + }, +}; + +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', + }, + }, +}; + +describe('getPagerDutyEntity', () => { + describe('when entity has no annotations', () => { + it('returns a PagerDutyEntity', () => { + expect(getPagerDutyEntity(entityWithoutAnnotations)).toEqual({ + name: 'pagerduty-test', + }); + }); + }); + + describe('when entity has integration key annotation', () => { + it('returns a PagerDutyEntity', () => { + expect(getPagerDutyEntity(entity)).toEqual({ + name: 'pagerduty-test', + integrationKey: 'abc123', + }); + }); + }); + + describe('when entity has service id annotation', () => { + it('returns a PagerDutyEntity', () => { + expect(getPagerDutyEntity(entityWithServiceId)).toEqual({ + name: 'pagerduty-test', + serviceId: 'def456', + }); + }); + }); + + describe('when entity has all annotations', () => { + it('returns a PagerDutyEntity', () => { + expect(getPagerDutyEntity(entityWithAllAnnotations)).toEqual({ + name: 'pagerduty-test', + integrationKey: 'abc123', + serviceId: 'def456', + }); + }); + }); +}); diff --git a/plugins/pagerduty/src/components/pagerDutyEntity.ts b/plugins/pagerduty/src/components/pagerDutyEntity.ts new file mode 100644 index 0000000000..950525881e --- /dev/null +++ b/plugins/pagerduty/src/components/pagerDutyEntity.ts @@ -0,0 +1,29 @@ +/* + * Copyright 2020 The Backstage Authors + * + * 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 { Entity } from '@backstage/catalog-model'; +import { PagerDutyEntity } from '../types'; +import { PAGERDUTY_INTEGRATION_KEY, PAGERDUTY_SERVICE_ID } from './constants'; + +export function getPagerDutyEntity(entity: Entity): PagerDutyEntity { + const { + [PAGERDUTY_INTEGRATION_KEY]: integrationKey, + [PAGERDUTY_SERVICE_ID]: serviceId, + } = entity.metadata.annotations || ({} as Record); + const name = entity.metadata.name; + + return { integrationKey, serviceId, name }; +}