diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts index 0141de3499..e1a064b3e6 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/azure.ts @@ -19,7 +19,7 @@ import { ScmIntegrationRegistry } from '@backstage/integration'; import { initRepoAndPush } from '../../../stages/publish/helpers'; import { GitRepositoryCreateOptions } from 'azure-devops-node-api/interfaces/GitInterfaces'; import { getPersonalAccessTokenHandler, WebApi } from 'azure-devops-node-api'; -import { parseRepoUrl } from './util'; +import { getRepoSourceDirectory, parseRepoUrl } from './util'; import { createTemplateAction } from '../../createTemplateAction'; export function createPublishAzureAction(options: { @@ -30,6 +30,7 @@ export function createPublishAzureAction(options: { return createTemplateAction<{ repoUrl: string; description?: string; + sourcePath?: string; }>({ id: 'publish:azure', description: @@ -47,6 +48,11 @@ export function createPublishAzureAction(options: { title: 'Repository Description', type: 'string', }, + sourcePath: { + title: + 'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the respository.', + type: 'string', + }, }, }, output: { @@ -112,7 +118,7 @@ export function createPublishAzureAction(options: { const repoContentsUrl = remoteUrl; await initRepoAndPush({ - dir: ctx.workspacePath, + dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl, auth: { username: 'notempty', diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts index a7370356cc..13f639d745 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/bitbucket.ts @@ -20,7 +20,7 @@ import { ScmIntegrationRegistry, } from '@backstage/integration'; import { initRepoAndPush } from '../../../stages/publish/helpers'; -import { parseRepoUrl } from './util'; +import { getRepoSourceDirectory, parseRepoUrl } from './util'; import fetch from 'cross-fetch'; import { createTemplateAction } from '../../createTemplateAction'; @@ -165,6 +165,7 @@ export function createPublishBitbucketAction(options: { repoUrl: string; description: string; repoVisibility: 'private' | 'public'; + sourcePath?: string; }>({ id: 'publish:bitbucket', description: @@ -187,6 +188,11 @@ export function createPublishBitbucketAction(options: { type: 'string', enum: ['private', 'public'], }, + sourcePath: { + title: + 'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the respository.', + type: 'string', + }, }, }, output: { @@ -233,7 +239,7 @@ export function createPublishBitbucketAction(options: { }); await initRepoAndPush({ - dir: ctx.workspacePath, + dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl, auth: { username: integrationConfig.config.username diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts index ca1b0f3d82..666c04f368 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/github.ts @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { join as joinPath, normalize as normalizePath } from 'path'; import { InputError } from '@backstage/errors'; import { GithubCredentialsProvider, @@ -21,7 +20,7 @@ import { } from '@backstage/integration'; import { Octokit } from '@octokit/rest'; import { initRepoAndPush } from '../../../stages/publish/helpers'; -import { parseRepoUrl } from './util'; +import { getRepoSourceDirectory, parseRepoUrl } from './util'; import { createTemplateAction } from '../../createTemplateAction'; export function createPublishGithubAction(options: { @@ -69,7 +68,8 @@ export function createPublishGithubAction(options: { enum: ['private', 'public', 'internal'], }, sourcePath: { - title: 'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the respository.', + title: + 'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the respository.', type: 'string', }, }, @@ -163,14 +163,9 @@ export function createPublishGithubAction(options: { const remoteUrl = data.clone_url; const repoContentsUrl = `${data.html_url}/blob/master`; - let outputPath = ctx.input.sourcePath; - if (ctx.input.sourcePath) { - const safeSuffix = normalizePath(ctx.input.sourcePath).replace(/^(\.\.(\/|\\|$))+/, ''); - outputPath = joinPath(ctx.workspace, safeSuffix); - } await initRepoAndPush({ - dir: outputPath, + dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl, auth: { username: 'x-access-token', diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts index 12fc90cc94..37074d6f57 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlab.ts @@ -18,7 +18,7 @@ import { InputError } from '@backstage/errors'; import { ScmIntegrationRegistry } from '@backstage/integration'; import { Gitlab } from '@gitbeaker/node'; import { initRepoAndPush } from '../../../stages/publish/helpers'; -import { parseRepoUrl } from './util'; +import { getRepoSourceDirectory, parseRepoUrl } from './util'; import { createTemplateAction } from '../../createTemplateAction'; export function createPublishGitlabAction(options: { @@ -29,6 +29,7 @@ export function createPublishGitlabAction(options: { return createTemplateAction<{ repoUrl: string; repoVisibility: 'private' | 'internal' | 'public'; + sourcePath?: string; }>({ id: 'publish:gitlab', description: @@ -47,6 +48,11 @@ export function createPublishGitlabAction(options: { type: 'string', enum: ['private', 'public', 'internal'], }, + sourcePath: { + title: + 'Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the respository.', + type: 'string', + }, }, }, output: { @@ -106,7 +112,7 @@ export function createPublishGitlabAction(options: { const repoContentsUrl = `${remoteUrl}/-/blob/master`; await initRepoAndPush({ - dir: ctx.workspacePath, + dir: getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath), remoteUrl: http_url_to_repo as string, auth: { username: 'oauth2', 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 89bbf24574..07b3823e7b 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/util.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/util.ts @@ -15,6 +15,21 @@ */ import { InputError } from '@backstage/errors'; +import { join as joinPath, normalize as normalizePath } from 'path'; + +export const getRepoSourceDirectory = ( + workspacePath: string, + sourcePath: string | undefined, +) => { + if (sourcePath) { + const safeSuffix = normalizePath(sourcePath).replace( + /^(\.\.(\/|\\|$))+/, + '', + ); + return joinPath(workspacePath, safeSuffix); + } + return workspacePath; +}; export const parseRepoUrl = (repoUrl: string) => { let parsed;