From 47616494b61f13d6e66c2c2755c68959a80a599b Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Wed, 16 Aug 2023 20:40:22 +0530 Subject: [PATCH 01/11] fetching project/repo from location Signed-off-by: Tarak Nath Tatwa --- plugins/azure-devops/README.md | 6 +++++ plugins/azure-devops/src/constants.ts | 1 + plugins/azure-devops/src/plugin.ts | 2 ++ .../src/utils/getAnnotationFromEntity.ts | 15 +++++++++++ .../azure-devops/src/utils/isAzureLocation.ts | 25 +++++++++++++++++++ 5 files changed, 49 insertions(+) create mode 100644 plugins/azure-devops/src/utils/isAzureLocation.ts diff --git a/plugins/azure-devops/README.md b/plugins/azure-devops/README.md index 7eeae09813..5055180be1 100644 --- a/plugins/azure-devops/README.md +++ b/plugins/azure-devops/README.md @@ -61,6 +61,12 @@ spec: # ... ``` +```yaml +backstage.io/managed-by-location: https://dev.azure.com/{org}/{project}/_git/{repo}?path=/.catalog/component.yml +``` + +if `dev.azure.com/project-repo` annotation is not provided and `dev.azure.com/project` not exits then to extract the project and repo information we can use `backstage.io/managed-by-location` annotation if location contains `dev.azure.com` in url to enable CICD tab. + #### Azure Pipelines Only If you are only using Azure Pipelines along with a different SCM tool then you can use the following two annotations to see Builds: diff --git a/plugins/azure-devops/src/constants.ts b/plugins/azure-devops/src/constants.ts index 95e5be4930..128b593145 100644 --- a/plugins/azure-devops/src/constants.ts +++ b/plugins/azure-devops/src/constants.ts @@ -18,4 +18,5 @@ 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_PROJECT_LOCATION = 'backstage.io/managed-by-location'; 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 4dd423f35a..e6eb0556f6 100644 --- a/plugins/azure-devops/src/plugin.ts +++ b/plugins/azure-devops/src/plugin.ts @@ -37,6 +37,7 @@ import { import { AzureDevOpsClient } from './api/AzureDevOpsClient'; import { Entity } from '@backstage/catalog-model'; import { azureDevOpsApiRef } from './api/AzureDevOpsApi'; +import { isDevAzureLocation } from './utils/isAzureLocation'; /** @public */ export const isAzureDevOpsAvailable = (entity: Entity) => @@ -45,6 +46,7 @@ export const isAzureDevOpsAvailable = (entity: Entity) => /** @public */ export const isAzurePipelinesAvailable = (entity: Entity) => Boolean(entity.metadata.annotations?.[AZURE_DEVOPS_REPO_ANNOTATION]) || + isDevAzureLocation(entity) || (Boolean(entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]) && Boolean( entity.metadata.annotations?.[AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION], diff --git a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts index 41cc1e6628..14f19668f0 100644 --- a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts @@ -18,6 +18,7 @@ import { AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION, AZURE_DEVOPS_PROJECT_ANNOTATION, AZURE_DEVOPS_REPO_ANNOTATION, + AZURE_DEVOPS_PROJECT_LOCATION, } from '../constants'; import { Entity } from '@backstage/catalog-model'; @@ -37,6 +38,20 @@ export function getAnnotationFromEntity(entity: Entity): { const project = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]; + + const location = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION]; + const isAzureDevUrl = location?.includes('dev.azure.com'); + + if (!project && location && isAzureDevUrl) { + const locInfoArr = location + .substring(location.indexOf('//') + 2) + .split('/'); + + const [proj, repo] = [locInfoArr[2], locInfoArr[4].split('?')[0]]; + const definition = undefined; + return { project: proj, repo, definition }; + } + if (!project) { throw new Error('Value for annotation dev.azure.com/project was not found'); } diff --git a/plugins/azure-devops/src/utils/isAzureLocation.ts b/plugins/azure-devops/src/utils/isAzureLocation.ts new file mode 100644 index 0000000000..6f0be835b7 --- /dev/null +++ b/plugins/azure-devops/src/utils/isAzureLocation.ts @@ -0,0 +1,25 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Entity } from '@backstage/catalog-model'; +import { AZURE_DEVOPS_PROJECT_LOCATION } from '../constants'; + +export const isDevAzureLocation = (entity: Entity): boolean => { + return ( + entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION].includes( + 'dev.azure.com', + ) || false + ); +}; From 91be863cacc063045d56e4ca4a12aafa4fb98dd9 Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Wed, 16 Aug 2023 21:07:25 +0530 Subject: [PATCH 02/11] correcting build error Signed-off-by: Tarak Nath Tatwa --- plugins/azure-devops/src/utils/isAzureLocation.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/plugins/azure-devops/src/utils/isAzureLocation.ts b/plugins/azure-devops/src/utils/isAzureLocation.ts index 6f0be835b7..7398c08fd0 100644 --- a/plugins/azure-devops/src/utils/isAzureLocation.ts +++ b/plugins/azure-devops/src/utils/isAzureLocation.ts @@ -17,9 +17,8 @@ import { Entity } from '@backstage/catalog-model'; import { AZURE_DEVOPS_PROJECT_LOCATION } from '../constants'; export const isDevAzureLocation = (entity: Entity): boolean => { - return ( - entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION].includes( - 'dev.azure.com', - ) || false - ); + const location = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION]; + + if (location) return location.includes('dev.azure.com'); + return false; }; From 09cfc3cf467d3e81630d1ff0973d0a57122575fd Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Wed, 23 Aug 2023 10:59:44 +0530 Subject: [PATCH 03/11] feedback changes Signed-off-by: Tarak Nath Tatwa --- .changeset/tiny-bulldogs-grow.md | 5 +++++ plugins/azure-devops/README.md | 2 +- plugins/azure-devops/src/constants.ts | 1 + .../src/utils/getAnnotationFromEntity.ts | 14 +++++++++----- plugins/azure-devops/src/utils/isAzureLocation.ts | 7 +++++-- 5 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 .changeset/tiny-bulldogs-grow.md diff --git a/.changeset/tiny-bulldogs-grow.md b/.changeset/tiny-bulldogs-grow.md new file mode 100644 index 0000000000..b6b54d5b57 --- /dev/null +++ b/.changeset/tiny-bulldogs-grow.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-azure-devops': minor +--- + +the project and repository details can be obtained using the `backstage.io/managed-by-location` annotation diff --git a/plugins/azure-devops/README.md b/plugins/azure-devops/README.md index 5055180be1..cc0f4c67f3 100644 --- a/plugins/azure-devops/README.md +++ b/plugins/azure-devops/README.md @@ -65,7 +65,7 @@ spec: backstage.io/managed-by-location: https://dev.azure.com/{org}/{project}/_git/{repo}?path=/.catalog/component.yml ``` -if `dev.azure.com/project-repo` annotation is not provided and `dev.azure.com/project` not exits then to extract the project and repo information we can use `backstage.io/managed-by-location` annotation if location contains `dev.azure.com` in url to enable CICD tab. +In cases where the annotations `dev.azure.com/project-repo` or `dev.azure.com/project` are not present, **the project and repository details can be obtained using the `backstage.io/managed-by-location` annotation**. This method comes into play when the URL within the location includes `dev.azure.com`, which then triggers the activation of the CICD tab. #### Azure Pipelines Only diff --git a/plugins/azure-devops/src/constants.ts b/plugins/azure-devops/src/constants.ts index 128b593145..bfc6603abf 100644 --- a/plugins/azure-devops/src/constants.ts +++ b/plugins/azure-devops/src/constants.ts @@ -20,3 +20,4 @@ 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_PROJECT_LOCATION = 'backstage.io/managed-by-location'; export const AZURE_DEVOPS_DEFAULT_TOP: number = 10; +export const AZURE_DEVOPS_URL_FORMAT = 'https://dev.azure.com'; diff --git a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts index 14f19668f0..70cff6da0d 100644 --- a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts @@ -19,8 +19,11 @@ import { AZURE_DEVOPS_PROJECT_ANNOTATION, AZURE_DEVOPS_REPO_ANNOTATION, AZURE_DEVOPS_PROJECT_LOCATION, + AZURE_DEVOPS_URL_FORMAT, } from '../constants'; +import parseGitUrl from 'git-url-parse'; + import { Entity } from '@backstage/catalog-model'; export function getAnnotationFromEntity(entity: Entity): { @@ -40,14 +43,15 @@ export function getAnnotationFromEntity(entity: Entity): { entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]; const location = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION]; - const isAzureDevUrl = location?.includes('dev.azure.com'); + const isAzureDevUrl = location?.includes(AZURE_DEVOPS_URL_FORMAT); if (!project && location && isAzureDevUrl) { - const locInfoArr = location - .substring(location.indexOf('//') + 2) - .split('/'); + const { name: repo, owner } = parseGitUrl(location); + + const projArrData = owner.split('/'); + + const proj = projArrData[projArrData.length - 2]; - const [proj, repo] = [locInfoArr[2], locInfoArr[4].split('?')[0]]; const definition = undefined; return { project: proj, repo, definition }; } diff --git a/plugins/azure-devops/src/utils/isAzureLocation.ts b/plugins/azure-devops/src/utils/isAzureLocation.ts index 7398c08fd0..d9d4cd6fdf 100644 --- a/plugins/azure-devops/src/utils/isAzureLocation.ts +++ b/plugins/azure-devops/src/utils/isAzureLocation.ts @@ -14,11 +14,14 @@ * limitations under the License. */ import { Entity } from '@backstage/catalog-model'; -import { AZURE_DEVOPS_PROJECT_LOCATION } from '../constants'; +import { + AZURE_DEVOPS_PROJECT_LOCATION, + AZURE_DEVOPS_URL_FORMAT, +} from '../constants'; export const isDevAzureLocation = (entity: Entity): boolean => { const location = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION]; - if (location) return location.includes('dev.azure.com'); + if (location) return location.includes(AZURE_DEVOPS_URL_FORMAT); return false; }; From 93bcacb1cb0bca788d84293fab4838b1e5e55e7d Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Wed, 23 Aug 2023 11:28:41 +0530 Subject: [PATCH 04/11] adding package in azure devops plugin Signed-off-by: Tarak Nath Tatwa --- plugins/azure-devops/package.json | 1 + yarn.lock | 1 + 2 files changed, 2 insertions(+) diff --git a/plugins/azure-devops/package.json b/plugins/azure-devops/package.json index abc08d6c65..6bedd5445b 100644 --- a/plugins/azure-devops/package.json +++ b/plugins/azure-devops/package.json @@ -38,6 +38,7 @@ "@material-ui/core": "^4.12.2", "@material-ui/icons": "^4.9.1", "@material-ui/lab": "4.0.0-alpha.61", + "git-url-parse": "^13.1.0", "humanize-duration": "^3.27.0", "luxon": "^3.0.0", "react-use": "^17.2.4" diff --git a/yarn.lock b/yarn.lock index 1341cca2f9..88c7a1fa08 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4716,6 +4716,7 @@ __metadata: "@testing-library/user-event": ^14.0.0 "@types/node": ^16.11.26 "@types/react": ^16.13.1 || ^17.0.0 + git-url-parse: ^13.1.0 humanize-duration: ^3.27.0 luxon: ^3.0.0 msw: ^1.0.0 From bc5b26ea580d083f0f7797334d6c7e54c62b37db Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Tue, 5 Sep 2023 10:50:17 +0530 Subject: [PATCH 05/11] feedback changes Signed-off-by: Tarak Nath Tatwa --- plugins/azure-devops/README.md | 6 ------ plugins/azure-devops/package.json | 1 - plugins/azure-devops/src/constants.ts | 2 -- plugins/azure-devops/src/plugin.ts | 2 -- .../src/utils/getAnnotationFromEntity.ts | 18 ------------------ 5 files changed, 29 deletions(-) diff --git a/plugins/azure-devops/README.md b/plugins/azure-devops/README.md index cc0f4c67f3..7eeae09813 100644 --- a/plugins/azure-devops/README.md +++ b/plugins/azure-devops/README.md @@ -61,12 +61,6 @@ spec: # ... ``` -```yaml -backstage.io/managed-by-location: https://dev.azure.com/{org}/{project}/_git/{repo}?path=/.catalog/component.yml -``` - -In cases where the annotations `dev.azure.com/project-repo` or `dev.azure.com/project` are not present, **the project and repository details can be obtained using the `backstage.io/managed-by-location` annotation**. This method comes into play when the URL within the location includes `dev.azure.com`, which then triggers the activation of the CICD tab. - #### Azure Pipelines Only If you are only using Azure Pipelines along with a different SCM tool then you can use the following two annotations to see Builds: diff --git a/plugins/azure-devops/package.json b/plugins/azure-devops/package.json index 29f5fc1eb6..9100f37091 100644 --- a/plugins/azure-devops/package.json +++ b/plugins/azure-devops/package.json @@ -40,7 +40,6 @@ "@material-ui/lab": "4.0.0-alpha.61", "@types/react": "^16.13.1 || ^17.0.0", "humanize-duration": "^3.27.0", - "git-url-parse": "^13.1.0", "luxon": "^3.0.0", "react-use": "^17.2.4" }, diff --git a/plugins/azure-devops/src/constants.ts b/plugins/azure-devops/src/constants.ts index bfc6603abf..95e5be4930 100644 --- a/plugins/azure-devops/src/constants.ts +++ b/plugins/azure-devops/src/constants.ts @@ -18,6 +18,4 @@ 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_PROJECT_LOCATION = 'backstage.io/managed-by-location'; export const AZURE_DEVOPS_DEFAULT_TOP: number = 10; -export const AZURE_DEVOPS_URL_FORMAT = 'https://dev.azure.com'; diff --git a/plugins/azure-devops/src/plugin.ts b/plugins/azure-devops/src/plugin.ts index e6eb0556f6..4dd423f35a 100644 --- a/plugins/azure-devops/src/plugin.ts +++ b/plugins/azure-devops/src/plugin.ts @@ -37,7 +37,6 @@ import { import { AzureDevOpsClient } from './api/AzureDevOpsClient'; import { Entity } from '@backstage/catalog-model'; import { azureDevOpsApiRef } from './api/AzureDevOpsApi'; -import { isDevAzureLocation } from './utils/isAzureLocation'; /** @public */ export const isAzureDevOpsAvailable = (entity: Entity) => @@ -46,7 +45,6 @@ export const isAzureDevOpsAvailable = (entity: Entity) => /** @public */ export const isAzurePipelinesAvailable = (entity: Entity) => Boolean(entity.metadata.annotations?.[AZURE_DEVOPS_REPO_ANNOTATION]) || - isDevAzureLocation(entity) || (Boolean(entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]) && Boolean( entity.metadata.annotations?.[AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION], diff --git a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts index 70cff6da0d..3e73c9646c 100644 --- a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts @@ -18,12 +18,8 @@ import { AZURE_DEVOPS_BUILD_DEFINITION_ANNOTATION, AZURE_DEVOPS_PROJECT_ANNOTATION, AZURE_DEVOPS_REPO_ANNOTATION, - AZURE_DEVOPS_PROJECT_LOCATION, - AZURE_DEVOPS_URL_FORMAT, } from '../constants'; -import parseGitUrl from 'git-url-parse'; - import { Entity } from '@backstage/catalog-model'; export function getAnnotationFromEntity(entity: Entity): { @@ -42,20 +38,6 @@ export function getAnnotationFromEntity(entity: Entity): { const project = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]; - const location = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION]; - const isAzureDevUrl = location?.includes(AZURE_DEVOPS_URL_FORMAT); - - if (!project && location && isAzureDevUrl) { - const { name: repo, owner } = parseGitUrl(location); - - const projArrData = owner.split('/'); - - const proj = projArrData[projArrData.length - 2]; - - const definition = undefined; - return { project: proj, repo, definition }; - } - if (!project) { throw new Error('Value for annotation dev.azure.com/project was not found'); } From 88318cdfad8ede81f0f101deda1dac832b4275c7 Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Tue, 5 Sep 2023 10:53:03 +0530 Subject: [PATCH 06/11] feedback changes Signed-off-by: Tarak Nath Tatwa --- .changeset/tiny-bulldogs-grow.md | 4 ++-- .../src/modules/core/AnnotateScmSlugEntityProcessor.ts | 4 ++++ yarn.lock | 1 - 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.changeset/tiny-bulldogs-grow.md b/.changeset/tiny-bulldogs-grow.md index b6b54d5b57..406a087ca3 100644 --- a/.changeset/tiny-bulldogs-grow.md +++ b/.changeset/tiny-bulldogs-grow.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-azure-devops': minor +'@backstage/plugin-azure-devops': patch --- -the project and repository details can be obtained using the `backstage.io/managed-by-location` annotation +set azure annotation `dev.azure.com/project-repo` in `AnnotateScmSlugEntityProcessor` to find the project and repo information for the repos that contains `dev.azure.com` in the url diff --git a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts index 241a27433e..a95a7e874e 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.ts @@ -27,6 +27,7 @@ import { CatalogProcessor } from '@backstage/plugin-catalog-node'; const GITHUB_ACTIONS_ANNOTATION = 'github.com/project-slug'; const GITLAB_ACTIONS_ANNOTATION = 'gitlab.com/project-slug'; +const AZURE_ACTIONS_ANNOTATION = 'dev.azure.com/project-repo'; /** @public */ export class AnnotateScmSlugEntityProcessor implements CatalogProcessor { @@ -81,6 +82,9 @@ export class AnnotateScmSlugEntityProcessor implements CatalogProcessor { case 'gitlab': annotation = GITLAB_ACTIONS_ANNOTATION; break; + case 'azure': + annotation = AZURE_ACTIONS_ANNOTATION; + break; default: return entity; } diff --git a/yarn.lock b/yarn.lock index 6d413632d1..363d1b26cf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4900,7 +4900,6 @@ __metadata: "@testing-library/user-event": ^14.0.0 "@types/node": ^16.11.26 "@types/react": ^16.13.1 || ^17.0.0 - git-url-parse: ^13.1.0 humanize-duration: ^3.27.0 luxon: ^3.0.0 msw: ^1.0.0 From ecd3cd587c3e3582f8f3548224faeb50591370b6 Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Tue, 5 Sep 2023 10:57:55 +0530 Subject: [PATCH 07/11] cleaning up Signed-off-by: Tarak Nath Tatwa --- .../src/utils/getAnnotationFromEntity.ts | 1 - .../azure-devops/src/utils/isAzureLocation.ts | 27 ------------------- 2 files changed, 28 deletions(-) delete mode 100644 plugins/azure-devops/src/utils/isAzureLocation.ts diff --git a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts index 3e73c9646c..41cc1e6628 100644 --- a/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts +++ b/plugins/azure-devops/src/utils/getAnnotationFromEntity.ts @@ -37,7 +37,6 @@ export function getAnnotationFromEntity(entity: Entity): { const project = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_ANNOTATION]; - if (!project) { throw new Error('Value for annotation dev.azure.com/project was not found'); } diff --git a/plugins/azure-devops/src/utils/isAzureLocation.ts b/plugins/azure-devops/src/utils/isAzureLocation.ts deleted file mode 100644 index d9d4cd6fdf..0000000000 --- a/plugins/azure-devops/src/utils/isAzureLocation.ts +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright 2023 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import { Entity } from '@backstage/catalog-model'; -import { - AZURE_DEVOPS_PROJECT_LOCATION, - AZURE_DEVOPS_URL_FORMAT, -} from '../constants'; - -export const isDevAzureLocation = (entity: Entity): boolean => { - const location = entity.metadata.annotations?.[AZURE_DEVOPS_PROJECT_LOCATION]; - - if (location) return location.includes(AZURE_DEVOPS_URL_FORMAT); - return false; -}; From 959cea5792e74ec8e3a6ec8ffb51dede918d0316 Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Wed, 6 Sep 2023 13:21:36 +0530 Subject: [PATCH 08/11] adding test cases Signed-off-by: Tarak Nath Tatwa --- .../AnnotateScmSlugEntityProcessor.test.ts | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts index 0cde9417db..95bd8a554e 100644 --- a/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts +++ b/plugins/catalog-backend/src/modules/core/AnnotateScmSlugEntityProcessor.test.ts @@ -268,4 +268,163 @@ describe('AnnotateScmSlugEntityProcessor', () => { }); }); }); + + describe('azure', () => { + it('adds annotation', async () => { + const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }; + const location: LocationSpec = { + type: 'url', + target: + 'https://dev.azure.com/organization/project/_git/repository?path=%2Fcatalog-info.yaml', + }; + + const processor = AnnotateScmSlugEntityProcessor.fromConfig( + new ConfigReader({}), + ); + + expect(await processor.preProcessEntity(entity, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'dev.azure.com/project-repo': 'project/repository', + }, + }, + }); + }); + + it('does not override existing annotation', async () => { + const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'dev.azure.com/project-repo': 'myproj/myrepo', + }, + }, + }; + const location: LocationSpec = { + type: 'url', + target: + 'https://dev.azure.com/organization/project/_git/repository?path=%2Fcatalog-info.yaml', + }; + + const processor = AnnotateScmSlugEntityProcessor.fromConfig( + new ConfigReader({}), + ); + + expect(await processor.preProcessEntity(entity, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'dev.azure.com/project-repo': 'myproj/myrepo', + }, + }, + }); + }); + + it('should not add annotation for other providers', async () => { + const entity: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }; + const location: LocationSpec = { + type: 'url', + target: + 'https://not-in-mock-config.example.com/backstage/backstage/-/blob/master/catalog-info.yaml', + }; + + const processor = AnnotateScmSlugEntityProcessor.fromConfig( + new ConfigReader({}), + ); + + expect(await processor.preProcessEntity(entity, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }); + }); + + it('should only process applicable kinds', async () => { + const component: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + }, + }; + + const api: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'API', + metadata: { + name: 'my-component', + }, + }; + + const system: Entity = { + apiVersion: 'backstage.io/v1alpha1', + kind: 'System', + metadata: { + name: 'my-component', + }, + }; + + const location: LocationSpec = { + type: 'url', + target: + 'https://dev.azure.com/organization/project/_git/repository?path=%2Fcatalog-info.yaml', + }; + + const processor = AnnotateScmSlugEntityProcessor.fromConfig( + new ConfigReader({}), + { kinds: ['API', 'Component'] }, + ); + + expect(await processor.preProcessEntity(component, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'Component', + metadata: { + name: 'my-component', + annotations: { + 'dev.azure.com/project-repo': 'project/repository', + }, + }, + }); + + expect(await processor.preProcessEntity(api, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'API', + metadata: { + name: 'my-component', + annotations: { + 'dev.azure.com/project-repo': 'project/repository', + }, + }, + }); + + expect(await processor.preProcessEntity(system, location)).toEqual({ + apiVersion: 'backstage.io/v1alpha1', + kind: 'System', + metadata: { + name: 'my-component', + }, + }); + }); + }); }); From 1dfedc6f28295db6d42e703d3a219ae977ef88ec Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Wed, 13 Sep 2023 14:05:56 +0530 Subject: [PATCH 09/11] updating changeset with minor(feedback) Signed-off-by: Tarak Nath Tatwa --- .changeset/tiny-bulldogs-grow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/tiny-bulldogs-grow.md b/.changeset/tiny-bulldogs-grow.md index 406a087ca3..3b9d04de7a 100644 --- a/.changeset/tiny-bulldogs-grow.md +++ b/.changeset/tiny-bulldogs-grow.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-azure-devops': patch +'@backstage/plugin-azure-devops': minor --- set azure annotation `dev.azure.com/project-repo` in `AnnotateScmSlugEntityProcessor` to find the project and repo information for the repos that contains `dev.azure.com` in the url From d8e38ecc99c060c739f1bd906cdc7e84de6fd5bc Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Fri, 15 Sep 2023 11:32:14 +0530 Subject: [PATCH 10/11] changing package name Signed-off-by: Tarak Nath Tatwa --- .changeset/tiny-bulldogs-grow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/tiny-bulldogs-grow.md b/.changeset/tiny-bulldogs-grow.md index 3b9d04de7a..e736a0b7f8 100644 --- a/.changeset/tiny-bulldogs-grow.md +++ b/.changeset/tiny-bulldogs-grow.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-azure-devops': minor +'@backstage/plugin-catalog-backend': minor' --- set azure annotation `dev.azure.com/project-repo` in `AnnotateScmSlugEntityProcessor` to find the project and repo information for the repos that contains `dev.azure.com` in the url From 7e8242df5c31c9e6cc629795bffccd084421fc16 Mon Sep 17 00:00:00 2001 From: Tarak Nath Tatwa Date: Fri, 15 Sep 2023 11:40:06 +0530 Subject: [PATCH 11/11] correcting typo Signed-off-by: Tarak Nath Tatwa --- .changeset/tiny-bulldogs-grow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/tiny-bulldogs-grow.md b/.changeset/tiny-bulldogs-grow.md index e736a0b7f8..3c999b8762 100644 --- a/.changeset/tiny-bulldogs-grow.md +++ b/.changeset/tiny-bulldogs-grow.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-catalog-backend': minor' +'@backstage/plugin-catalog-backend': minor --- set azure annotation `dev.azure.com/project-repo` in `AnnotateScmSlugEntityProcessor` to find the project and repo information for the repos that contains `dev.azure.com` in the url