fix the getLastCommitShortHash and changed the endpoint

Signed-off-by: Carlo Giuseppe Sergi <carlo.sergi@klarna.com>
This commit is contained in:
Carlo Giuseppe Sergi
2022-07-06 16:57:39 +02:00
parent 73782cebc7
commit 90c87f28e8
3 changed files with 22 additions and 25 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Moving to endpoint of bitbucket from https://docs.atlassian.com/bitbucket-server/rest/7.9.0/bitbucket-rest.html#idp222 to https://docs.atlassian.com/bitbucket-server/rest/7.9.0/bitbucket-rest.html#idp224, to have the last commit in function of different branch, and not only the list of default branch
@@ -83,13 +83,11 @@ describe('BitbucketServerUrlReader', () => {
),
),
rest.get(
'https://api.bitbucket.mycompany.net/rest/api/1.0/projects/backstage/repos/mock/commits',
'https://api.bitbucket.mycompany.net/rest/api/1.0/projects/backstage/repos/mock/commits/*',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.json({
values: [{ id: '12ab34cd56ef78gh90ij12kl34mn56op78qr90st' }],
}),
ctx.json({ id: '12ab34cd56ef78gh90ij12kl34mn56op78qr90st' }),
),
),
);
@@ -132,13 +130,11 @@ describe('BitbucketServerUrlReader', () => {
),
),
rest.get(
'https://api.bitbucket.mycompany.net/rest/api/1.0/projects/backstage/repos/mock/commits',
'https://api.bitbucket.mycompany.net/rest/api/1.0/projects/backstage/repos/mock/commits/*',
(_, res, ctx) =>
res(
ctx.status(200),
ctx.json({
values: [{ id: '12ab34cd56ef78gh90ij12kl34mn56op78qr90st' }],
}),
ctx.json({ id: '12ab34cd56ef78gh90ij12kl34mn56op78qr90st' }),
),
),
);
@@ -186,33 +186,29 @@ export class BitbucketServerUrlReader implements UrlReader {
}
private async getLastCommitShortHash(url: string): Promise<string> {
const { name: repoName, owner: project } = parseGitUrl(url);
const { name: repoName, owner: project, ref: branch } = parseGitUrl(url);
// Bitbucket Server https://docs.atlassian.com/bitbucket-server/rest/7.9.0/bitbucket-rest.html#idp222
const commitsApiUrl = `${this.integration.config.apiBaseUrl}/projects/${project}/repos/${repoName}/commits`;
// Bitbucket Server https://docs.atlassian.com/bitbucket-server/rest/7.9.0/bitbucket-rest.html#idp224
const commitApiUrl = `${this.integration.config.apiBaseUrl}/projects/${project}/repos/${repoName}/commits/${branch}`;
const commitsResponse = await fetch(
commitsApiUrl,
const commitResponse = await fetch(
commitApiUrl,
getBitbucketServerRequestOptions(this.integration.config),
);
if (!commitsResponse.ok) {
const message = `Failed to retrieve commits from ${commitsApiUrl}, ${commitsResponse.status} ${commitsResponse.statusText}`;
if (commitsResponse.status === 404) {
if (!commitResponse.ok) {
const message = `Failed to retrieve commits from ${commitApiUrl}, ${commitResponse.status} ${commitResponse.statusText}`;
if (commitResponse.status === 404) {
throw new NotFoundError(message);
}
throw new Error(message);
}
const commits = await commitsResponse.json();
if (
commits &&
commits.values &&
commits.values.length > 0 &&
commits.values[0].id
) {
return commits.values[0].id.substring(0, 12);
const commits = await commitResponse.json();
if (commits && commits.id) {
return commits.id.substring(0, 12);
}
throw new Error(`Failed to read response from ${commitsApiUrl}`);
throw new Error(`Failed to read response from ${commitApiUrl}`);
}
}