From 89599a43c628b6b7004fe7aa8301b40f2597b744 Mon Sep 17 00:00:00 2001 From: Balasundaram Date: Wed, 1 Dec 2021 12:50:16 -0500 Subject: [PATCH] Adding changes for PR review comments Signed-off-by: Balasundaram --- .../builtin/publish/gitlabMergeRequest.ts | 32 +++++++------------ 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts index 19aeabefdd..0928ff3b6d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts @@ -16,20 +16,19 @@ import { createTemplateAction } from '../../createTemplateAction'; import { readFile } from 'fs-extra'; import { Gitlab } from '@gitbeaker/node'; -import path from 'path'; import globby from 'globby'; import { CommitAction } from '@gitbeaker/core/dist/types/services/Commits'; -import { CreateMergeRequestOptions } from '@gitbeaker/core/dist/types/services/MergeRequests'; import { ScmIntegrationRegistry } from '@backstage/integration'; import { InputError } from '@backstage/errors'; import { parseRepoUrl } from './util'; +import { resolveSafeChildPath } from '@backstage/backend-common'; export type GitlabMergeRequestActionInput = { projectid: string; repoUrl: string; title: string; description: string; - destinationBranch: string; + branchName: string; targetPath: string; }; @@ -39,10 +38,10 @@ export const createPublishGitlabMergeRequestAction = (options: { const { integrations } = options; return createTemplateAction({ - id: 'publish:gitlab-merge-request', + id: 'publish:gitlab:merge-request', schema: { input: { - required: ['projectid', 'repoUrl', 'targetPath'], + required: ['projectid', 'repoUrl', 'targetPath', 'branchName'], type: 'object', properties: { repoUrl: { @@ -65,7 +64,7 @@ export const createPublishGitlabMergeRequestAction = (options: { title: 'Merge Request Description', description: 'The description of the merge request', }, - destinationBranch: { + branchName: { type: 'string', title: 'Destination Branch name', description: 'The description of the merge request', @@ -98,15 +97,8 @@ export const createPublishGitlabMergeRequestAction = (options: { const integrationConfig = integrations.gitlab.byHost(host); const actions: CommitAction[] = []; - const formatedTimestamp = () => { - const d = new Date(); - const date = d.toISOString().split('T')[0]; - const time = d.toTimeString().split(' ')[0].replace(/:/g, '_'); - return `${date}_${time}`; - }; - const destinationBranch = ctx.input.destinationBranch - ? ctx.input.destinationBranch + formatedTimestamp() - : `backstage_${formatedTimestamp()}`; + + const destinationBranch = ctx.input.branchName; if (!integrationConfig) { throw new InputError( @@ -124,14 +116,14 @@ export const createPublishGitlabMergeRequestAction = (options: { }); const fileRoot = ctx.workspacePath; - const localFilePaths = await globby([`${ctx.input.targetPath}'/**'`], { + const localFilePaths = await globby([`${ctx.input.targetPath}/**`], { cwd: fileRoot, gitignore: true, dot: true, }); const fileContents = await Promise.all( - localFilePaths.map(p => readFile(path.resolve(fileRoot, p))), + localFilePaths.map(p => readFile(resolveSafeChildPath(fileRoot, p))), ); const repoFilePaths = localFilePaths.map(repoFilePath => { @@ -146,11 +138,9 @@ export const createPublishGitlabMergeRequestAction = (options: { }); } - const defaultBranch: any = await api.Projects.show( + const { default_branch: defaultBranch }: any = await api.Projects.show( ctx.input.projectid, - ).then(projectJSON => { - return projectJSON?.default_branch; - }); + ); try { await api.Branches.create(