diff --git a/.changeset/sweet-rats-invite.md b/.changeset/sweet-rats-invite.md new file mode 100644 index 0000000000..f292e7bb43 --- /dev/null +++ b/.changeset/sweet-rats-invite.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Add sourcePath option to publish:gerrit action diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index dfca5994fd..4e09c66755 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -316,6 +316,7 @@ export function createPublishGerritAction(options: { gitCommitMessage?: string | undefined; gitAuthorName?: string | undefined; gitAuthorEmail?: string | undefined; + sourcePath?: string | undefined; }>; // @public 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 0b1ffe0de6..bb1beba791 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 @@ -137,6 +137,57 @@ describe('publish:gerrit', () => { 'https://gerrithost.org/repo/+/refs/heads/master', ); }); + + it('can correctly create a new project with a specific sourcePath', 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: ['owner'], + 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&owner=owner&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 51355ec357..d23e860e1c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gerrit.ts @@ -92,6 +92,7 @@ export function createPublishGerritAction(options: { gitCommitMessage?: string; gitAuthorName?: string; gitAuthorEmail?: string; + sourcePath?: string; }>({ id: 'publish:gerrit', description: @@ -129,6 +130,11 @@ export function createPublishGerritAction(options: { type: 'string', description: `Sets the default author email for the commit.`, }, + sourcePath: { + title: 'Source Path', + type: 'string', + description: `Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.`, + }, }, }, output: { @@ -153,6 +159,7 @@ export function createPublishGerritAction(options: { gitAuthorName, gitAuthorEmail, gitCommitMessage = 'initial commit', + sourcePath, } = ctx.input; const { repo, host, owner, workspace } = parseRepoUrl( repoUrl, @@ -199,7 +206,7 @@ export function createPublishGerritAction(options: { const remoteUrl = `${integrationConfig.config.cloneUrl}/a/${repo}`; await initRepoAndPush({ - dir: getRepoSourceDirectory(ctx.workspacePath, undefined), + dir: getRepoSourceDirectory(ctx.workspacePath, sourcePath), remoteUrl, auth, defaultBranch,