diff --git a/.changeset/three-sheep-remember.md b/.changeset/three-sheep-remember.md new file mode 100644 index 0000000000..51ffc8e3db --- /dev/null +++ b/.changeset/three-sheep-remember.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-azure-devops-backend': minor +--- + +Fixed bug in plugin-azure-devops-backend where proper error was not thrown when gitRepository was not found. diff --git a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.test.ts b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.test.ts index 174c5ff6cd..a056beee02 100644 --- a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.test.ts +++ b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.test.ts @@ -465,6 +465,43 @@ describe('AzureDevOpsApi', () => { ]); }); + it('should throw error when gitRepository is undefined', async () => { + const mockApi = { + getGitApi: jest.fn().mockReturnValue({}), + serverUrl: 'serverUrl', + }; + + (WebApi as unknown as jest.Mock).mockImplementation(() => mockApi); + + const api = AzureDevOpsApi.fromConfig(mockConfig, { + logger: mockLogger, + urlReader: mockUrlReader, + }); + + const pullRequestOptions: PullRequestOptions = { + top: 10, + status: PullRequestStatus.Active, + }; + + api.getGitRepository = jest.fn().mockResolvedValue(undefined); + + const temp = async () => { + try { + await api.getPullRequests('project', 'repo', pullRequestOptions); + return null; + } catch (error) { + return error; + } + }; + + const error = await temp(); + + expect(error).toHaveProperty( + 'message', + 'No repository found for Project "project" with Repository "repo" on host "undefined" under organization "undefined".', + ); + }); + it('should get build definitions', async () => { const mockBuilds: Build[] = [ { diff --git a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts index 4ed54e63eb..468d6a9d88 100644 --- a/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts +++ b/plugins/azure-devops-backend/src/api/AzureDevOpsApi.ts @@ -231,6 +231,11 @@ export class AzureDevOpsApi { host, org, ); + if (!gitRepository) { + throw new Error( + `No repository found for Project "${projectName}" with Repository "${repoName}" on host "${host}" under organization "${org}".`, + ); + } const buildList = await this.getBuildList( projectName, gitRepository.id as string, @@ -262,6 +267,11 @@ export class AzureDevOpsApi { host, org, ); + if (!gitRepository) { + throw new Error( + `No repository found for Project "${projectName}" with Repository "${repoName}" on host "${host}" under organization "${org}".`, + ); + } const webApi = await this.getWebApi(host, org); const client = await webApi.getGitApi(); const tagRefs: GitRef[] = await client.getRefs( @@ -304,6 +314,11 @@ export class AzureDevOpsApi { host, org, ); + if (!gitRepository) { + throw new Error( + `No repository found for Project "${projectName}" with Repository "${repoName}" on host "${host}" under organization "${org}".`, + ); + } const webApi = await this.getWebApi(host, org); const client = await webApi.getGitApi(); const searchCriteria: GitPullRequestSearchCriteria = { @@ -514,6 +529,11 @@ export class AzureDevOpsApi { host, org, ); + if (!gitRepository) { + throw new Error( + `No repository found for Project "${projectName}" with Repository "${repoName}" on host "${host}" under organization "${org}".`, + ); + } repoId = gitRepository.id; }