From 4944dcf0a7ee7e423c657d426edc7bcd6e67f9bb Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 27 Jan 2021 11:43:20 +0100 Subject: [PATCH] joinGitUrlPath Co-authored-by: Patrik Oldsberg --- .../src/scaffolder/stages/helpers.ts | 19 +++++++++++++++++++ .../scaffolder-backend/src/service/router.ts | 9 ++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/helpers.ts index f8d8229298..784e72ca87 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/helpers.ts @@ -13,6 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +import { posix as posixPath } from 'path'; import { TemplateEntityV1alpha1, LOCATION_ANNOTATION, @@ -53,3 +55,20 @@ export const parseLocationAnnotation = ( location, }; }; + +export function joinGitUrlPath(repoUrl: string, path?: string): string { + const parsed = new URL(repoUrl); + + if (parsed.hostname.endsWith('azure.com')) { + const templatePath = posixPath.normalize( + posixPath.join( + posixPath.dirname(parsed.searchParams.get('path') || '/'), + path || '.', + ), + ); + parsed.searchParams.set('path', templatePath); + return parsed.toString(); + } + + return new URL(path || '.', repoUrl).toString().replace(/\/$/, ''); +} diff --git a/plugins/scaffolder-backend/src/service/router.ts b/plugins/scaffolder-backend/src/service/router.ts index a3e7202f95..9098e5d05e 100644 --- a/plugins/scaffolder-backend/src/service/router.ts +++ b/plugins/scaffolder-backend/src/service/router.ts @@ -27,6 +27,7 @@ import { TemplaterValues, PublisherBuilder, parseLocationAnnotation, + joinGitUrlPath, FilePreparer, } from '../scaffolder'; import { CatalogEntityClient } from '../lib/catalog'; @@ -138,12 +139,10 @@ export async function createRouter( const preparer = preparers.get(templateEntityLocation); - const url = new URL( - template.spec.path || '.', + const url = joinGitUrlPath( templateEntityLocation, - ) - .toString() - .replace(/\/$/, ''); + template.spec.path, + ); await preparer.prepare({ url,