Merge pull request #14113 from awanlin/topic/bitbucket-support-no-branch

Added back support for when no branch is provided for the Bitbucket Server UrlReader
This commit is contained in:
Fredrik Adelöw
2022-10-17 13:48:51 +01:00
committed by GitHub
3 changed files with 47 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Added back support for when no branch is provided for the Bitbucket Server `UrlReader`
@@ -102,6 +102,25 @@ describe('BitbucketServerUrlReader', () => {
}),
),
),
rest.get(
'https://api.bitbucket.mycompany.net/rest/api/1.0/projects/backstage/repos/mock/branches/default',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.json({
id: 'refs/heads/master',
displayId: 'master',
type: 'BRANCH',
latestCommit: '3bdd5457286abdf920db4b77bf2fef79a06190c2',
latestChangeset: '3bdd5457286abdf920db4b77bf2fef79a06190c2',
isDefault: true,
}),
),
),
rest.get(
'https://api.bitbucket.mycompany.net/rest/api/1.0/projects/backstage/repos/mock/default-branch',
(_, res, ctx) => res(ctx.status(404)),
),
);
});
@@ -119,6 +138,14 @@ describe('BitbucketServerUrlReader', () => {
expect(indexMarkdownFile.toString()).toBe('# Test\n');
});
it('uses default branch when no branch is provided', async () => {
const response = await reader.readTree(
'https://bitbucket.mycompany.net/projects/backstage/repos/mock/browse/src',
);
expect(response.etag).toBe('3bdd5457286a');
});
});
describe('readTree without branch', () => {
@@ -188,11 +188,13 @@ export class BitbucketServerUrlReader implements UrlReader {
private async getLastCommitShortHash(url: string): Promise<string> {
const { name: repoName, owner: project, ref: branch } = parseGitUrl(url);
const branchListUrl = `${
this.integration.config.apiBaseUrl
}/projects/${project}/repos/${repoName}/branches?filterText=${encodeURIComponent(
branch,
)}`;
// If a branch is provided use that otherwise fall back to the default branch
const branchParameter = branch
? `?filterText=${encodeURIComponent(branch)}`
: '/default';
// https://docs.atlassian.com/bitbucket-server/rest/7.9.0/bitbucket-rest.html#idp211 (branches docs)
const branchListUrl = `${this.integration.config.apiBaseUrl}/projects/${project}/repos/${repoName}/branches${branchParameter}`;
const branchListResponse = await fetch(
branchListUrl,
@@ -216,8 +218,15 @@ export class BitbucketServerUrlReader implements UrlReader {
return exactBranchMatch.latestCommit.substring(0, 12);
}
// Handle when no branch is provided using the default as the fallback
if (!branch && branchMatches) {
return branchMatches.latestCommit.substring(0, 12);
}
throw new Error(
`Failed to find branch "${branch}" in property "displayId" of response to ${branchListUrl}`,
`Failed to find Last Commit using ${
branch ? `branch "${branch}"` : 'default branch'
} in response from ${branchListUrl}`,
);
}
}