From be129f8f3cd15b4e10950450bb8ca3ec8a74daf2 Mon Sep 17 00:00:00 2001 From: Matteo Silvestri Date: Mon, 20 Feb 2023 16:24:51 +0100 Subject: [PATCH] filter gitlab groups by prefix Signed-off-by: Matteo Silvestri --- .changeset/wild-bulldogs-suffer.md | 5 ++++ .../GitlabOrgDiscoveryEntityProvider.ts | 28 ++++++++++++++----- 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 .changeset/wild-bulldogs-suffer.md diff --git a/.changeset/wild-bulldogs-suffer.md b/.changeset/wild-bulldogs-suffer.md new file mode 100644 index 0000000000..02f50927e9 --- /dev/null +++ b/.changeset/wild-bulldogs-suffer.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-gitlab': patch +--- + +filter gitlab groups by prefix diff --git a/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts b/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts index f90a61f9e9..c507b19549 100644 --- a/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts +++ b/plugins/catalog-backend-module-gitlab/src/providers/GitlabOrgDiscoveryEntityProvider.ts @@ -199,6 +199,10 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { continue; } + if (!group.full_path.startsWith(this.config.group)) { + continue; + } + groupRes.scanned++; groupRes.matches.push(group); @@ -253,7 +257,11 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { type: 'full', entities: [...userEntities, ...groupEntities].map(entity => ({ locationKey: this.getProviderName(), - entity: this.withLocations(this.integration.config.host, entity), + entity: this.withLocations( + this.integration.config.host, + this.integration.config.baseUrl, + entity, + ), })), }); } @@ -273,7 +281,9 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { const entity = this.createGroupEntity(group, host); if (group.parent_id && idMapped.hasOwnProperty(group.parent_id)) { - entity.spec.parent = idMapped[group.parent_id].full_path; + entity.spec.parent = this.groupName( + idMapped[group.parent_id].full_path, + ); } entities.push(entity); @@ -282,11 +292,11 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { return entities; } - private withLocations(host: string, entity: Entity): Entity { + private withLocations(host: string, baseUrl: string, entity: Entity): Entity { const location = entity.kind === 'Group' - ? `url:${host}/teams/${entity.metadata.name}` - : `url:${host}/${entity.metadata.name}`; + ? `url:${baseUrl}/${entity.metadata.annotations[`${host}/team-path`]}` + : `url:${baseUrl}/${entity.metadata.name}`; return merge( { metadata: { @@ -338,13 +348,17 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { if (!entity.spec.memberOf) { entity.spec.memberOf = []; } - entity.spec.memberOf.push(group.full_path.replace('/', '-')); + entity.spec.memberOf.push(this.groupName(group.full_path)); } } return entity; } + private groupName(full_path: string): string { + return full_path.replace(`${this.config.group}/`, '').replaceAll('/', '-'); + } + private createGroupEntity(group: GitLabGroup, host: string): GroupEntity { const annotations: { [annotationName: string]: string } = {}; @@ -354,7 +368,7 @@ export class GitlabOrgDiscoveryEntityProvider implements EntityProvider { apiVersion: 'backstage.io/v1alpha1', kind: 'Group', metadata: { - name: group.full_path.replace('/', '-'), + name: this.groupName(group.full_path), annotations: annotations, }, spec: {