Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: service dependency sync #125

Merged
merged 5 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backstage.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "1.27.7"
"version": "1.29.1"
}
9 changes: 9 additions & 0 deletions dev/mockPagerDutyApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ import { Entity } from '@backstage/catalog-model';
import { v4 as uuidv4 } from 'uuid';

export const mockPagerDutyApi: PagerDutyApi = {
async getSetting(id: string) {
return {
id: id,
value: 'backstage',
};
},
async storeSettings(settings) {
return new Response(JSON.stringify(settings));
},
async getEntityMappings() {
return {
mappings: [
Expand Down
24 changes: 13 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"types": "dist/index.d.ts"
},
"backstage": {
"role": "frontend-plugin"
"role": "frontend-plugin",
"pluginId": "pagerduty",
"pluginPackages": "pagerduty"
},
"homepage": "https://github.com/pagerduty/backstage-plugin",
"repository": {
Expand All @@ -34,12 +36,12 @@
},
"dependencies": {
"@backstage/catalog-model": "^1.5.0",
"@backstage/core-components": "^0.14.7",
"@backstage/core-plugin-api": "^1.9.2",
"@backstage/core-components": "^0.14.9",
"@backstage/core-plugin-api": "^1.9.3",
"@backstage/errors": "^1.2.4",
"@backstage/plugin-catalog-react": "^1.12.0",
"@backstage/plugin-home-react": "^0.1.13",
"@backstage/theme": "^0.5.5",
"@backstage/plugin-catalog-react": "^1.12.2",
"@backstage/plugin-home-react": "^0.1.15",
"@backstage/theme": "^0.5.6",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@material-ui/core": "^4.12.2",
Expand All @@ -48,7 +50,7 @@
"@mui/icons-material": "^5.15.19",
"@mui/material": "^5.15.19",
"@mui/x-date-pickers": "^7.6.1",
"@pagerduty/backstage-plugin-common": "0.2.0",
"@pagerduty/backstage-plugin-common": "0.2.1",
"@tanstack/react-query": "^5.40.1",
"classnames": "^2.2.6",
"luxon": "^3.4.1",
Expand All @@ -63,10 +65,10 @@
"react-router-dom": "^6.3.0"
},
"devDependencies": {
"@backstage/cli": "^0.26.6",
"@backstage/core-app-api": "^1.12.5",
"@backstage/dev-utils": "^1.0.32",
"@backstage/test-utils": "^1.5.5",
"@backstage/cli": "^0.26.11",
"@backstage/core-app-api": "^1.14.0",
"@backstage/dev-utils": "^1.0.35",
"@backstage/test-utils": "^1.5.8",
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@testing-library/dom": "^8.0.0",
Expand Down
80 changes: 79 additions & 1 deletion src/api/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import { MockFetchApi } from '@backstage/test-utils';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { PagerDutyClient, UnauthorizedError } from './client';
import { PagerDutyService } from '@pagerduty/backstage-plugin-common';
import { PagerDutyService, PagerDutySetting } from '@pagerduty/backstage-plugin-common';
import { NotFoundError } from '@backstage/errors';
import { Entity } from '@backstage/catalog-model';

Expand Down Expand Up @@ -365,3 +365,81 @@ describe('PagerDutyClient', () => {
});
});
});

