From 7fb0476dc8c506427c25a0b21d751ab44357bf8c Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 25 Mar 2023 16:01:24 +0100 Subject: [PATCH] util: isValidRef method Signed-off-by: CrazyMax --- __tests__/util.test.ts | 15 ++++++++++++++- src/util.ts | 12 ++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index bda42d7..1dcac75 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -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()}`; diff --git a/src/util.ts b/src/util.ts index 1c4766b..7d1052f 100644 --- a/src/util.ts +++ b/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) { const powershellPath: string = await io.which('powershell', true); const escapedScript = script.replace(/'/g, "''").replace(/"|\n|\r/g, '');