add parseLocationReference/stringifyLocationReference

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2021-03-05 13:56:25 +01:00
parent 98387ac6b5
commit 0b42fff222
15 changed files with 259 additions and 93 deletions
+1 -1
View File
@@ -105,7 +105,7 @@ describe('parseReferenceAnnotation', () => {
'backstage.io/techdocs-ref',
mockEntityWithBadAnnotation,
);
}).toThrow(/Failure to parse/);
}).toThrow(/Unable to parse/);
});
});
+6 -18
View File
@@ -15,7 +15,7 @@
*/
import { Git, InputError, UrlReader } from '@backstage/backend-common';
import { Entity } from '@backstage/catalog-model';
import { Entity, parseLocationReference } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import fs from 'fs-extra';
import parseGitUrl from 'git-url-parse';
@@ -36,28 +36,15 @@ export const parseReferenceAnnotation = (
entity: Entity,
): ParsedLocationAnnotation => {
const annotation = entity.metadata.annotations?.[annotationName];
if (!annotation) {
throw new InputError(
`No location annotation provided in entity: ${entity.metadata.name}`,
);
}
// split on the first colon for the protocol and the rest after the first split
// is the location.
const [type, target] = annotation.split(/:(.+)/) as [
RemoteProtocol?,
string?,
];
if (!type || !target) {
throw new InputError(
`Failure to parse either protocol or location for entity: ${entity.metadata.name}`,
);
}
const { type, target } = parseLocationReference(annotation);
return {
type,
type: type as RemoteProtocol,
target,
};
};
@@ -77,8 +64,9 @@ export const getLocationForEntity = (
case 'url':
return { type, target };
case 'dir':
if (path.isAbsolute(target)) return { type, target };
if (path.isAbsolute(target)) {
return { type, target };
}
return parseReferenceAnnotation(
'backstage.io/managed-by-location',
entity,