From 6a4ad4ee45956936ef1185d3c80b116f7a98330d Mon Sep 17 00:00:00 2001 From: Kacper Nowacki Date: Mon, 3 Jun 2024 14:42:55 +0200 Subject: [PATCH 1/2] Fetch default branch for target repository instead of using hardcoded master branch if no targetBranch is provided Signed-off-by: Kacper Nowacki --- .changeset/fair-rockets-leave.md | 6 +++ .../bitbucketServerPullRequest.test.ts | 18 +++++++ .../src/actions/bitbucketServerPullRequest.ts | 47 ++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 .changeset/fair-rockets-leave.md diff --git a/.changeset/fair-rockets-leave.md b/.changeset/fair-rockets-leave.md new file mode 100644 index 0000000000..41cf28fc9e --- /dev/null +++ b/.changeset/fair-rockets-leave.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-scaffolder-backend-module-bitbucket-server': patch +--- + +Instead of using hardcoded targetBranch we are fetching the default branch from Bitbucket repository. +This prevents from errors when no targetBranch is provided and the default repository branch is different from `master`, for example: `main`. diff --git a/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.test.ts b/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.test.ts index 9d2dd915f8..61adf0e3c8 100644 --- a/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.test.ts +++ b/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.test.ts @@ -103,6 +103,14 @@ describe('publish:bitbucketServer:pull-request', () => { ], start: 0, }; + const responseOfDefaultBranch = { + id: 'refs/heads/main', + displayId: 'main', + type: 'BRANCH', + latestCommit: '1245346tsdfgdf', + latestChangeset: 'wsdfgdh234', + isDefault: true, + }; const responseOfPullRequests = { id: 19, version: 0, @@ -190,6 +198,16 @@ describe('publish:bitbucketServer:pull-request', () => { ); }, ), + rest.get( + 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos/repo/default-branch', + (_, res, ctx) => { + return res( + ctx.status(200), + ctx.set('Content-Type', 'application/json'), + ctx.json(responseOfDefaultBranch), + ); + }, + ), rest.post( 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos/repo/pull-requests', (_, res, ctx) => { diff --git a/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.ts b/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.ts index 8fddffaba4..440e402764 100644 --- a/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.ts +++ b/plugins/scaffolder-backend-module-bitbucket-server/src/actions/bitbucketServerPullRequest.ts @@ -204,6 +204,39 @@ const createBranch = async (opts: { return await response.json(); }; +const getDefaultBranch = async (opts: { + project: string; + repo: string; + authorization: string; + apiBaseUrl: string; +}) => { + const { project, repo, authorization, apiBaseUrl } = opts; + let response: Response; + + const options: RequestInit = { + method: 'GET', + headers: { + Authorization: authorization, + 'Content-Type': 'application/json', + }, + }; + + try { + response = await fetch( + `${apiBaseUrl}/projects/${project}/repos/${repo}/default-branch`, + options, + ); + } catch (error) { + throw error; + } + + const { displayId } = await response.json(); + const defaultBranch = displayId; + if (!defaultBranch) { + throw new Error(`Could not fetch default branch for ${project}/${repo}`); + } + return defaultBranch; +}; /** * Creates a BitbucketServer Pull Request action. * @public @@ -288,7 +321,7 @@ export function createPublishBitbucketServerPullRequestAction(options: { repoUrl, title, description, - targetBranch = 'master', + targetBranch, sourceBranch, gitAuthorName, gitAuthorEmail, @@ -326,10 +359,20 @@ export function createPublishBitbucketServerPullRequestAction(options: { const apiBaseUrl = integrationConfig.config.apiBaseUrl; + let finalTargetBranch = targetBranch; + if (!finalTargetBranch) { + finalTargetBranch = await getDefaultBranch({ + project, + repo, + authorization, + apiBaseUrl, + }); + } + const toRef = await findBranches({ project, repo, - branchName: targetBranch, + branchName: finalTargetBranch!, authorization, apiBaseUrl, }); From a2f8ba85bef7e334706dade75c745cd79f145bb0 Mon Sep 17 00:00:00 2001 From: Kacper Nowacki Date: Tue, 18 Jun 2024 09:36:27 +0200 Subject: [PATCH 2/2] Update .changeset/fair-rockets-leave.md Signed-off-by: Kacper Nowacki --- .changeset/fair-rockets-leave.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/fair-rockets-leave.md b/.changeset/fair-rockets-leave.md index 41cf28fc9e..20f2e97552 100644 --- a/.changeset/fair-rockets-leave.md +++ b/.changeset/fair-rockets-leave.md @@ -2,5 +2,5 @@ '@backstage/plugin-scaffolder-backend-module-bitbucket-server': patch --- -Instead of using hardcoded targetBranch we are fetching the default branch from Bitbucket repository. -This prevents from errors when no targetBranch is provided and the default repository branch is different from `master`, for example: `main`. +Instead of using hardcoded `targetBranch` now fetch the default branch from Bitbucket repository. +This prevents from errors when no `targetBranch` is provided and the default repository branch is different from `master`, for example: `main`.