diff --git a/.changeset/old-months-attack.md b/.changeset/old-months-attack.md index c97d7d8f46..8f0810cbc3 100644 --- a/.changeset/old-months-attack.md +++ b/.changeset/old-months-attack.md @@ -2,4 +2,4 @@ '@backstage/plugin-azure-devops': minor --- -Updated to support cases where only Azure Pipelines are being used by adding an annotation that get builds by definition name +Updated to support cases where only Azure Pipelines to see Builds. You can use this new feature by adding the `dev.azure.com/project` and `dev.azure.com/build-definition` annotations to your `catalog-info.yaml` files. The Azure DevOps plugin [README has more detailed instructions](https://github.com/backstage/backstage/tree/master/plugins/azure-devops#setup). diff --git a/plugins/azure-devops/README.md b/plugins/azure-devops/README.md index 1737b72af7..e4b81fe97c 100644 --- a/plugins/azure-devops/README.md +++ b/plugins/azure-devops/README.md @@ -51,13 +51,14 @@ spec: #### Azure Pipelines Only -If you are only using Azure Pipelines along with a different SCM tool then you can use the following annotation to see Builds: +If you are only using Azure Pipelines along with a different SCM tool then you can use the following two annotations to see Builds: ```yaml -dev.azure.com/project-definition: / +dev.azure.com/project: +dev.azure.com/build-definition: ``` -In this case `` will be the name of your Team Project and `` will be the name of the Build Definition you would like to see Builds for. If the Build Definition name has spaces in it make sure to put quotes around it +In this case `` will be the name of your Team Project and `` will be the name of the Build Definition you would like to see Builds for. If the Build Definition name has spaces in it make sure to put quotes around it ### Azure Pipelines Component @@ -93,7 +94,7 @@ To get the Azure Pipelines component working you'll need to do the following two ``` - 2. If you are using the `dev.azure.com/project-definition` annotation then you'll want to do this: + 2. If you are using the ``dev.azure.com/project` and `dev.azure.com/build-definition` annotations then you'll want to do this: ```tsx // In packages/app/src/components/catalog/EntityPage.tsx diff --git a/plugins/azure-devops/src/api/AzureDevOpsApi.ts b/plugins/azure-devops/src/api/AzureDevOpsApi.ts index b727ce7dc9..09b48356c8 100644 --- a/plugins/azure-devops/src/api/AzureDevOpsApi.ts +++ b/plugins/azure-devops/src/api/AzureDevOpsApi.ts @@ -51,7 +51,7 @@ export interface AzureDevOpsApi { getAllTeams(): Promise; getUserTeamIds(userId: string): Promise; - + getBuildRuns( projectName: string, repoName?: string, diff --git a/plugins/azure-devops/src/api/AzureDevOpsClient.ts b/plugins/azure-devops/src/api/AzureDevOpsClient.ts index 688e5aa426..96c10a7b2e 100644 --- a/plugins/azure-devops/src/api/AzureDevOpsClient.ts +++ b/plugins/azure-devops/src/api/AzureDevOpsClient.ts @@ -93,7 +93,7 @@ export class AzureDevOpsClient implements AzureDevOpsApi { public getUserTeamIds(userId: string): Promise { return this.get(`users/${userId}/team-ids`); } - + public async getBuildRuns( projectName: string, repoName?: string, diff --git a/plugins/azure-devops/src/constants.ts b/plugins/azure-devops/src/constants.ts index 8a920b59e2..95e5be4930 100644 --- a/plugins/azure-devops/src/constants.ts +++ b/plugins/azure-devops/src/constants.ts @@ -14,7 +14,8 @@ * limitations under the License. */ +export const AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION = + 'dev.azure.com/build-definition'; +export const AZURE_DEVOPS_PROJECT_ANNOTATION = 'dev.azure.com/project'; export const AZURE_DEVOPS_REPO_ANNOTATION = 'dev.azure.com/project-repo'; -export const AZURE_DEVOPS_DEFINITION_ANNOTATION = - 'dev.azure.com/project-definition'; export const AZURE_DEVOPS_DEFAULT_TOP: number = 10; diff --git a/plugins/azure-devops/src/plugin.ts b/plugins/azure-devops/src/plugin.ts index d22dff2863..c2ecb7457a 100644 --- a/plugins/azure-devops/src/plugin.ts +++ b/plugins/azure-devops/src/plugin.ts @@ -15,7 +15,8 @@ */ import { - AZURE_DEVOPS_DEFINITION_ANNOTATION, + AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION, + AZURE_DEVOPS_PROJECT_ANNOTATION, AZURE_DEVOPS_REPO_ANNOTATION, } from './constants'; import { @@ -40,7 +41,10 @@ export const isAzureDevOpsAvailable = (entity: Entity) => export const isAzurePipelinesAvailable = (entity: Entity) => Boolean(entity.metadata.annotations?.[AZURE_DEVOPS_REPO_ANNOTATION]) || - Boolean(entity.metadata.annotations?.[AZURE_DEVOPS_DEFINITION_ANNOTATION]); + (Boolean(entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]) && + Boolean( + entity.metadata.annotations?.[AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION], + )); export const azureDevOpsPlugin = createPlugin({ id: 'azureDevOps', diff --git a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts index 5509a72fef..41cc1e6628 100644 --- a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts @@ -15,7 +15,8 @@ */ import { - AZURE_DEVOPS_DEFINITION_ANNOTATION, + AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION, + AZURE_DEVOPS_PROJECT_ANNOTATION, AZURE_DEVOPS_REPO_ANNOTATION, } from '../constants'; @@ -26,37 +27,30 @@ export function getAnnotationFromEntity(entity: Entity): { repo?: string; definition?: string; } { - let annotation = - entity.metadata.annotations?.[AZURE_DEVOPS_DEFINITION_ANNOTATION]; - if (annotation) { - const { project, definition } = getProjectDefinition(annotation); - const repo = undefined; - return { project, repo, definition }; - } - - annotation = entity.metadata.annotations?.[AZURE_DEVOPS_REPO_ANNOTATION]; + const annotation = + entity.metadata.annotations?.[AZURE_DEVOPS_REPO_ANNOTATION]; if (annotation) { const { project, repo } = getProjectRepo(annotation); const definition = undefined; return { project, repo, definition }; } - throw new Error('No supported Azure DevOps annotation was found'); -} + const project = + entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]; + if (!project) { + throw new Error('Value for annotation dev.azure.com/project was not found'); + } -function getProjectDefinition(annotation: string): { - project: string; - definition: string; -} { - const [project, definition] = annotation.split('/'); - - if (!project && !definition) { + const definition = + entity.metadata.annotations?.[AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION]; + if (!definition) { throw new Error( - 'Value for annotation dev.azure.com/project-definition was not in the correct format: /', + 'Value for annotation dev.azure.com/build-definition was not found', ); } - return { project, definition }; + const repo = undefined; + return { project, repo, definition }; } function getProjectRepo(annotation: string): {