Merge pull request #21797 from awanlin/topic/handle-tfs-subpath-in-annotations

[Azure DevOps] Added support for annotations that use a subpath for the host
This commit is contained in:
Ben Lambert
2023-12-11 15:14:58 +01:00
committed by GitHub
3 changed files with 90 additions and 7 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-azure-devops': patch
---
Added support for annotations that use a subpath for the host. Also validated that the annotations have the correct number of slashes.
@@ -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,78 @@ 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('host-org 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"',
);
});
});
describe('project-repo with more then expected slashes', () => {
it('should throw incorrect format error', () => {
const entity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
metadata: {
namespace: 'default',
name: 'project-repo',
annotations: {
'dev.azure.com/project-repo': 'project/another/repo/final',
},
},
};
const test = () => {
return getAnnotationValuesFromEntity(entity);
};
expect(test).toThrow(
'Invalid value for annotation "dev.azure.com/project-repo"; expected format is: <project-name>/<repo-name>, found: "project/another/repo/final"',
);
});
});
});
@@ -68,7 +68,7 @@ function getProjectRepo(annotations?: Record<string, string>): {
return { project: undefined, repo: undefined };
}
if (annotation.includes('/')) {
if (annotation.split('/').length === 2) {
const [project, repo] = annotation.split('/');
if (project && repo) {
return { project, repo };
@@ -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(