From c17fa10182574f52491ecdf1295a526f9d62e781 Mon Sep 17 00:00:00 2001 From: Yuri Titi Date: Tue, 7 Mar 2023 16:11:02 -0300 Subject: [PATCH 1/6] feat(@backstage/plugin-catalog-backend-module-azure): Add branch filter support Signed-off-by: Yuri Titi --- .changeset/modern-colts-exercise.md | 5 +++ docs/integrations/azure/discovery.md | 9 +++-- .../src/lib/azure.ts | 1 + .../AzureDevOpsEntityProvider.test.ts | 40 +++++++++++++++++-- .../providers/AzureDevOpsEntityProvider.ts | 10 +++-- .../src/providers/config.test.ts | 24 ++++++++++- .../src/providers/config.ts | 2 + .../src/providers/types.ts | 1 + 8 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 .changeset/modern-colts-exercise.md diff --git a/.changeset/modern-colts-exercise.md b/.changeset/modern-colts-exercise.md new file mode 100644 index 0000000000..d869945870 --- /dev/null +++ b/.changeset/modern-colts-exercise.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-azure': minor +--- + +Add branch filter support diff --git a/docs/integrations/azure/discovery.md b/docs/integrations/azure/discovery.md index 6cb135900e..d96b7d84ab 100644 --- a/docs/integrations/azure/discovery.md +++ b/docs/integrations/azure/discovery.md @@ -61,6 +61,7 @@ catalog: host: selfhostedazure.yourcompany.com organization: myorg project: myproject + branch: development ``` The parameters available are: @@ -70,6 +71,7 @@ The parameters available are: - **`project:`** _(optional)_ Your project slug. Wildcards are supported as show on the examples above. If not set, all projects will be searched. For a project name containing spaces, use both single and double quotes as in `project: '"My Project Name"'`. - **`repository:`** _(optional)_ The repository name. Wildcards are supported as show on the examples above. If not set, all repositories will be searched. - **`path:`** _(optional)_ Where to find catalog-info.yaml files. Defaults to /catalog-info.yaml. +- **`branch:`** _(optional)_ The branch name to use. - **`schedule`** _(optional)_: - **`frequency`**: How often you want the task to run. The system does its best to avoid overlapping invocations. @@ -80,9 +82,10 @@ The parameters available are: - **`scope`** _(optional)_: `'global'` or `'local'`. Sets the scope of concurrency control. -_Note:_ the path parameter follows the same rules as the search on Azure DevOps -web interface. For more details visit the -[official search documentation](https://docs.microsoft.com/en-us/azure/devops/project/search/get-started-search?view=azure-devops). +_Note:_ + +- The path parameter follows the same rules as the search on Azure DevOps web interface. For more details visit the [official search documentation](https://docs.microsoft.com/en-us/azure/devops/project/search/get-started-search?view=azure-devops). +- The branch parameters need that the branch desired it`s added on 'Searchable branches' on Azure DevOps Repositories As this provider is not one of the default providers, you will first need to install the Azure catalog plugin: diff --git a/plugins/catalog-backend-module-azure/src/lib/azure.ts b/plugins/catalog-backend-module-azure/src/lib/azure.ts index 0c96d133d3..714090d5bc 100644 --- a/plugins/catalog-backend-module-azure/src/lib/azure.ts +++ b/plugins/catalog-backend-module-azure/src/lib/azure.ts @@ -34,6 +34,7 @@ export interface CodeSearchResultItem { project: { name: string; }; + branch?: string; } const isCloud = (host: string) => host === 'dev.azure.com'; diff --git a/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts b/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts index dff1164899..53919b9bb4 100644 --- a/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts +++ b/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts @@ -105,9 +105,13 @@ describe('AzureDevOpsEntityProvider', () => { await (taskDef.fn as () => Promise)(); const expectedEntities = codeSearchResults.map(item => { - const url = encodeURI( - `${expectedBaseUrl}/_git/${item.repository.name}?path=${item.path}`, - ); + const url = item.branch + ? encodeURI( + `${expectedBaseUrl}/_git/${item.repository.name}?path=${item.path}&version=GBmybranch`, + ) + : encodeURI( + `${expectedBaseUrl}/_git/${item.repository.name}?path=${item.path}`, + ); return { entity: { apiVersion: 'backstage.io/v1alpha1', @@ -177,6 +181,36 @@ describe('AzureDevOpsEntityProvider', () => { ); }); + // eslint-disable-next-line jest/expect-expect + it('single mutation when repos use branch filter', async () => { + return expectMutation( + 'allReposSingleFile', + { + organization: 'myorganization', + project: 'myproject', + branch: 'mybranch', + }, + [ + { + fileName: 'catalog-info.yaml', + path: '/catalog-info.yaml', + repository: { + name: 'myrepo', + }, + project: { + name: 'myproject', + }, + branch: 'mybranch', + }, + ], + 'https://dev.azure.com/myorganization/myproject', + { + 'myrepo?path=/catalog-info.yaml': + 'generated-589e8cc47341987c7a34f5291791151fa64f7754', + }, + ); + }); + // eslint-disable-next-line jest/expect-expect it('single mutation when multiple repos have multiple files', async () => { return expectMutation( diff --git a/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.ts b/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.ts index 788b9c2f05..457a9e74f2 100644 --- a/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.ts +++ b/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.ts @@ -177,8 +177,12 @@ export class AzureDevOpsEntityProvider implements EntityProvider { private createObjectUrl(file: CodeSearchResultItem): string { const baseUrl = `https://${this.config.host}/${this.config.organization}/${file.project.name}`; - return encodeURI( - `${baseUrl}/_git/${file.repository.name}?path=${file.path}`, - ); + + let fullUrl = `${baseUrl}/_git/${file.repository.name}?path=${file.path}`; + if (this.config.branch) { + fullUrl += `&version=GB${this.config.branch}`; + } + + return encodeURI(fullUrl); } } diff --git a/plugins/catalog-backend-module-azure/src/providers/config.test.ts b/plugins/catalog-backend-module-azure/src/providers/config.test.ts index a09079f1c3..02dbaab90e 100644 --- a/plugins/catalog-backend-module-azure/src/providers/config.test.ts +++ b/plugins/catalog-backend-module-azure/src/providers/config.test.ts @@ -44,17 +44,30 @@ describe('readAzureDevOpsConfigs', () => { }, }, }; + const provider5 = { + host: 'azure.mycompany.com', + organization: 'mycompany', + project: 'myproject', + branch: 'mybranch', + }; + const config = { catalog: { providers: { - azureDevOps: { provider1, provider2, provider3, provider4 }, + azureDevOps: { + provider1, + provider2, + provider3, + provider4, + provider5, + }, }, }, }; const actual = readAzureDevOpsConfigs(new ConfigReader(config)); - expect(actual).toHaveLength(4); + expect(actual).toHaveLength(5); expect(actual[0]).toEqual({ ...provider1, path: '/catalog-info.yaml', @@ -85,5 +98,12 @@ describe('readAzureDevOpsConfigs', () => { frequency: Duration.fromISO(provider4.schedule.frequency), }, }); + expect(actual[4]).toEqual({ + ...provider5, + branch: 'mybranch', + path: '/catalog-info.yaml', + repository: '*', + id: 'provider5', + }); }); }); diff --git a/plugins/catalog-backend-module-azure/src/providers/config.ts b/plugins/catalog-backend-module-azure/src/providers/config.ts index c84c242488..3c10a50863 100644 --- a/plugins/catalog-backend-module-azure/src/providers/config.ts +++ b/plugins/catalog-backend-module-azure/src/providers/config.ts @@ -41,6 +41,7 @@ function readAzureDevOpsConfig(id: string, config: Config): AzureDevOpsConfig { const project = config.getString('project'); const host = config.getOptionalString('host') || 'dev.azure.com'; const repository = config.getOptionalString('repository') || '*'; + const branch = config.getOptionalString('branch'); const path = config.getOptionalString('path') || '/catalog-info.yaml'; const schedule = config.has('schedule') @@ -53,6 +54,7 @@ function readAzureDevOpsConfig(id: string, config: Config): AzureDevOpsConfig { organization, project, repository, + branch, path, schedule, }; diff --git a/plugins/catalog-backend-module-azure/src/providers/types.ts b/plugins/catalog-backend-module-azure/src/providers/types.ts index b22a8551a5..3cd827b380 100644 --- a/plugins/catalog-backend-module-azure/src/providers/types.ts +++ b/plugins/catalog-backend-module-azure/src/providers/types.ts @@ -22,6 +22,7 @@ export type AzureDevOpsConfig = { organization: string; project: string; repository: string; + branch?: string; path: string; schedule?: TaskScheduleDefinition; }; From 616c278b898cb0021d3c9303ba1347e11710e608 Mon Sep 17 00:00:00 2001 From: Yuri Titi Date: Wed, 8 Mar 2023 08:58:49 -0300 Subject: [PATCH 2/6] Update .changeset/modern-colts-exercise.md Co-authored-by: Johan Haals Signed-off-by: Yuri Titi --- .changeset/modern-colts-exercise.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/modern-colts-exercise.md b/.changeset/modern-colts-exercise.md index d869945870..1dfe2f441a 100644 --- a/.changeset/modern-colts-exercise.md +++ b/.changeset/modern-colts-exercise.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-catalog-backend-module-azure': minor +'@backstage/plugin-catalog-backend-module-azure': patch --- Add branch filter support From 7de756703dbd15c0356a3f6ac1049dd2b3511092 Mon Sep 17 00:00:00 2001 From: Yuri Titi Date: Wed, 8 Mar 2023 08:59:36 -0300 Subject: [PATCH 3/6] Update plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts Co-authored-by: Johan Haals Signed-off-by: Yuri Titi --- .../src/providers/AzureDevOpsEntityProvider.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts b/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts index 53919b9bb4..dba29cbb88 100644 --- a/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts +++ b/plugins/catalog-backend-module-azure/src/providers/AzureDevOpsEntityProvider.test.ts @@ -107,7 +107,7 @@ describe('AzureDevOpsEntityProvider', () => { const expectedEntities = codeSearchResults.map(item => { const url = item.branch ? encodeURI( - `${expectedBaseUrl}/_git/${item.repository.name}?path=${item.path}&version=GBmybranch`, + `${expectedBaseUrl}/_git/${item.repository.name}?path=${item.path}&version=GB${item.branch}`, ) : encodeURI( `${expectedBaseUrl}/_git/${item.repository.name}?path=${item.path}`, From 075761de52824a061987bb039c23ba5ee2fbd162 Mon Sep 17 00:00:00 2001 From: Yuri Titi Date: Wed, 8 Mar 2023 14:47:29 -0300 Subject: [PATCH 4/6] Update discovery.md Signed-off-by: Yuri Titi --- docs/integrations/azure/discovery.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/integrations/azure/discovery.md b/docs/integrations/azure/discovery.md index d96b7d84ab..2308b8634b 100644 --- a/docs/integrations/azure/discovery.md +++ b/docs/integrations/azure/discovery.md @@ -85,7 +85,13 @@ The parameters available are: _Note:_ - The path parameter follows the same rules as the search on Azure DevOps web interface. For more details visit the [official search documentation](https://docs.microsoft.com/en-us/azure/devops/project/search/get-started-search?view=azure-devops). -- The branch parameters need that the branch desired it`s added on 'Searchable branches' on Azure DevOps Repositories +- To use branch parameters, it is necessary that the desired branch be added to the "Searchable branches" list within Azure DevOps Repositories. To do this, follow the instructions below: +1. Access your Azure DevOps and open the repository in which you want to add the branch. +2. Click on "Settings" in the lower-left corner of the screen. +3. Select the "Options" option in the left navigation bar. +4. In the "Searchable branches" section, click on the "Add" button to add a new branch. +5. In the window that appears, enter the name of the branch you want to add and click "Add". +6. The added branch will now appear in the "Searchable branches" list. As this provider is not one of the default providers, you will first need to install the Azure catalog plugin: From e91cf3796967814baa9c0ed8d986e090abb0f158 Mon Sep 17 00:00:00 2001 From: Yuri Titi Date: Wed, 8 Mar 2023 21:55:09 -0300 Subject: [PATCH 5/6] docs(azure-discovery): fix prettier Signed-off-by: Yuri Titi --- docs/integrations/azure/discovery.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/integrations/azure/discovery.md b/docs/integrations/azure/discovery.md index 2308b8634b..bcbb00a49d 100644 --- a/docs/integrations/azure/discovery.md +++ b/docs/integrations/azure/discovery.md @@ -86,6 +86,7 @@ _Note:_ - The path parameter follows the same rules as the search on Azure DevOps web interface. For more details visit the [official search documentation](https://docs.microsoft.com/en-us/azure/devops/project/search/get-started-search?view=azure-devops). - To use branch parameters, it is necessary that the desired branch be added to the "Searchable branches" list within Azure DevOps Repositories. To do this, follow the instructions below: + 1. Access your Azure DevOps and open the repository in which you want to add the branch. 2. Click on "Settings" in the lower-left corner of the screen. 3. Select the "Options" option in the left navigation bar. From cad86281a24db3643f3a2f3965a8be0cf91d2d23 Mon Sep 17 00:00:00 2001 From: Yuri Titi Date: Thu, 9 Mar 2023 09:43:30 -0300 Subject: [PATCH 6/6] Add link to discovery information Signed-off-by: Yuri Titi --- .changeset/modern-colts-exercise.md | 1 + 1 file changed, 1 insertion(+) diff --git a/.changeset/modern-colts-exercise.md b/.changeset/modern-colts-exercise.md index 1dfe2f441a..651f10bc98 100644 --- a/.changeset/modern-colts-exercise.md +++ b/.changeset/modern-colts-exercise.md @@ -3,3 +3,4 @@ --- Add branch filter support +https://backstage.io/docs/integrations/azure/discovery