Merge pull request #22628 from MartinWallgren/gerrit-project

Scaffolder-backend-gerrit: Set branches to defaultBranch
This commit is contained in:
Ben Lambert
2024-01-31 16:24:27 +01:00
committed by GitHub
3 changed files with 64 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-gerrit': patch
---
Provide default branch when creating repositories.
@@ -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,
@@ -38,15 +38,17 @@ const createGerritProject = async (
parent: string;
owner?: string;
description: string;
defaultBranch: string;
},
): Promise<void> => {
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!,