Skip to content

Commit

Permalink
Merge pull request #40 from vijaykramesh/require_brackets
Browse files Browse the repository at this point in the history
add require_brackets config
  • Loading branch information
Vijay Ramesh authored Sep 21, 2023
2 parents 12799fd + be20375 commit e85a542
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ check_title: true
check_branch: true
check_commits: true
ignore_case: true
require_brackets: true
```
## Local Development
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions fixtures/no-brackets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
projects: ['PROJ', 'ABC']
check_title: true
check_branch: false
ignore_case: true
require_brackets: false
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const defaults = {
check_branch: false,
check_commits: false,
ignore_case: false,
require_brackets: true,
};

function createProjectRegex(project, ignoreCase = false) {
Expand All @@ -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(
Expand Down Expand Up @@ -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;
}
Expand Down
28 changes: 28 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' } },
Expand Down Expand Up @@ -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);
});
});

0 comments on commit e85a542

Please sign in to comment.