diff --git a/.changeset/witty-ladybugs-laugh.md b/.changeset/witty-ladybugs-laugh.md new file mode 100644 index 0000000000..d47bbb3250 --- /dev/null +++ b/.changeset/witty-ladybugs-laugh.md @@ -0,0 +1,6 @@ +--- +'@backstage/plugin-scaffolder': patch +'@backstage/plugin-scaffolder-backend': patch +--- + +Allow to create Gerrit project using default owner diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.test.ts index bb1beba791..6e817292d4 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.test.ts @@ -62,13 +62,6 @@ describe('publish:gerrit', () => { }); it('should throw an error when the repoUrl is not well formed', async () => { - await expect( - action.handler({ - ...mockContext, - input: { repoUrl: 'gerrithost.org?workspace=w&repo=repo', description }, - }), - ).rejects.toThrow(/missing owner/); - await expect( action.handler({ ...mockContext, @@ -188,6 +181,56 @@ describe('publish:gerrit', () => { ); }); + it('can correctly create a new project without specifying owner', 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({ + 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', + sourcePath: 'repository/', + }, + }); + + expect(initRepoAndPush).toHaveBeenCalledWith({ + dir: `${mockContext.workspacePath}/repository/`, + remoteUrl: 'https://gerrithost.org/a/repo', + defaultBranch: 'master', + 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/master', + ); + }); + afterEach(() => { jest.resetAllMocks(); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts index d23e860e1c..83653b3052 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts @@ -31,7 +31,7 @@ const createGerritProject = async ( options: { projectName: string; parent: string; - owner: string; + owner?: string; description: string; }, ): Promise => { @@ -42,7 +42,7 @@ const createGerritProject = async ( body: JSON.stringify({ parent, description, - owners: [owner], + owners: owner ? [owner] : [], create_empty_commit: false, }), headers: { @@ -174,11 +174,6 @@ export function createPublishGerritAction(options: { ); } - if (!owner) { - throw new InputError( - `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing owner`, - ); - } if (!workspace) { throw new InputError( `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing workspace`, diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/util.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/util.ts index 49156e7d97..fff3213cb7 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/util.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/util.ts @@ -85,7 +85,7 @@ export const parseRepoUrl = ( ); } } else { - if (!owner) { + if (!owner && type !== 'gerrit') { throw new InputError( `Invalid repo URL passed to publisher: ${repoUrl}, missing owner`, ); diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.test.tsx b/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.test.tsx index 66f0a234be..f51d00adf2 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.test.tsx +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.test.tsx @@ -18,7 +18,7 @@ import React from 'react'; import { GerritRepoPicker } from './GerritRepoPicker'; import { render, fireEvent } from '@testing-library/react'; -describe('BitbucketRepoPicker', () => { +describe('GerritRepoPicker', () => { describe('owner input field', () => { it('calls onChange when the owner input changes', () => { const onChange = jest.fn(); diff --git a/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.tsx b/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.tsx index f021948532..13af966c25 100644 --- a/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.tsx +++ b/plugins/scaffolder/src/components/fields/RepoUrlPicker/GerritRepoPicker.tsx @@ -29,18 +29,14 @@ export const GerritRepoPicker = (props: { const { workspace, owner } = state; return ( <> - 0 && !workspace} - > + 0 && !workspace}> Owner onChange({ owner: e.target.value })} value={owner} /> - The owner of the project + The owner of the project (optional)