diff --git a/lib/branch.js b/lib/branch.js index ff04b94e..63bc100e 100644 --- a/lib/branch.js +++ b/lib/branch.js @@ -1,20 +1,3 @@ -exports.isBranchNameValid = (branchName) => - isBranchNameValid(branchName) || isBranchNameValidLegacy(branchName); - -function isBranchNameValidLegacy(branchName) { - return ( - (!!branchName.match( - /^(core|feature|fix|hotfix|asset|rework|documentation|mobsuccessbot|dependabot)\/([a-z][a-z0-9._-]*)$/ - ) || - !!branchName.match(/^(mobsuccessbot)\/([a-z0-9_\-@./_-]*)$/) || - !!branchName.match(/^(dependabot)\/([a-z][a-zA-Z0-9./_-]*)$/) || - !!branchName.match(/^revert-[0-9]+-/) || - !!branchName.match(/^(rework)\/([A-Z][a-zA-Z0-9.-]*)$/)) && - !branchName.match(/---/) && - !branchName.match(/\/\//) - ); -} - function isBranchNameValid(branchName) { return ( !!branchName.match( @@ -24,3 +7,7 @@ function isBranchNameValid(branchName) { !!branchName.match(/^(dependabot)\/([a-z][a-zA-Z0-9./_-]*)$/) ); } + +module.exports = { + isBranchNameValid, +}; diff --git a/lib/branch.test.js b/lib/branch.test.js index 72156295..2c45487d 100644 --- a/lib/branch.test.js +++ b/lib/branch.test.js @@ -1,36 +1,42 @@ const { isBranchNameValid } = require("./branch"); describe("branch", () => { - test("recognize valid branch names", () => { - expect(isBranchNameValid("core/foo")).toBe(true); - expect(isBranchNameValid("feature/foo")).toBe(true); - expect(isBranchNameValid("fix/foo")).toBe(true); - expect(isBranchNameValid("hotfix/foo")).toBe(true); - expect(isBranchNameValid("asset/foo")).toBe(true); - expect(isBranchNameValid("rework/foo")).toBe(true); - expect(isBranchNameValid("rework/DriveFooterBar")).toBe(true); - expect(isBranchNameValid("documentation/foo")).toBe(true); - expect(isBranchNameValid("dependabot/foo")).toBe(true); - expect(isBranchNameValid("dependabot/npm_and_yarn/axios-0.21.1")).toBe( - true - ); - expect(isBranchNameValid("mobsuccessbot/foo")).toBe(true); - expect(isBranchNameValid("mobsuccessbot/foo@foo")).toBe(true); - expect(isBranchNameValid("mobsuccessbot/foo@foo/foo")).toBe(true); - expect(isBranchNameValid("foo/foo-bar")).toBe(false); - expect(isBranchNameValid("core/foo--bar")).toBe(true); - expect(isBranchNameValid("core/foo--bar--z")).toBe(true); - expect(isBranchNameValid("feature/branch-name_from_linear")).toBe(true); + test.each([ + "feat/foo", + "fix/foo", + "refactor/foo", + "docs/foo", + "chore/foo--bar", + "chore/foo--bar--z", + "feat/branch-name_from_linear", + "mobsuccessbot/foo", + "mobsuccessbot/foo@foo", + "mobsuccessbot/foo@foo/foo", + "dependabot/foo", + "dependabot/npm_and_yarn/axios-0.21.1", + "dependabot/npm_and_yarn/axios-0.21.1", + ])("Test branch %s should be valid", (branch) => { + expect(isBranchNameValid(branch)).toBe(true); }); - test("recognize invalid branch names", () => { - expect(isBranchNameValid("core/npm_and_yarn/axios-0.21.1")).toBe(false); - expect(isBranchNameValid("core")).toBe(false); - expect(isBranchNameValid("core/")).toBe(false); - expect(isBranchNameValid("core/FOO")).toBe(false); - expect(isBranchNameValid("foo/foo")).toBe(false); - expect(isBranchNameValid("foo/foo/bar")).toBe(false); - expect(isBranchNameValid("fix/DriveFooterBar")).toBe(false); - expect(isBranchNameValid("rework/fooDriveFooterBar")).toBe(false); - expect(isBranchNameValid("core//foo")).toBe(false); + test.each([ + "core/foo--bar", + "core/foo--bar--z", + "foo/foo-bar", + "feature/foo", + "hotfix/foo", + "core/foo", + "asset/foo", + "documentation/foo", + "chore", + "chore/", + "chore/FOO", + "foo/foo", + "foo/foo/bar", + "fix/DriveFooterBar", + "refactor/DriveFooterBar", + "rework/fooDriveFooterBar", + "chore//foo", + ])("Test branch %s should be invalid", (branch) => { + expect(isBranchNameValid(branch)).toBe(false); }); }); diff --git a/lib/pullRequest.js b/lib/pullRequest.js index 6f92cdde..c59c8273 100644 --- a/lib/pullRequest.js +++ b/lib/pullRequest.js @@ -1,21 +1,9 @@ -exports.isPullRequestTitleValid = (pullRequestName) => - isPullRequestTitleValid(pullRequestName) || - isPullRequestTitleValidLegacy(pullRequestName); - -function isPullRequestTitleValidLegacy(pullRequestName) { - return ( - !!pullRequestName.match( - /^(Add|Clean|Fix|Improve|Remove|Update|Rework|Ignore|Bump|Switch|Migrate) .+$/ - /* remember to edit the Notion when changing this value: - https://www.notion.so/mobsuccess/Git-Guidelines-41996ef576cb4f29b7737772b74289c5 - */ - ) || !!pullRequestName.match(/^Revert ".*"$/) - ); -} - -// commit convention: prefix([optional scope]): description function isPullRequestTitleValid(pullRequestName) { return !!pullRequestName.match( /^(feat|fix|chore|docs|refactor|test|revert|build|ci|perf|style|change|remove|poc)(?:\(.+\))?!?:.+/ ); } + +module.exports = { + isPullRequestTitleValid, +}; diff --git a/lib/pullRequest.test.js b/lib/pullRequest.test.js index 427780b2..8c64fa96 100644 --- a/lib/pullRequest.test.js +++ b/lib/pullRequest.test.js @@ -1,34 +1,23 @@ const { isPullRequestTitleValid } = require("./pullRequest"); describe("pull request", () => { - test("recognize pull request branch names", () => { - expect(isPullRequestTitleValid("Add feature")).toBe(true); - expect(isPullRequestTitleValid("chore: remove base files")).toBe(true); - expect( - isPullRequestTitleValid( - "fix(design-system): update color palette for accessibility compliance" - ) - ).toBe(true); - expect( - isPullRequestTitleValid( - "feat!: send an email to the customer when a product is shipped" - ) - ).toBe(true); - expect( - isPullRequestTitleValid( - "feat(api)!: send an email to the customer when a product is shipped" - ) - ).toBe(true); + test.each([ + "chore: remove base files", + "fix(design-system): update color palette for accessibility compliance", + "feat!: send an email to the customer when a product is shipped", + "feat(api)!: send an email to the customer when a product is shipped", + ])("Test pull request title %s should be valid", (title) => { + expect(isPullRequestTitleValid(title)).toBe(true); }); - test("recognize invalid branch names", () => { - expect(isPullRequestTitleValid("add feature")).toBe(false); - expect(isPullRequestTitleValid("chore remove base files")).toBe(false); - expect(isPullRequestTitleValid("chore(): remove base files")).toBe(false); - expect(isPullRequestTitleValid("add: add new project")).toBe(false); - expect( - isPullRequestTitleValid( - "feat!(api): send an email to the customer when a product is shipped" - ) - ).toBe(false); + test.each([ + "Add feature", + "Remove feature", + "add feature", + "chore remove base files", + "chore(): remove base files", + "add: add new project", + "feat!(api): send an email to the customer when a product is shipped", + ])("Test pull request title %s should be invalid", (title) => { + expect(isPullRequestTitleValid(title)).toBe(false); }); });