Skip to content

Commit

Permalink
fix: handle error when target is not well formatted url
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniomuso committed Mar 6, 2023
1 parent a2cab01 commit ca49ad4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
30 changes: 30 additions & 0 deletions packages/gitlab-backend/src/processor/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,34 @@ describe('Processor', () => {
entity.metadata?.annotations?.[GITLAB_PROJECT_ID]
).toBeUndefined();
});

it('The processor should check if an entity is allowed before the target check', async () => {
const processor = new GitlabFillerProcessor(config);
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
name: 'backstage',
},
};
await expect(
processor.postProcessEntity(
entity,
{
type: 'url',
// Target is not a URL
target: 'my.custom-gitlab.com/backstage/backstage/blob/next/catalog.yaml',
},
() => undefined
)
).resolves.toEqual(entity);

expect(
entity.metadata?.annotations?.[GITLAB_PROJECT_SLUG]
).toBeUndefined();
expect(entity.metadata?.annotations?.[GITLAB_INSTANCE]).toBeUndefined();
expect(
entity.metadata?.annotations?.[GITLAB_PROJECT_ID]
).toBeUndefined();
});
});
10 changes: 7 additions & 3 deletions packages/gitlab-backend/src/processor/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class GitlabFillerProcessor implements CatalogProcessor {
_emit: CatalogProcessorEmit
): Promise<Entity> {
// Check if it is a GitLab Host
if (this.isValidLocation(location) && this.isAllowedEntity(entity)) {
if (this.isAllowedEntity(entity) && this.isValidLocation(location)) {
if (!entity.metadata.annotations) entity.metadata.annotations = {};
// Generate Project Slug if not specified
if (
Expand Down Expand Up @@ -82,8 +82,12 @@ export class GitlabFillerProcessor implements CatalogProcessor {
private isValidLocation({ target, type }: LocationSpec): boolean {
if (type !== 'url') return false;

const url = new URL(target);

let url: URL;
try {
url = new URL(target);
} catch (e) {
return false;
}
// Check if it is valid gitlab Host
return this.gitLabIntegrationsConfig.some((config) => {
const baseUrl = new URL(config.baseUrl);
Expand Down

0 comments on commit ca49ad4

Please sign in to comment.