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 <martinwa@axis.com>
This commit is contained in:
Martin Wallgren
2024-01-31 10:06:30 +01:00
parent a5cd12d428
commit 8b269e1879
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!,