describe('getSetting', () => {
const settingId = 'settingId';
const setting: PagerDutySetting = {
id: settingId,
value: 'disabled',
};

beforeEach(() => {
mockFetch.mockResolvedValueOnce({
status: 200,
ok: true,
json: () => Promise.resolve(setting),
});
});

it('should fetch the setting by ID', async () => {
const result = await client.getSetting(settingId);

expect(result).toEqual(setting);
expect(mockFetch).toHaveBeenCalledWith(
'http://localhost:7007/pagerduty/settings/settingId',
requestHeaders,
);
});

describe('storeSettings', () => {
const settings: PagerDutySetting[] = [
{
id: 'setting1',
value: 'disabled',
},
{
id: 'setting2',
value: 'backstage',
},
];

beforeEach(() => {
mockFetch.mockReset();
});

it('should send a POST request to the correct URL with the settings', async () => {
const response = new Response(null, { status: 200 });
mockFetch.mockResolvedValueOnce(response);

const expectedUrl = 'http://localhost:7007/pagerduty/settings';
const expectedOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
Accept: 'application/json, text/plain, */*',
},
body: JSON.stringify(settings),
};

await expect(client.storeSettings(settings)).resolves.toEqual(response);
expect(mockFetch).toHaveBeenCalledWith(expectedUrl, expectedOptions);
});

it('should throw an error if the request fails', async () => {
const errorResponse = {
status: 500,
ok: false,
json: () =>
Promise.resolve({
errors: ['Internal server error'],
}),
};
mockFetch.mockResolvedValueOnce(errorResponse);

await expect(client.storeSettings(settings)).rejects.toThrow(
'Request failed with 500, Internal server error',
);
});
});

});
30 changes: 29 additions & 1 deletion src/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import { PagerDutyChangeEventsResponse,
PagerDutyIncidentsResponse,
PagerDutyServiceStandardsResponse,
PagerDutyServiceMetricsResponse,
PagerDutyEntityMappingsResponse
PagerDutyEntityMappingsResponse,
PagerDutySetting
} from '@pagerduty/backstage-plugin-common';
import { createApiRef, ConfigApi } from '@backstage/core-plugin-api';
import { NotFoundError } from '@backstage/errors';
Expand Down Expand Up @@ -106,6 +107,33 @@ export class PagerDutyClient implements PagerDutyApi {
return response;
}

async getSetting(id: string): Promise<PagerDutySetting> {
const url = `${await this.config.discoveryApi.getBaseUrl(
'pagerduty',
)}/settings/${id}`;

return await this.findByUrl<PagerDutySetting>(url);
}

async storeSettings(settings: PagerDutySetting[]): Promise<Response> {
const body = JSON.stringify(settings);

const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
Accept: 'application/json, text/plain, */*',
},
body,
};

const url = `${await this.config.discoveryApi.getBaseUrl(
'pagerduty',
)}/settings`;

return this.request(url, options);
}

async getEntityMappings(): Promise<PagerDutyEntityMappingsResponse> {
const url = `${await this.config.discoveryApi.getBaseUrl(
'pagerduty',
Expand Down
13 changes: 12 additions & 1 deletion src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import { PagerDutyChangeEventsResponse,
PagerDutyServiceMetricsResponse,
PagerDutyServiceStandards,
PagerDutyServiceMetrics,
PagerDutyEntityMappingsResponse
PagerDutyEntityMappingsResponse,
PagerDutySetting
} from '@pagerduty/backstage-plugin-common';
import { DiscoveryApi, FetchApi } from '@backstage/core-plugin-api';
import { Entity } from '@backstage/catalog-model';
Expand Down Expand Up @@ -52,6 +53,16 @@ export type PagerDutyCardServiceResponse = {

/** @public */
export interface PagerDutyApi {
/**
* Fetches PagerDuty setting from store.
*
*/
getSetting(id: string): Promise<PagerDutySetting>;
/**
* Stores PagerDuty setting in the database.
*
*/
storeSettings(settings: PagerDutySetting[]): Promise<Response>;
/**
* Fetches all entity mappings.
*
Expand Down
4 changes: 2 additions & 2 deletions src/components/PagerDutyPage/MappingTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ export const MappingTable = ({
mutationFn: async (mapping: PagerDutyEntityMapping) => {
return await pagerDutyApi.storeServiceMapping(
mapping.serviceId,
mapping.integrationKey || "",
mapping.integrationKey ?? "",
mapping.entityRef,
mapping.account || ""
mapping.account ?? ""
);
},
});
Expand Down
Loading
Loading