util: isValidRef method

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-03-25 16:01:24 +01:00
parent 9b9ff70b75
commit 7fb0476dc8
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
2 changed files with 26 additions and 1 deletions

View File

@ -194,7 +194,7 @@ describe('asyncForEach', () => {
});
});
describe('isValidUrl', () => {
describe('isValidURL', () => {
test.each([
['https://github.com/docker/buildx.git', true],
['https://github.com/docker/buildx.git#refs/pull/648/head', true],
@ -207,6 +207,19 @@ describe('isValidUrl', () => {
});
});
describe('isValidRef', () => {
test.each([
['https://github.com/docker/buildx.git', true],
['https://github.com/docker/buildx.git#refs/pull/648/head', true],
['git@github.com:moby/buildkit.git', true],
['git://github.com/user/repo.git', true],
['github.com/moby/buildkit.git#main', true],
['v0.4.1', false]
])('given %p', async (url, expected) => {
expect(Util.isValidRef(url)).toEqual(expected);
});
});
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
function getInputName(name: string): string {
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;

View File

@ -75,6 +75,18 @@ export class Util {
return url.protocol === 'http:' || url.protocol === 'https:';
}
public static isValidRef(refStr: string): boolean {
if (Util.isValidURL(refStr)) {
return true;
}
for (const prefix of ['git://', 'github.com/', 'git@']) {
if (refStr.startsWith(prefix)) {
return true;
}
}
return false;
}
public static async powershellCommand(script: string, params?: Record<string, string>) {
const powershellPath: string = await io.which('powershell', true);
const escapedScript = script.replace(/'/g, "''").replace(/"|\n|\r/g, '');