trim leading and trailing slashes from parseRepoUrl query parameters

Signed-off-by: Matt Benson <gudnabrsam@gmail.com>
This commit is contained in:
Matt Benson
2025-05-08 16:25:20 -05:00
parent a51855db73
commit 16e2e9c1db
5 changed files with 45 additions and 10 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-node': patch
---
trim leading and trailing slashes from parseRepoUrl query parameters
+3 -1
View File
@@ -66,6 +66,7 @@
"globby": "^11.0.0",
"isomorphic-git": "^1.23.0",
"jsonschema": "^1.5.0",
"lodash": "^4.17.21",
"p-limit": "^3.1.0",
"tar": "^6.1.12",
"winston": "^3.2.1",
@@ -76,6 +77,7 @@
"devDependencies": {
"@backstage/backend-test-utils": "workspace:^",
"@backstage/cli": "workspace:^",
"@backstage/config": "workspace:^"
"@backstage/config": "workspace:^",
"@types/lodash": "^4.14.151"
}
}
@@ -15,6 +15,7 @@
*/
import { ScmIntegrationRegistry } from '@backstage/integration';
import { parseRepoUrl } from './util';
import { mapValues } from 'lodash';
const queryString = (
params: Partial<
@@ -217,6 +218,30 @@ describe('scaffolder action utils', () => {
parseRepoUrl(`${host}${queryString({ owner, repo })}`, integrations),
).toMatchObject({ host, owner, repo });
});
it('trims leading and trailing / from params', () => {
const [host, owner, organization, workspace, project, repo] = [
'anywhere',
'anyone',
'anything',
'anyway',
'anyhow',
'any',
];
const junkedUp = mapValues(
{ owner, organization, workspace, project, repo },
v => `//${v}//`,
);
return expect(
parseRepoUrl(`${host}${queryString(junkedUp)}`, integrations),
).toMatchObject({
host,
owner,
organization,
workspace,
project,
repo,
});
});
});
});
});
+10 -9
View File
@@ -22,6 +22,7 @@ import { TemplateActionOptions } from './createTemplateAction';
import zodToJsonSchema from 'zod-to-json-schema';
import { z } from 'zod';
import { Schema } from 'jsonschema';
import { trim } from 'lodash';
/**
* @public
@@ -67,11 +68,6 @@ export const parseRepoUrl = (
);
}
const host = parsed.host;
const owner = parsed.searchParams.get('owner') ?? undefined;
const organization = parsed.searchParams.get('organization') ?? undefined;
const workspace = parsed.searchParams.get('workspace') ?? undefined;
const project = parsed.searchParams.get('project') ?? undefined;
const type = integrations.byHost(host)?.type;
if (!type) {
@@ -79,8 +75,14 @@ export const parseRepoUrl = (
`No matching integration configuration for host ${host}, please check your integrations config`,
);
}
const repo: string = parsed.searchParams.get('repo')!;
const { owner, organization, workspace, project, repo } = Object.fromEntries(
['owner', 'organization', 'workspace', 'project', 'repo'].map(param => [
param,
parsed.searchParams.has(param)
? trim(parsed.searchParams.get(param)!, '/')
: undefined,
]),
);
switch (type) {
case 'bitbucket': {
if (host === 'www.bitbucket.org') {
@@ -113,8 +115,7 @@ export const parseRepoUrl = (
break;
}
}
return { host, owner, repo, organization, workspace, project };
return { host, owner, repo: repo!, organization, workspace, project };
};
function checkRequiredParams(repoUrl: URL, ...params: string[]) {
+2
View File
@@ -8020,11 +8020,13 @@ __metadata:
"@backstage/plugin-scaffolder-common": "workspace:^"
"@backstage/types": "workspace:^"
"@isomorphic-git/pgp-plugin": "npm:^0.0.7"
"@types/lodash": "npm:^4.14.151"
concat-stream: "npm:^2.0.0"
fs-extra: "npm:^11.2.0"
globby: "npm:^11.0.0"
isomorphic-git: "npm:^1.23.0"
jsonschema: "npm:^1.5.0"
lodash: "npm:^4.17.21"
p-limit: "npm:^3.1.0"
tar: "npm:^6.1.12"
winston: "npm:^3.2.1"