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,