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!,