From 8b269e1879b580bc29b5852421cb33b29e3b8d40 Mon Sep 17 00:00:00 2001 From: Martin Wallgren Date: Wed, 31 Jan 2024 10:06:30 +0100 Subject: [PATCH] Scaffolder-backend-gerrit: Set branches to defaultBranch When a project is created in `publish:gerrit` the default branch needs to be provided to the Gerrit API that creates the project. If the branches attribute is not provided, the default branch for the project will be set to whatever is configured as default on the Gerrit host. Reference: https://gerrit-review.googlesource.com/Documentation/rest-api-projects.html#project-input Signed-off-by: Martin Wallgren --- .changeset/short-cherries-mix.md | 5 ++ .../src/actions/gerrit.test.ts | 55 +++++++++++++++++++ .../src/actions/gerrit.ts | 5 +- 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .changeset/short-cherries-mix.md diff --git a/.changeset/short-cherries-mix.md b/.changeset/short-cherries-mix.md new file mode 100644 index 0000000000..866f7a0971 --- /dev/null +++ b/.changeset/short-cherries-mix.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-gerrit': patch +--- + +Provide default branch when creating repositories. diff --git a/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.test.ts b/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.test.ts index d05c8727c4..2bd7783cb6 100644 --- a/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.test.ts +++ b/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.test.ts @@ -101,6 +101,7 @@ describe('publish:gerrit', () => { 'Basic Z2Vycml0dXNlcjp1c2VydG9rZW4=', ); expect(req.body).toEqual({ + branches: ['master'], create_empty_commit: false, owners: ['owner'], description, @@ -150,6 +151,7 @@ describe('publish:gerrit', () => { 'Basic Z2Vycml0dXNlcjp1c2VydG9rZW4=', ); expect(req.body).toEqual({ + branches: ['master'], create_empty_commit: false, owners: ['owner'], description, @@ -200,6 +202,7 @@ describe('publish:gerrit', () => { 'Basic Z2Vycml0dXNlcjp1c2VydG9rZW4=', ); expect(req.body).toEqual({ + branches: ['master'], create_empty_commit: false, owners: [], description, @@ -241,6 +244,58 @@ describe('publish:gerrit', () => { 'https://gerrithost.org/repo/+/refs/heads/master', ); }); + + it('can correctly create a new project with main as default branch', async () => { + expect.assertions(5); + server.use( + rest.put('https://gerrithost.org/a/projects/repo', (req, res, ctx) => { + expect(req.headers.get('Authorization')).toBe( + 'Basic Z2Vycml0dXNlcjp1c2VydG9rZW4=', + ); + expect(req.body).toEqual({ + branches: ['main'], + create_empty_commit: false, + owners: [], + description, + parent: 'workspace', + }); + return res( + ctx.status(201), + ctx.set('Content-Type', 'application/json'), + ctx.json({}), + ); + }), + ); + + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + repoUrl: 'gerrithost.org?workspace=workspace&repo=repo', + defaultBranch: 'main', + }, + }); + + expect(initRepoAndPush).toHaveBeenCalledWith({ + dir: mockContext.workspacePath, + remoteUrl: 'https://gerrithost.org/a/repo', + defaultBranch: 'main', + auth: { username: 'gerrituser', password: 'usertoken' }, + logger: mockContext.logger, + commitMessage: expect.stringContaining('initial commit\n\nChange-Id:'), + gitAuthorInfo: {}, + }); + + expect(mockContext.output).toHaveBeenCalledWith( + 'remoteUrl', + 'https://gerrithost.org/a/repo', + ); + expect(mockContext.output).toHaveBeenCalledWith( + 'repoContentsUrl', + 'https://gerrithost.org/repo/+/refs/heads/main', + ); + }); + it('should not create new projects on dryRun', async () => { await action.handler({ ...mockContext, diff --git a/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.ts b/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.ts index 6f729227e6..102d48a706 100644 --- a/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.ts +++ b/plugins/scaffolder-backend-module-gerrit/src/actions/gerrit.ts @@ -38,15 +38,17 @@ const createGerritProject = async ( parent: string; owner?: string; description: string; + defaultBranch: string; }, ): Promise => { - const { projectName, parent, owner, description } = options; + const { projectName, parent, owner, description, defaultBranch } = options; const fetchOptions: RequestInit = { method: 'PUT', body: JSON.stringify({ parent, description, + branches: [defaultBranch], owners: owner ? [owner] : [], create_empty_commit: false, }), @@ -221,6 +223,7 @@ export function createPublishGerritAction(options: { owner: owner, projectName: repo, parent: workspace, + defaultBranch, }); const auth = { username: integrationConfig.config.username!,