Skip to content

Commit

Permalink
feat(processor): added indexing feature
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Jan 19, 2023
1 parent e3b314f commit 1374ea0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 21 deletions.
89 changes: 86 additions & 3 deletions packages/gitlab-backend/src/processor/processor.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,103 @@
import { GitlabFillerProcessor } from './processor';
import { ConfigReader } from '@backstage/config';
import { Entity } from '@backstage/catalog-model';

// To write tests
describe('Processor', () => {
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
host: 'my.custom-gitlab.com',
apiBaseUrl: 'https://my.custom-gitlab.com/api/v4',
},
{
host: 'my.second-custom-gitlab.com',
apiBaseUrl: 'https://my.second-custom-gitlab.com/api/v4',
},
],
},
});
const processor = new GitlabFillerProcessor(config);

it('added label', () => {
it('Processor has the correct name', () => {
const processor = new GitlabFillerProcessor(config);
expect(processor.getProcessorName()).toEqual('GitlabFillerProcessor');
});

it('Processor creates the right annotation', async () => {
const processor = new GitlabFillerProcessor(config);
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'backstage',
},
};
await processor.postProcessEntity(
entity,
{
type: 'url',
target: 'https://my.custom-gitlab.com/backstage/backstage/-/blob/next/catalog.yaml',
},
() => undefined
);

expect(
entity.metadata?.annotations?.['gitlab.com/project-slug']
).toEqual('0@backstage/backstage');
});

it('Processor creates the right old gitlab', async () => {
const processor = new GitlabFillerProcessor(config);
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'backstage',
},
};
await processor.postProcessEntity(
entity,
{
type: 'url',
target: 'https://my.custom-gitlab.com/backstage/backstage/blob/next/catalog.yaml',
},
() => undefined
);

expect(
entity.metadata?.annotations?.['gitlab.com/project-slug']
).toEqual('0@backstage/backstage');
});

it('The processor does not update annotation if the annotations exist', async () => {
const processor = new GitlabFillerProcessor(config);
const projectId = '3922';
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'backstage',
annotations: {
'gitlab.com/project-id': projectId,
},
},
};
await processor.postProcessEntity(
entity,
{
type: 'url',
target: 'https://my.custom-gitlab.com/backstage/backstage/blob/next/catalog.yaml',
},
() => undefined
);

expect(
entity.metadata?.annotations?.['gitlab.com/project-slug']
).toBeUndefined();

expect(entity.metadata?.annotations?.['gitlab.com/project-id']).toEqual(
projectId
);
});
});
22 changes: 18 additions & 4 deletions packages/gitlab-backend/src/processor/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,34 @@ export class GitlabFillerProcessor implements CatalogProcessor {
): Promise<Entity> {
if (
location.type === 'url' &&
entity.metadata.annotations &&
// To make annotation constants
!entity.metadata.annotations['gitlab.com/project-id'] &&
!entity.metadata.annotations['gitlab.com/project-slug'] &&
!entity.metadata.annotations?.['gitlab.com/project-id'] &&
!entity.metadata.annotations?.['gitlab.com/project-slug'] &&
this.allowedKinds.has(entity.kind.toLowerCase()) &&
this.isValidGitlabHost(location.target)
) {
if (!entity.metadata.annotations) entity.metadata.annotations = {};
entity.metadata.annotations['gitlab.com/project-slug'] =
getProjectPath(location.target);
this.generateAnnotation(location.target);
}

return entity;
}

private generateAnnotation(target: string): string {
const url = new URL(target);

// TODO: handle the possibility to have a baseUrl with a path
const index = this.gitLabIntegrationsConfig.findIndex(
({ baseUrl }) => baseUrl === url.origin
);

const projectPath = getProjectPath(target);
if (index < 0) return projectPath;

return `${index}@${projectPath}`;
}

private isValidGitlabHost(target: string) {
const url = new URL(target);

Expand Down
16 changes: 2 additions & 14 deletions packages/gitlab-backend/src/processor/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function buildProjectUrl(target: string): URL {
.split('/blob/')
.splice(0, 1)
.join('/')
.split('/-/')
.split('/-')
.splice(0, 1)
.join('/');

Expand All @@ -26,18 +26,6 @@ export function buildProjectUrl(target: string): URL {
}
}

export function isValidGitlabHost(
target: string,
hosts: GitLabIntegrationConfig[]
): boolean {
const url = new URL(target);

return hosts.some((config) => {
const baseUrl = new URL(config.baseUrl);
return url.origin === baseUrl.origin;
});
}

// from: https://gitlab.com/groupA/teams/teamA/subgroupA/repoA/-/blob/branch/filepath
// to: groupA/teams/teamA/subgroupA/repoA
/**
Expand All @@ -52,7 +40,7 @@ export function getProjectPath(target: string): string {
.split('/blob/')
.splice(0, 1)
.join('/')
.split('/-/')
.split('/-')
.splice(0, 1)
.join('/')
.slice(1);
Expand Down

0 comments on commit 1374ea0

Please sign in to comment.