From ffec894ed0bedc8cfa27843bb4bc22bc8cdb0b56 Mon Sep 17 00:00:00 2001 From: Manuel Stein Date: Thu, 17 Mar 2022 14:54:59 +0200 Subject: [PATCH] catalog-backend: add gitlab to AnnotateScmSlug Signed-off-by: Manuel Stein --- .changeset/tidy-emus-stare.md | 5 + .../AnnotateScmSlugEntityProcessor.test.ts | 93 ++++++++++++++++++- .../core/AnnotateScmSlugEntityProcessor.ts | 25 +++-- 3 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 .changeset/tidy-emus-stare.md diff --git a/.changeset/tidy-emus-stare.md b/.changeset/tidy-emus-stare.md new file mode 100644 index 0000000000000..93f3dccde772a --- /dev/null +++ b/.changeset/tidy-emus-stare.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +add gitlab to AnnotateScmSlugEntityProcessor diff --git a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts index 1e553a44f8b61..7fd6514bb87cc 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts @@ -94,7 +94,98 @@ describe('AnnotateScmSlugEntityProcessor', () => { const location: LocationSpec = { type: 'url', target: - 'https://gitlab.com/backstage/backstage/-/blob/master/catalog-info.yaml', + 'https://not-in-mock-config.example.com/backstage/backstage/-/blob/master/catalog-info.yaml', + }; + + const processor = AnnotateScmSlugEntityProcessor.fromConfig( + new ConfigReader({}), + ); + + expect(await processor.preProcessEntity(entity, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }); + }); + }); + describe('gitlab', () => { + it('adds annotation', async () => { + const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }; + const location: LocationSpec = { + type: 'url', + target: + 'https://gitlab.com/group/subgroup/project/-/blob/master/catalog-info.yaml', + }; + + const processor = AnnotateScmSlugEntityProcessor.fromConfig( + new ConfigReader({}), + ); + + expect(await processor.preProcessEntity(entity, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'gitlab.com/project-slug': 'group/subgroup/project', + }, + }, + }); + }); + + it('does not override existing annotation', async () => { + const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'gitlab.com/project-slug': 'backstage/community', + }, + }, + }; + const location: LocationSpec = { + type: 'url', + target: + 'https://gitlab.com/group/subgroup/project/-/blob/master/catalog-info.yaml', + }; + + const processor = AnnotateScmSlugEntityProcessor.fromConfig( + new ConfigReader({}), + ); + + expect(await processor.preProcessEntity(entity, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'gitlab.com/project-slug': 'backstage/community', + }, + }, + }); + }); + + it('should not add annotation for other providers', async () => { + const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }; + const location: LocationSpec = { + type: 'url', + target: + 'https://not-in-mock-config.example.com/group/subgroup/project/-/blob/master/catalog-info.yaml', }; const processor = AnnotateScmSlugEntityProcessor.fromConfig( diff --git a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts index e44db4d44d346..c4f721889647b 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts @@ -24,6 +24,7 @@ import { identity, merge, pickBy } from 'lodash'; import { CatalogProcessor, LocationSpec } from '../../api'; const GITHUB_ACTIONS_ANNOTATION = 'github.com/project-slug'; +const GITLAB_ACTIONS_ANNOTATION = 'gitlab.com/project-slug'; /** @public */ export class AnnotateScmSlugEntityProcessor implements CatalogProcessor { @@ -53,16 +54,26 @@ export class AnnotateScmSlugEntityProcessor implements CatalogProcessor { location.target, ); - if (!scmIntegration || scmIntegration.type !== 'github') { + if (!scmIntegration) { return entity; } - const gitUrl = parseGitUrl(location.target); - let githubProjectSlug = - entity.metadata.annotations?.[GITHUB_ACTIONS_ANNOTATION]; + let annotation; + switch (scmIntegration.type) { + case 'github': + annotation = GITHUB_ACTIONS_ANNOTATION; + break; + case 'gitlab': + annotation = GITLAB_ACTIONS_ANNOTATION; + break; + default: + return entity; + } - if (!githubProjectSlug) { - githubProjectSlug = `${gitUrl.owner}/${gitUrl.name}`; + let projectSlug = entity.metadata.annotations?.[annotation]; + if (!projectSlug) { + const gitUrl = parseGitUrl(location.target); + projectSlug = `${gitUrl.owner}/${gitUrl.name}`; } return merge( @@ -70,7 +81,7 @@ export class AnnotateScmSlugEntityProcessor implements CatalogProcessor { metadata: { annotations: pickBy( { - [GITHUB_ACTIONS_ANNOTATION]: githubProjectSlug, + [annotation]: projectSlug, }, identity, ),