From 7c9af0bd28a261722500a503a1344e234ac519e4 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Fri, 8 Dec 2023 13:54:30 -0600 Subject: [PATCH 1/3] Added support for annotations that use a subpath for the host Signed-off-by: Andre Wanlin --- .changeset/odd-bats-kick.md | 5 ++ .../getAnnotationValuesFromEntity.test.ts | 58 +++++++++++++++++-- .../utils/getAnnotationValuesFromEntity.ts | 8 ++- 3 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 .changeset/odd-bats-kick.md diff --git a/.changeset/odd-bats-kick.md b/.changeset/odd-bats-kick.md new file mode 100644 index 0000000000..2ac568ae36 --- /dev/null +++ b/.changeset/odd-bats-kick.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-azure-devops': patch +--- + +Added support for annotations that use a subpath for the host diff --git a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts index 67e46cee7b..1f7ce53bde 100644 --- a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts +++ b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts @@ -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: /, found: "host/subpath/another-path/org/project"', + ); + }); + }); }); diff --git a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts index dec140706c..bf0b038c95 100644 --- a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts @@ -89,11 +89,15 @@ function getHostOrg(annotations?: Record): { 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( From 200c36ecf8ee861469a63282f8ad3514a90557a2 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Fri, 8 Dec 2023 14:03:26 -0600 Subject: [PATCH 2/3] Test for slashes in project repo as well Signed-off-by: Andre Wanlin --- .../getAnnotationValuesFromEntity.test.ts | 26 ++++++++++++++++++- .../utils/getAnnotationValuesFromEntity.ts | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts index 1f7ce53bde..c13dc526d8 100644 --- a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts +++ b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts @@ -334,7 +334,7 @@ describe('getAnnotationValuesFromEntity', () => { }); }); - describe('with more then expected slashes', () => { + describe('host-org with more then expected slashes', () => { it('should throw incorrect format error', () => { const entity: Entity = { apiVersion: 'backstage.io/v1alpha1', @@ -357,4 +357,28 @@ describe('getAnnotationValuesFromEntity', () => { ); }); }); + + 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: /, found: "project/another/repo/final"', + ); + }); + }); }); diff --git a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts index bf0b038c95..83091fb155 100644 --- a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts @@ -68,7 +68,7 @@ function getProjectRepo(annotations?: Record): { return { project: undefined, repo: undefined }; } - if (annotation.includes('/')) { + if (annotation.split('/').length === 2) { const [project, repo] = annotation.split('/'); if (project && repo) { return { project, repo }; From 2216e831b4300cf5f5ae74ca05f39010e111e527 Mon Sep 17 00:00:00 2001 From: Andre Wanlin Date: Fri, 8 Dec 2023 14:06:06 -0600 Subject: [PATCH 3/3] Updated changeset Signed-off-by: Andre Wanlin --- .changeset/odd-bats-kick.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/odd-bats-kick.md b/.changeset/odd-bats-kick.md index 2ac568ae36..f43f0ebe7d 100644 --- a/.changeset/odd-bats-kick.md +++ b/.changeset/odd-bats-kick.md @@ -2,4 +2,4 @@ '@backstage/plugin-azure-devops': patch --- -Added support for annotations that use a subpath for the host +Added support for annotations that use a subpath for the host. Also validated that the annotations have the correct number of slashes.