From 4880d43e2554ddd448068d623bc0ac087cec4c55 Mon Sep 17 00:00:00 2001 From: Arthur Gavlyukovskiy Date: Wed, 5 Oct 2022 23:20:17 +0200 Subject: [PATCH] Fixed setting default branch for bitbucket server. The main problem comes from `outputs.repoContentsUrl` that does not have reference to a branch, so that subsequent `catalog:register` action fails. Bitbucket Cloud does not support setting default branch from REST API, but since the first pushed branch becomes default branch it works with current approach. Signed-off-by: Arthur Gavlyukovskiy --- .changeset/slimy-suns-learn.md | 5 ++ .../builtin/publish/bitbucketServer.test.ts | 60 +++++++++++++++---- .../builtin/publish/bitbucketServer.ts | 4 ++ 3 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 .changeset/slimy-suns-learn.md diff --git a/.changeset/slimy-suns-learn.md b/.changeset/slimy-suns-learn.md new file mode 100644 index 0000000000..4a924a7a18 --- /dev/null +++ b/.changeset/slimy-suns-learn.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Fixed setting default branch for Bitbucket Server diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.test.ts index 06d3dfb735..52f445098b 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.test.ts @@ -123,7 +123,11 @@ describe('publish:bitbucketServer', () => { 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe('Bearer thing'); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -165,7 +169,11 @@ describe('publish:bitbucketServer', () => { expect(req.headers.get('Authorization')).toBe( 'Basic dGVzdC11c2VyOnRlc3QtcGFzc3dvcmQ=', ); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -206,7 +214,11 @@ describe('publish:bitbucketServer', () => { 'https://no-credentials.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe(`Bearer ${token}`); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -327,7 +339,11 @@ describe('publish:bitbucketServer', () => { 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe('Bearer thing'); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -371,7 +387,11 @@ describe('publish:bitbucketServer', () => { expect(req.headers.get('Authorization')).toBe( 'Basic dGVzdC11c2VyOnRlc3QtcGFzc3dvcmQ=', ); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -419,7 +439,11 @@ describe('publish:bitbucketServer', () => { 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe('Bearer thing'); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'main', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -495,7 +519,11 @@ describe('publish:bitbucketServer', () => { 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe('Bearer thing'); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -562,7 +590,11 @@ describe('publish:bitbucketServer', () => { 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe('Bearer thing'); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -605,7 +637,11 @@ describe('publish:bitbucketServer', () => { 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe('Bearer thing'); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'master', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), @@ -647,7 +683,11 @@ describe('publish:bitbucketServer', () => { 'https://hosted.bitbucket.com/rest/api/1.0/projects/project/repos', (req, res, ctx) => { expect(req.headers.get('Authorization')).toBe('Bearer thing'); - expect(req.body).toEqual({ public: false, name: 'repo' }); + expect(req.body).toEqual({ + public: false, + name: 'repo', + defaultBranch: 'main', + }); return res( ctx.status(201), ctx.set('Content-Type', 'application/json'), diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts index 712a17de70..d0cde4ad31 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucketServer.ts @@ -30,6 +30,7 @@ const createRepository = async (opts: { repo: string; description?: string; repoVisibility: 'private' | 'public'; + defaultBranch: string; authorization: string; apiBaseUrl: string; }) => { @@ -39,6 +40,7 @@ const createRepository = async (opts: { description, authorization, repoVisibility, + defaultBranch, apiBaseUrl, } = opts; @@ -48,6 +50,7 @@ const createRepository = async (opts: { body: JSON.stringify({ name: repo, description: description, + defaultBranch: defaultBranch, public: repoVisibility === 'public', }), headers: { @@ -232,6 +235,7 @@ export function createPublishBitbucketServerAction(options: { project, repo, repoVisibility, + defaultBranch, description, apiBaseUrl, });