feat(plugins/pagerduty): refactor to use PagerDutyClient.getServiceByEntity
Signed-off-by: Alec Jacobs <cajacobs5401@gmail.com>
This commit is contained in:
@@ -90,8 +90,7 @@ const service: Service = {
|
||||
};
|
||||
|
||||
const mockPagerDutyApi: Partial<PagerDutyClient> = {
|
||||
getServiceByIntegrationKey: async () => [],
|
||||
getServiceByServiceId: async () => service,
|
||||
getServiceByEntity: async () => ({ service }),
|
||||
getOnCallByPolicyId: async () => [],
|
||||
getIncidentsByServiceId: async () => [],
|
||||
};
|
||||
@@ -129,9 +128,9 @@ describe('isPluginApplicableToEntity', () => {
|
||||
|
||||
describe('PageDutyCard', () => {
|
||||
it('Render pagerduty', async () => {
|
||||
mockPagerDutyApi.getServiceByIntegrationKey = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockImplementationOnce(async () => [service]);
|
||||
.mockImplementationOnce(async () => ({ service }));
|
||||
|
||||
const { getByText, queryByTestId } = render(
|
||||
wrapInTestApp(
|
||||
@@ -150,7 +149,7 @@ describe('PageDutyCard', () => {
|
||||
});
|
||||
|
||||
it('Handles custom error for missing token', async () => {
|
||||
mockPagerDutyApi.getServiceByIntegrationKey = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new UnauthorizedError());
|
||||
|
||||
@@ -168,7 +167,7 @@ describe('PageDutyCard', () => {
|
||||
});
|
||||
|
||||
it('Handles custom NotFoundError', async () => {
|
||||
mockPagerDutyApi.getServiceByIntegrationKey = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new NotFoundError());
|
||||
|
||||
@@ -186,7 +185,7 @@ describe('PageDutyCard', () => {
|
||||
});
|
||||
|
||||
it('handles general error', async () => {
|
||||
mockPagerDutyApi.getServiceByIntegrationKey = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new Error('An error occurred'));
|
||||
const { getByText, queryByTestId } = render(
|
||||
@@ -207,28 +206,10 @@ describe('PageDutyCard', () => {
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('handles empty response from getServiceByIntegrationKey', async () => {
|
||||
mockPagerDutyApi.getServiceByIntegrationKey = jest
|
||||
.fn()
|
||||
.mockImplementationOnce(async () => []);
|
||||
|
||||
const { getByText, queryByTestId } = render(
|
||||
wrapInTestApp(
|
||||
<ApiProvider apis={apis}>
|
||||
<EntityProvider entity={entity}>
|
||||
<PagerDutyCard />
|
||||
</EntityProvider>
|
||||
</ApiProvider>,
|
||||
),
|
||||
);
|
||||
await waitFor(() => !queryByTestId('progress'));
|
||||
expect(getByText('PagerDuty Service Not Found')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('opens the dialog when trigger button is clicked', async () => {
|
||||
mockPagerDutyApi.getServiceByIntegrationKey = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockImplementationOnce(async () => [service]);
|
||||
.mockImplementationOnce(async () => ({ service }));
|
||||
|
||||
const { getByText, queryByTestId, getByRole } = render(
|
||||
wrapInTestApp(
|
||||
@@ -251,9 +232,9 @@ describe('PageDutyCard', () => {
|
||||
|
||||
describe('when entity has the pagerduty.com/service-id annotation', () => {
|
||||
it('Renders PagerDuty service information', async () => {
|
||||
mockPagerDutyApi.getServiceByServiceId = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockImplementationOnce(async () => service);
|
||||
.mockImplementationOnce(async () => ({ service }));
|
||||
|
||||
const { getByText, queryByTestId } = render(
|
||||
wrapInTestApp(
|
||||
@@ -272,7 +253,7 @@ describe('PageDutyCard', () => {
|
||||
});
|
||||
|
||||
it('Handles custom error for missing token', async () => {
|
||||
mockPagerDutyApi.getServiceByServiceId = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new UnauthorizedError());
|
||||
|
||||
@@ -292,7 +273,7 @@ describe('PageDutyCard', () => {
|
||||
});
|
||||
|
||||
it('Handles custom NotFoundError', async () => {
|
||||
mockPagerDutyApi.getServiceByServiceId = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new NotFoundError());
|
||||
|
||||
@@ -310,7 +291,7 @@ describe('PageDutyCard', () => {
|
||||
});
|
||||
|
||||
it('handles general error', async () => {
|
||||
mockPagerDutyApi.getServiceByServiceId = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockRejectedValueOnce(new Error('An error occurred'));
|
||||
const { getByText, queryByTestId } = render(
|
||||
@@ -332,9 +313,9 @@ describe('PageDutyCard', () => {
|
||||
});
|
||||
|
||||
it('disables the Create Incident button', async () => {
|
||||
mockPagerDutyApi.getServiceByServiceId = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockImplementationOnce(async () => service);
|
||||
.mockImplementationOnce(async () => ({ service }));
|
||||
|
||||
const { queryByTestId, getByTitle } = render(
|
||||
wrapInTestApp(
|
||||
@@ -346,7 +327,6 @@ describe('PageDutyCard', () => {
|
||||
),
|
||||
);
|
||||
await waitFor(() => !queryByTestId('progress'));
|
||||
expect(queryByTestId('trigger-dialoger')).not.toBeInTheDocument();
|
||||
expect(
|
||||
getByTitle('Must provide an integration-key to create incidents')
|
||||
.className,
|
||||
@@ -356,22 +336,20 @@ describe('PageDutyCard', () => {
|
||||
|
||||
describe('when entity has all annotations', () => {
|
||||
it('queries by integration key', async () => {
|
||||
mockPagerDutyApi.getServiceByIntegrationKey = jest
|
||||
mockPagerDutyApi.getServiceByEntity = jest
|
||||
.fn()
|
||||
.mockImplementationOnce(async () => [service]);
|
||||
mockPagerDutyApi.getServiceByServiceId = jest.fn();
|
||||
.mockImplementationOnce(async () => ({ service }));
|
||||
|
||||
const { getByText, queryByTestId } = render(
|
||||
wrapInTestApp(
|
||||
<ApiProvider apis={apis}>
|
||||
<EntityProvider entity={entity}>
|
||||
<EntityProvider entity={entityWithAllAnnotations}>
|
||||
<PagerDutyCard />
|
||||
</EntityProvider>
|
||||
</ApiProvider>,
|
||||
),
|
||||
);
|
||||
await waitFor(() => !queryByTestId('progress'));
|
||||
expect(mockPagerDutyApi.getServiceByServiceId).not.toHaveBeenCalled();
|
||||
expect(getByText('Service Directory')).toBeInTheDocument();
|
||||
expect(getByText('Create Incident')).toBeInTheDocument();
|
||||
expect(getByText('Nice! No incidents found!')).toBeInTheDocument();
|
||||
|
||||
@@ -29,7 +29,6 @@ import { usePagerdutyEntity } from '../../hooks';
|
||||
import { PAGERDUTY_INTEGRATION_KEY, PAGERDUTY_SERVICE_ID } from '../constants';
|
||||
import { TriggerDialog } from '../TriggerDialog';
|
||||
import { ChangeEvents } from '../ChangeEvents';
|
||||
import { Service } from '../types';
|
||||
|
||||
import { useApi } from '@backstage/core-plugin-api';
|
||||
import { NotFoundError } from '@backstage/errors';
|
||||
@@ -53,7 +52,7 @@ export const isPluginApplicableToEntity = (entity: Entity) =>
|
||||
);
|
||||
|
||||
export const PagerDutyCard = () => {
|
||||
const { integrationKey, serviceId } = usePagerdutyEntity();
|
||||
const pagerDutyEntity = usePagerdutyEntity();
|
||||
const api = useApi(pagerDutyApiRef);
|
||||
const [refreshIncidents, setRefreshIncidents] = useState<boolean>(false);
|
||||
const [refreshChangeEvents, setRefreshChangeEvents] =
|
||||
@@ -77,17 +76,9 @@ export const PagerDutyCard = () => {
|
||||
loading,
|
||||
error,
|
||||
} = useAsync(async () => {
|
||||
let foundService: Service;
|
||||
|
||||
if (integrationKey) {
|
||||
const services = await api.getServiceByIntegrationKey(
|
||||
integrationKey as string,
|
||||
);
|
||||
foundService = services[0];
|
||||
if (!foundService) throw new NotFoundError();
|
||||
} else {
|
||||
foundService = await api.getServiceByServiceId(serviceId!);
|
||||
}
|
||||
const { service: foundService } = await api.getServiceByEntity(
|
||||
pagerDutyEntity,
|
||||
);
|
||||
|
||||
return {
|
||||
id: foundService.id,
|
||||
@@ -138,7 +129,7 @@ export const PagerDutyCard = () => {
|
||||
* There is no guarantee the current user entity has a valid email association, so instead just
|
||||
* only allow triggering incidents when an integration key is present.
|
||||
*/
|
||||
const createIncidentDisabled = !integrationKey;
|
||||
const createIncidentDisabled = !pagerDutyEntity.integrationKey;
|
||||
const triggerLink: IconLinkVerticalProps = {
|
||||
label: 'Create Incident',
|
||||
onClick: showDialog,
|
||||
|
||||
Reference in New Issue
Block a user