diff --git a/packages/resource-utils/package.json b/packages/resource-utils/package.json index 5c7263c0..5e71642e 100644 --- a/packages/resource-utils/package.json +++ b/packages/resource-utils/package.json @@ -2,7 +2,7 @@ "name": "@dolthub/proto-resource-utils", "author": "DoltHub", "description": "Utilities for handling Google protobuf resources and names", - "version": "0.1.2", + "version": "0.1.3", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", "types": "dist/index.d.ts", diff --git a/packages/resource-utils/src/__tests__/helpers/branchNames.ts b/packages/resource-utils/src/__tests__/helpers/branchNames.ts index d65539d8..0e84a450 100644 --- a/packages/resource-utils/src/__tests__/helpers/branchNames.ts +++ b/packages/resource-utils/src/__tests__/helpers/branchNames.ts @@ -6,20 +6,14 @@ export const validBranchNames = [ "user/{/a.tt/}", ]; -export const invalidBranchNames = [ +export const invalidBranchProtectionNames = [ "", - "this-is-a-..-test", "this-is-a-@{-test", "this-is-a- -test", "this-is-a-\t-test", "this-is-a-//-test", "this-is-a-:-test", - "this-is-a-?-test", - "this-is-a-[-test", - "this-is-a-\\-test", - "this-is-a-^-test", "this-is-a-~-test", - "this-is-a-*-test", "this-is-a-\x00-test", "this-is-a-\x01-test", "this-is-a-\x02-test", @@ -63,3 +57,29 @@ export const invalidBranchNames = [ "user.lock/working/mybranch", "-", ]; + +export const invalidBranchNames = [ + ...invalidBranchProtectionNames, + "this-is-a-..-test", + "this-is-a-?-test", + "this-is-a-[-test", + "this-is-a-\\-test", + "this-is-a-^-test", + "this-is-a-*-test", +]; + +export const validBranchProtectionNames = [ + ...validBranchNames, + "foo*", + "qa/**/*", + "c{at,ub}s", + "c?t", + "c??t", + "c*", + "c*t", + "ca[a-z]", + "ca[^t]", + "?", + "*", + "[\\?]", +]; diff --git a/packages/resource-utils/src/__tests__/validResourceSegmentPattern.test.ts b/packages/resource-utils/src/__tests__/validResourceSegmentPattern.test.ts index d1f2d774..e597c686 100644 --- a/packages/resource-utils/src/__tests__/validResourceSegmentPattern.test.ts +++ b/packages/resource-utils/src/__tests__/validResourceSegmentPattern.test.ts @@ -1,7 +1,18 @@ -import { validBranchNamePattern } from "../validResourceSegmentPattern"; -import { invalidBranchNames, validBranchNames } from "./helpers/branchNames"; +import { + validBranchNamePattern, + validBranchProtectionNamePattern, +} from "../validResourceSegmentPattern"; +import { + invalidBranchNames, + validBranchNames, + validBranchProtectionNames, + invalidBranchProtectionNames, +} from "./helpers/branchNames"; const validBranchNameRegex = new RegExp(validBranchNamePattern); +const validBranchProtectionNameRegex = new RegExp( + validBranchProtectionNamePattern, +); test("validBranchNamePattern", () => { validBranchNames.forEach(bn => expect(bn).toMatch(validBranchNameRegex)); @@ -9,3 +20,12 @@ test("validBranchNamePattern", () => { expect(bn).not.toMatch(validBranchNameRegex), ); }); + +test("validBranchProtectionNamePattern", () => { + validBranchProtectionNames.forEach(bn => + expect(bn).toMatch(validBranchProtectionNameRegex), + ); + invalidBranchProtectionNames.forEach(bn => + expect(bn).not.toMatch(validBranchProtectionNameRegex), + ); +}); diff --git a/packages/resource-utils/src/index.ts b/packages/resource-utils/src/index.ts index df2ac650..f94a0b37 100644 --- a/packages/resource-utils/src/index.ts +++ b/packages/resource-utils/src/index.ts @@ -22,5 +22,6 @@ export { export * as validResourceSegmentPattern from "./validResourceSegmentPattern"; export { invalidBranchNames, + invalidBranchProtectionNames, validBranchNames, } from "./__tests__/helpers/branchNames"; diff --git a/packages/resource-utils/src/validResourceSegmentPattern.ts b/packages/resource-utils/src/validResourceSegmentPattern.ts index c2ad0571..6fe9454b 100644 --- a/packages/resource-utils/src/validResourceSegmentPattern.ts +++ b/packages/resource-utils/src/validResourceSegmentPattern.ts @@ -8,40 +8,41 @@ const validLongResourcePattern = "[-a-zA-Z0-9_]{3,36}"; const validUuidPattern = "[a-f0-9-]{36}"; const validBackupIdPattern = "[0-9]{8}T[0-9]{6}.[0-9]{3}"; -const invalidBranchNamePattern = [ - // Any appearance of the following characters: :, ?, [, \, ^, ~, SPACE, TAB, * +// Removed ?,^, [, \, * from invalidBranchNamePattern as they are allowed in fnmatch +const invalidBranchProtectionNamePattern = [ `:`, - `\\?`, - `\\[`, - `\\\\`, - `\\^`, `~`, ` `, `\t`, - `\\*`, - // Any ASCII control character. `[\x00-\x1f]`, `\x7f`, - // Any component starting with a "." `^\\.`, `/\\.`, - // Any component ending with ".lock" `\\.lock$`, `\\.lock/`, - // An exact name of "" or "-" `^$`, `^-$`, - // Any appearance of ".." or "@{" `\\.\\.`, `@{`, - // Any empty component; that is, starting or ending with "/" or any appearance of "//" `//`, `^/`, `/$`, ].join("|"); +const invalidBranchNamePattern = [ + `\\?`, + `\\^`, + `\\[`, + `\\\\`, + `\\*`, + invalidBranchProtectionNamePattern, +].join("|"); + const invertRegexPattern = (p: string) => `^(?!.*(${p}))`; const validBranchNamePattern = invertRegexPattern(invalidBranchNamePattern); +const validBranchProtectionNamePattern = invertRegexPattern( + invalidBranchProtectionNamePattern, +); export { validBackupIdPattern, @@ -53,4 +54,5 @@ export { validNumberPattern, validResourceSegmentPattern, validUuidPattern, + validBranchProtectionNamePattern, };