Merge pull request #10269 from nokia/catalog-backend-scm-slug-add-gitlab

catalog-backend: add gitlab to AnnotateScmSlugEntityProcessor
This commit is contained in:
Fredrik Adelöw
2022-03-17 16:42:17 +01:00
committed by GitHub
3 changed files with 115 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---
add gitlab to AnnotateScmSlugEntityProcessor
@@ -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(
@@ -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,
),