Add sourcePath option to all git publishers

Signed-off-by: Jonah Back <jonah@jonahback.com>
This commit is contained in:
Jonah Back
2021-03-24 21:20:58 -07:00
parent d3accca860
commit 8419d669c7
5 changed files with 43 additions and 15 deletions
@@ -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',
@@ -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
@@ -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',
@@ -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',
@@ -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;