Added support for annotations that use a subpath for the host

Signed-off-by: Andre Wanlin <awanlin@spotify.com>
This commit is contained in:
Andre Wanlin
2023-12-08 13:54:30 -06:00
parent 139b364198
commit 7c9af0bd28
3 changed files with 65 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-azure-devops': patch
---
Added support for annotations that use a subpath for the host
@@ -67,7 +67,7 @@ describe('getAnnotationValuesFromEntity', () => {
});
describe('with project-repo annotation missing project', () => {
it('should throw missing project error', () => {
it('should throw incorrect format error', () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
@@ -91,7 +91,7 @@ describe('getAnnotationValuesFromEntity', () => {
});
describe('with project-repo annotation missing repo', () => {
it('should throw missing repo error', () => {
it('should throw incorrect format error', () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
@@ -261,7 +261,7 @@ describe('getAnnotationValuesFromEntity', () => {
});
describe('with host-org annotation missing host', () => {
it('should throw missing project error', () => {
it('should throw incorrect format error', () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
@@ -285,7 +285,7 @@ describe('getAnnotationValuesFromEntity', () => {
});
describe('with host-org annotation missing org', () => {
it('should throw missing repo error', () => {
it('should throw incorrect format error', () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
@@ -307,4 +307,54 @@ describe('getAnnotationValuesFromEntity', () => {
);
});
});
describe('with tfs subpath for org', () => {
it('should return host and org', () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
namespace: 'default',
name: 'tfs-subpath',
annotations: {
'dev.azure.com/project-repo': 'projectName/repoName',
'dev.azure.com/host-org': 'company.com/tfs/organizationName',
},
},
};
const values = getAnnotationValuesFromEntity(entity);
expect(values).toEqual({
project: 'projectName',
repo: 'repoName',
definition: undefined,
host: 'company.com/tfs',
org: 'organizationName',
});
});
});
describe('with more then expected slashes', () => {
it('should throw incorrect format error', () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
namespace: 'default',
name: 'host-org',
annotations: {
'dev.azure.com/host-org': 'host/subpath/another-path/org/project',
},
},
};
const test = () => {
return getAnnotationValuesFromEntity(entity);
};
expect(test).toThrow(
'Invalid value for annotation "dev.azure.com/host-org"; expected format is: <host-name>/<organization-name>, found: "host/subpath/another-path/org/project"',
);
});
});
});
@@ -89,11 +89,15 @@ function getHostOrg(annotations?: Record<string, string>): {
return { host: undefined, org: undefined };
}
if (annotation.includes('/')) {
const [host, org] = annotation.split('/');
const segments = annotation.split('/');
if (segments.length === 2) {
const [host, org] = segments;
if (host && org) {
return { host, org };
}
} else if (segments.length === 3) {
const [host, subpath, org] = segments;
return { host: `${host}/${subpath}`, org };
}
throw new Error(