Skip to content

Liuliu/resource-utils: add branch protection name pattern #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/resource-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 27 additions & 7 deletions packages/resource-utils/src/__tests__/helpers/branchNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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]",
"?",
"*",
"[\\?]",
];
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
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));
invalidBranchNames.forEach(bn =>
expect(bn).not.toMatch(validBranchNameRegex),
);
});

test("validBranchProtectionNamePattern", () => {
validBranchProtectionNames.forEach(bn =>
expect(bn).toMatch(validBranchProtectionNameRegex),
);
invalidBranchProtectionNames.forEach(bn =>
expect(bn).not.toMatch(validBranchProtectionNameRegex),
);
});
1 change: 1 addition & 0 deletions packages/resource-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ export {
export * as validResourceSegmentPattern from "./validResourceSegmentPattern";
export {
invalidBranchNames,
invalidBranchProtectionNames,
validBranchNames,
} from "./__tests__/helpers/branchNames";
28 changes: 15 additions & 13 deletions packages/resource-utils/src/validResourceSegmentPattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these all in invalidBranchNamePattern too? Can spread this array in there so it's clearer what the differences are

`:`,
`\\?`,
`\\[`,
`\\\\`,
`\\^`,
`~`,
` `,
`\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,
Expand All @@ -53,4 +54,5 @@ export {
validNumberPattern,
validResourceSegmentPattern,
validUuidPattern,
validBranchProtectionNamePattern,
};
Loading