Skip to content

Commit

Permalink
catalog-backend: add gitlab to AnnotateScmSlug
Browse files Browse the repository at this point in the history
Signed-off-by: Manuel Stein <manuel.stein@nokia-bell-labs.com>
  • Loading branch information
manuelstein committed Mar 17, 2022
1 parent 8307218 commit ffec894
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-emus-stare.md
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend': patch
---

add gitlab to AnnotateScmSlugEntityProcessor
Expand Up @@ -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(
Expand Down
Expand Up @@ -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 {
Expand Down Expand Up @@ -53,24 +54,34 @@ 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(
{
metadata: {
annotations: pickBy(
{
[GITHUB_ACTIONS_ANNOTATION]: githubProjectSlug,
[annotation]: projectSlug,
},
identity,
),
Expand Down

0 comments on commit ffec894

Please sign in to comment.