diff --git a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts index 5584567188..0d7a11f48b 100644 --- a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts +++ b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.test.ts @@ -31,13 +31,14 @@ describe('getAnnotationValuesFromEntity', () => { }, }, }; - const { project, repo, definition, host, org } = - getAnnotationValuesFromEntity(entity); - expect(project).toEqual('projectName'); - expect(repo).toEqual('repoName'); - expect(definition).toEqual(undefined); - expect(host).toEqual(undefined); - expect(org).toEqual(undefined); + const values = getAnnotationValuesFromEntity(entity); + expect(values).toEqual({ + project: 'projectName', + repo: 'repoName', + definition: undefined, + host: undefined, + org: undefined, + }); }); }); @@ -127,13 +128,14 @@ describe('getAnnotationValuesFromEntity', () => { }, }, }; - const { project, repo, definition, host, org } = - getAnnotationValuesFromEntity(entity); - expect(project).toEqual('projectName'); - expect(repo).toEqual(undefined); - expect(definition).toEqual('buildDefinitionName'); - expect(host).toEqual(undefined); - expect(org).toEqual(undefined); + const values = getAnnotationValuesFromEntity(entity); + expect(values).toEqual({ + project: 'projectName', + repo: undefined, + definition: 'buildDefinitionName', + host: undefined, + org: undefined, + }); }); }); @@ -197,13 +199,14 @@ describe('getAnnotationValuesFromEntity', () => { }, }, }; - const { project, repo, definition, host, org } = - getAnnotationValuesFromEntity(entity); - expect(project).toEqual('projectName'); - expect(repo).toEqual('repoName'); - expect(definition).toEqual(undefined); - expect(host).toEqual('hostName'); - expect(org).toEqual('organizationName'); + const values = getAnnotationValuesFromEntity(entity); + expect(values).toEqual({ + project: 'projectName', + repo: 'repoName', + definition: undefined, + host: 'hostName', + org: 'organizationName', + }); }); }); @@ -222,13 +225,14 @@ describe('getAnnotationValuesFromEntity', () => { }, }, }; - const { project, repo, definition, host, org } = - getAnnotationValuesFromEntity(entity); - expect(project).toEqual('projectName'); - expect(repo).toEqual(undefined); - expect(definition).toEqual('buildDefinitionName'); - expect(host).toEqual('hostName'); - expect(org).toEqual('organizationName'); + const values = getAnnotationValuesFromEntity(entity); + expect(values).toEqual({ + project: 'projectName', + repo: undefined, + definition: 'buildDefinitionName', + host: 'hostName', + org: 'organizationName', + }); }); }); @@ -256,7 +260,7 @@ describe('getAnnotationValuesFromEntity', () => { }); }); - describe('with host-rg annotation missing host', () => { + describe('with host-org annotation missing host', () => { it('should throw missing project error', () => { const entity: Entity = { apiVersion: 'backstage.io/v1alpha1', diff --git a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts index 8ce096d3c5..7099352524 100644 --- a/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationValuesFromEntity.ts @@ -30,16 +30,16 @@ export function getAnnotationValuesFromEntity(entity: Entity): { host?: string; org?: string; } { - const hostOrgAnnotation = - entity.metadata.annotations?.[AZURE_DEVOPS_HOST_ORG_ANNOTATION]; - const { host, org } = getHostOrg(hostOrgAnnotation); + const { host, org } = getHostOrg(entity.metadata.annotations); - const projectRepoAnnotation = - entity.metadata.annotations?.[AZURE_DEVOPS_REPO_ANNOTATION]; - if (projectRepoAnnotation) { - const { project, repo } = getProjectRepo(projectRepoAnnotation); - const definition = undefined; - return { project, repo, definition, host, org }; + const projectRepoValues = getProjectRepo(entity.metadata.annotations); + if (projectRepoValues.project && projectRepoValues.repo) { + return { + project: projectRepoValues.project, + repo: projectRepoValues.repo, + host, + org, + }; } const project = @@ -57,15 +57,18 @@ export function getAnnotationValuesFromEntity(entity: Entity): { `Value for annotation ${AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION} was not found`, ); } - - const repo = undefined; - return { project, repo, definition, host, org }; + return { project, definition, host, org }; } -function getProjectRepo(annotation: string): { - project: string; - repo: string; +function getProjectRepo(annotations?: Record): { + project?: string; + repo?: string; } { + const annotation = annotations?.[AZURE_DEVOPS_REPO_ANNOTATION]; + if (!annotation) { + return { project: undefined, repo: undefined }; + } + if (!annotation.includes('/')) { throw new Error( `Value for annotation ${AZURE_DEVOPS_REPO_ANNOTATION} was not in the correct format: /`, @@ -89,10 +92,11 @@ function getProjectRepo(annotation: string): { return { project, repo }; } -function getHostOrg(annotation?: string): { +function getHostOrg(annotations?: Record): { host?: string; org?: string; } { + const annotation = annotations?.[AZURE_DEVOPS_HOST_ORG_ANNOTATION]; if (!annotation) { return { host: undefined, org: undefined }; }