mirror of
https://github.com/docker/actions-toolkit.git
synced 2024-11-23 03:16:09 +08:00
util: isValidRef method
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
parent
9b9ff70b75
commit
7fb0476dc8
@ -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()}`;
|
||||
|
12
src/util.ts
12
src/util.ts
@ -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, '');
|
||||
|
Loading…
Reference in New Issue
Block a user