From 9790c02d16e986fc70f0d66daf20420a7b4f1691 Mon Sep 17 00:00:00 2001 From: Rogerio Angeliski Date: Wed, 16 Oct 2024 11:48:56 -0300 Subject: [PATCH] fix(catalog-backend-module-github): update parent to not send a object with empty value fix #26109 Signed-off-by: Rogerio Angeliski --- .changeset/rotten-mangos-hug.md | 5 ++ .../providers/GithubOrgEntityProvider.test.ts | 89 +++++++++++++++++++ .../src/providers/GithubOrgEntityProvider.ts | 4 +- 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 .changeset/rotten-mangos-hug.md diff --git a/.changeset/rotten-mangos-hug.md b/.changeset/rotten-mangos-hug.md new file mode 100644 index 0000000000..013a815653 --- /dev/null +++ b/.changeset/rotten-mangos-hug.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-github': patch +--- + +Fix bug when receive a `team.creted` github event without parent diff --git a/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts b/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts index 82c93b0d57..52dc2b6d14 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.test.ts @@ -530,6 +530,95 @@ describe('GithubOrgEntityProvider', () => { }); }); + it('should apply delta added on receive a created team without parent', async () => { + const entityProviderConnection: EntityProviderConnection = { + applyMutation: jest.fn(), + refresh: jest.fn(), + }; + + const logger = mockServices.logger.mock(); + const events = DefaultEventsService.create({ logger }); + const gitHubConfig: GithubIntegrationConfig = { + host: 'github.com', + }; + + const mockGetCredentials = jest.fn().mockReturnValue({ + headers: { token: 'blah' }, + type: 'app', + }); + + const githubCredentialsProvider: GithubCredentialsProvider = { + getCredentials: mockGetCredentials, + }; + + const entityProvider = new GithubOrgEntityProvider({ + events, + id: 'my-id', + githubCredentialsProvider, + orgUrl: 'https://github.com/backstage', + gitHubConfig, + logger, + }); + + entityProvider.connect(entityProviderConnection); + + const expectedEntity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Group', + metadata: { + name: 'new-team', + description: 'description from the new team', + annotations: { + 'backstage.io/edit-url': + 'https://github.com/orgs/test-org/teams/new-team/edit', + 'backstage.io/managed-by-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + 'backstage.io/managed-by-origin-location': + 'url:https://github.com/orgs/test-org/teams/new-team', + 'github.com/team-slug': 'test-org/new-team', + }, + }, + spec: { + type: 'team', + children: [], + members: [], + profile: { + displayName: 'New Team', + }, + }, + }; + + const event: EventParams = { + topic: 'github.team', + eventPayload: { + action: 'created', + team: { + name: 'New Team', + slug: 'new-team', + description: 'description from the new team', + html_url: 'https://github.com/orgs/test-org/teams/new-team', + }, + organization: { + login: 'test-org', + }, + }, + }; + + await events.publish(event); + + expect(entityProviderConnection.applyMutation).toHaveBeenCalledTimes(1); + expect(entityProviderConnection.applyMutation).toHaveBeenCalledWith({ + type: 'delta', + added: [ + { + locationKey: 'github-org-provider:my-id', + entity: expectedEntity, + }, + ], + removed: [], + }); + }); + it('should apply delta removed on receive a deleted team', async () => { const entityProviderConnection: EntityProviderConnection = { applyMutation: jest.fn(), diff --git a/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.ts b/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.ts index f7183b4df9..6e83b8d65f 100644 --- a/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.ts +++ b/plugins/catalog-backend-module-github/src/providers/GithubOrgEntityProvider.ts @@ -511,7 +511,9 @@ export class GithubOrgEntityProvider implements EntityProvider { editTeamUrl: `${url}/edit`, combinedSlug: `${org}/${slug}`, description: description || undefined, - parentTeam: { slug: event.team?.parent?.slug || '' } as GithubTeam, + parentTeam: event.team?.parent?.slug + ? ({ slug: event.team.parent.slug } as GithubTeam) + : undefined, // entity will be removed members: [], },