diff --git a/.changeset/modern-colts-exercise.md b/.changeset/modern-colts-exercise.md new file mode 100644 index 0000000000..651f10bc98 --- /dev/null +++ b/.changeset/modern-colts-exercise.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-catalog-backend-module-azure': patch +--- + +Add branch filter support +https://backstage.io/docs/integrations/azure/discovery diff --git a/docs/integrations/azure/discovery.md b/docs/integrations/azure/discovery.md index d1309a80eb..8dd5da85c9 100644 --- a/docs/integrations/azure/discovery.md +++ b/docs/integrations/azure/discovery.md @@ -60,6 +60,7 @@ catalog: host: selfhostedazure.yourcompany.com organization: myorg project: myproject + branch: development ``` The parameters available are: @@ -69,6 +70,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. @@ -79,9 +81,17 @@ 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). +- 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: 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 23a668d60b..d89f10c949 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=GB${item.branch}`, + ) + : 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 89572286a1..6ad4e9ae0b 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; };