diff --git a/README.md b/README.md index 74e3b9a..1a30006 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ check_title: true check_branch: true check_commits: true ignore_case: true +require_brackets: true ``` ## Local Development @@ -67,6 +68,8 @@ Run `yarn test` to test: ✓ passes if ignore_case and lower case title/branch (6 ms) ✓ passes if ignore_case and lower case commits (7 ms) ✓ fails if not ignore_case and lower case title/branch (4 ms) + ✓ passes if require_brakcets is false and title matches without brackets (5 ms) + ✓ fails if require_brackets is true or default and title matches without brackets (4 ms) ``` ## Lint diff --git a/fixtures/no-brackets.yml b/fixtures/no-brackets.yml new file mode 100644 index 0000000..f86face --- /dev/null +++ b/fixtures/no-brackets.yml @@ -0,0 +1,5 @@ +projects: ['PROJ', 'ABC'] +check_title: true +check_branch: false +ignore_case: true +require_brackets: false \ No newline at end of file diff --git a/index.js b/index.js index 393d069..eb13e39 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,7 @@ const defaults = { check_branch: false, check_commits: false, ignore_case: false, + require_brackets: true, }; function createProjectRegex(project, ignoreCase = false) { @@ -28,8 +29,11 @@ function findFailedCommits(projects, commitsInPR, ignoreCase) { return failedCommits; } -function createWrappedProjectRegex(project) { - return new RegExp(`\\[${project}-\\d*\\]`); +function createWrappedProjectRegex(project, requireBrackets = false) { + if (requireBrackets) { + return new RegExp(`\\[${project}-\\d*\\]`); + } + return new RegExp(`${project}[-_]\\d*`); } Toolkit.run( @@ -59,7 +63,7 @@ Toolkit.run( const title_passed = (() => { if (config.check_title) { // check the title matches [PROJECT-1234] somewhere - if (!projects.some((project) => title.match(createWrappedProjectRegex(project)))) { + if (!projects.some((project) => title.match(createWrappedProjectRegex(project, config.require_brackets)))) { tools.log(`PR title ${title} does not contain approved project with format [PROJECT-1234]`); return false; } diff --git a/index.test.js b/index.test.js index ada26c0..74d879c 100644 --- a/index.test.js +++ b/index.test.js @@ -75,6 +75,8 @@ describe('pr-lint-action', () => { const good_title_and_bad_branch = { title: '[PROJ-1234] a good PR title', ref_name: 'fix_things' }; const bad_title_and_good_branch = { title: 'no ticket in me', ref_name: 'bug/PROJ_1234/a_good_branch' }; const lower_case_good_title_and_branch = { title: '[proj-1234] a lower case good title', ref_name: 'bug/proj_1234/a_good_lowercase_branch' }; + const no_brackets_title_and_branch = { title: 'PROJ-1234 a good no brackets PR title', ref_name: 'bug/PROJ-1234/a_good_branch' }; + const good_commits = [ { commit: { message: 'PROJ-1234 Commit 1' } }, { commit: { message: 'PROJ-1234 Commit 2' } }, @@ -339,4 +341,30 @@ describe('pr-lint-action', () => { expect(tools.exit.failure).toHaveBeenCalledWith('PR Linting Failed'); expect.assertions(1); }); + + it('passes if require_brackets is false and title matches without brackets', async () => { + nock('https://api.github.com') + .get(/\/repos\/vijaykramesh\/.*/) + .query(true) + .reply(200, configFixture('no-brackets.yml')); + + tools.context.payload = pullRequestOpenedFixture(no_brackets_title_and_branch); + + await action(tools); + expect(tools.exit.success).toHaveBeenCalled(); + expect.assertions(1); + }); + + it('fails if require_brackets is true or default and title matches without brackets', async () => { + nock('https://api.github.com') + .get(/\/repos\/vijaykramesh\/.*/) + .query(true) + .reply(200, configFixture('title.yml')); + + tools.context.payload = pullRequestOpenedFixture(no_brackets_title_and_branch); + + await action(tools); + expect(tools.exit.failure).toHaveBeenCalledWith('PR Linting Failed'); + expect.assertions(1); + }); });