Skip to content

Commit

Permalink
fix: allow only supported delimiter characters between keyword and is…
Browse files Browse the repository at this point in the history
…sue ref
  • Loading branch information
pvdlg committed Nov 24, 2018
1 parent 5959468 commit 55c5661
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ FIX #1
{actions: {close: [{raw: 'FIX #1', action: 'Fix', slug: undefined, prefix: '#', issue: '1'}]}}
```

### Support delimiters between action keyword and issue

```text
Fix: #1
```
```js
{actions: {close: [{raw: 'Fix: #1', action: 'Fix', slug: undefined, prefix: '#', issue: '1'}]}}
```

### Ignore references in back-tick quotes

```text
Expand Down Expand Up @@ -342,6 +351,13 @@ Object with type of action as key and array of keywords as value.

Each keyword match will be placed in the corresponding property of the [`result`](#result) `action` object. For example the with the configuration `{actions: fix: ['fixed', 'fixing']}` each action matching `fixed` or `fixing` will be under `result.actions.fix`.

##### delimiters

Type: `Array<String>` `String`<br>
Default: `[':']`

List of delimiter characters allowed between an action keywords and the issue reference. The characters space (` `) and tab (` `) are always allowed.

##### mentionsPrefixes

Type: `Array<String>` `String`<br>
Expand Down
16 changes: 7 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ function buildMentionsRegexp({mentionsPrefixes}) {
return `((?:(?:[^\\w\\n\\v\\r]|^)+(?:${join(mentionsPrefixes)})[\\w-\\.]+[^\\W])+)`;
}

function buildRefRegexp({actions, issuePrefixes, issueURLSegments, hosts}) {
return `(?:(?:[^\\w\\n\\v\\r]|^)+(${join([].concat(...Object.keys(actions).map(key => actions[key])))}))?(?:${[
'[^\\w\\n\\v\\r]|^',
join([...issuePrefixes, ...issueURLSegments]),
].join('|')})+${hosts.length > 0 ? `(?:${join(hosts)})?` : ''}((?:(?:[\\w-\\.]+)\\/)+(?:[\\w-\\.]+))?(${join([
...issuePrefixes,
...issueURLSegments,
])})(\\d+)(?!\\w)`;
function buildRefRegexp({actions, delimiters, issuePrefixes, issueURLSegments, hosts}) {
return `(?:(?:[^\\w\\n\\v\\r]|^)+(${join(
[].concat(...Object.keys(actions).map(key => actions[key]))
)}))?(?:[^\\w\\n\\v\\r]|^|(?: |\\t)*(?:${join([' ', '\t', ...delimiters])})(?: |\\t)*)${
hosts.length > 0 ? `(?:${join(hosts)})?` : ''
}((?:(?:[\\w-\\.]+)\\/)+(?:[\\w-\\.]+))?(${join([...issuePrefixes, ...issueURLSegments])})(\\d+)(?!\\w)`;
}

function buildRegexp(opts) {
Expand All @@ -65,7 +63,7 @@ function buildRegexp(opts) {
}

function buildMentionRegexp({mentionsPrefixes}) {
return new RegExp(`(${join(mentionsPrefixes)})([\\w-.]+)`, 'gim');
return new RegExp(`(${join(mentionsPrefixes)})([\\w-\\.]+)`, 'gim');
}

function parse(text, regexp, mentionRegexp, {actions, issuePrefixes, hosts}) {
Expand Down
5 changes: 5 additions & 0 deletions lib/hosts-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
// https://help.github.com/articles/about-duplicate-issues-and-pull-requests
duplicate: ['Duplicate of'],
},
delimiters: [':'],
// https://guides.github.com/features/issues/#notifications
mentionsPrefixes: ['@'],
issuePrefixes: ['#', 'gh-'],
Expand Down Expand Up @@ -39,6 +40,7 @@ module.exports = {
childOf: [],
duplicate: [],
},
delimiters: [],
// https://confluence.atlassian.com/bitbucket/mark-up-comments-issues-and-commit-messages-321859781.html
mentionsPrefixes: ['@'],
// https://confluence.atlassian.com/bitbucket/mark-up-comments-issues-and-commit-messages-321859781.html
Expand Down Expand Up @@ -74,6 +76,7 @@ module.exports = {
// https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/12845
duplicate: ['/duplicate'],
},
delimiters: [],
// https://about.gitlab.com/2016/03/08/gitlab-tutorial-its-all-connected
mentionsPrefixes: ['@'],
// https://about.gitlab.com/2016/03/08/gitlab-tutorial-its-all-connected
Expand All @@ -95,6 +98,7 @@ module.exports = {
// https://help.github.com/articles/about-duplicate-issues-and-pull-requests
duplicate: ['Duplicate of'],
},
delimiters: [':'],
// https://guides.github.com/features/issues/#notifications
mentionsPrefixes: ['@'],
issuePrefixes: ['#', 'gh-'],
Expand Down Expand Up @@ -127,6 +131,7 @@ module.exports = {
childOf: ['child of', 'child to', 'child'],
duplicate: ['Duplicate of', '/duplicate'],
},
delimiters: [':'],
mentionsPrefixes: ['@'],
issuePrefixes: ['#', 'gh-'],
hosts: ['https://github.com', 'https://gitlab.com'],
Expand Down
19 changes: 14 additions & 5 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import m from '..';
test('Parse GitHub issue', t => {
t.deepEqual(
m('GitHub')(
'Fix #1 reSOLved gh-2 CLOSES Gh-3 fix o/r#4 #5 o/r#6 fix https://github.com/o/r/issues/7 https://github.com/o/r/issues/8 fix https://github.com/o/r/pull/9 https://github.com/o/r/pull/10 fixing #11 Duplicate OF #12 @user'
'Fix #1 reSOLved gh-2 CLOSES Gh-3 fix o/r#4 #5 o/r#6 fix https://github.com/o/r/issues/7 https://github.com/o/r/issues/8 fix https://github.com/o/r/pull/9 https://github.com/o/r/pull/10 fixing #11 Duplicate OF #12 Fix:#13 Fix: #14 Fix::#15 @user'
),
{
actions: {
Expand All @@ -15,6 +15,8 @@ test('Parse GitHub issue', t => {
{raw: 'fix o/r#4', action: 'Fix', slug: 'o/r', prefix: '#', issue: '4'},
{raw: 'fix https://github.com/o/r/issues/7', action: 'Fix', slug: 'o/r', prefix: undefined, issue: '7'},
{raw: 'fix https://github.com/o/r/pull/9', action: 'Fix', slug: 'o/r', prefix: undefined, issue: '9'},
{raw: 'Fix:#13', action: 'Fix', slug: undefined, prefix: '#', issue: '13'},
{raw: 'Fix: #14', action: 'Fix', slug: undefined, prefix: '#', issue: '14'},
],
duplicate: [{raw: 'Duplicate OF #12', action: 'Duplicate of', slug: undefined, prefix: '#', issue: '12'}],
},
Expand All @@ -24,6 +26,7 @@ test('Parse GitHub issue', t => {
{raw: 'https://github.com/o/r/issues/8', slug: 'o/r', prefix: undefined, issue: '8'},
{raw: 'https://github.com/o/r/pull/10', slug: 'o/r', prefix: undefined, issue: '10'},
{raw: '#11', slug: undefined, prefix: '#', issue: '11'},
{raw: '#15', slug: undefined, prefix: '#', issue: '15'},
],
mentions: [{raw: '@user', prefix: '@', user: 'user'}],
}
Expand Down Expand Up @@ -130,7 +133,7 @@ test('Parse Waffle issue', t => {
test('Parse with default options', t => {
t.deepEqual(
m()(
'Fix #1 reSOLved gh-2 CLOSES Gh-3 fix o/r#4 #5 o/r#6 implementing #7 https://github.com/o/r/issues/8 implementing https://github.com/o/r/issues/9 Duplicate OF #10 @user'
'Fix #1 reSOLved gh-2 CLOSES Gh-3 fix o/r#4 #5 o/r#6 implementing #7 https://github.com/o/r/issues/8 implementing https://github.com/o/r/issues/9 Duplicate OF #10 Fix: #11 @user'
),
{
actions: {
Expand All @@ -147,6 +150,7 @@ test('Parse with default options', t => {
prefix: undefined,
issue: '9',
},
{raw: 'Fix: #11', action: 'Fix', slug: undefined, prefix: '#', issue: '11'},
],
block: [],
require: [],
Expand All @@ -167,17 +171,22 @@ test('Parse with default options', t => {
test('Parse with custom options', t => {
t.deepEqual(
m({
actions: {close: ['fix'], fix: ['fix'], duplicate: undefined},
actions: {close: ['fix', 'close'], fix: ['fix'], duplicate: undefined},
delimiters: [':', '*'],
mentionsPrefixes: '!',
issuePrefixes: ['#'],
hosts: ['http://host1.com/', 'http://host2.com'],
issueURLSegments: ['bugs'],
})(
'Fix #1 reSOLved gh-2 CLOSES Gh-3 fixed o/r#4 #5 o/r#6 fixing #7 http://host1.com/o/r/bugs/8 http://host2.com/o/r/bugs/9 Duplicate OF #10 !user @other'
'Fix #1 reSOLved gh-2 CLOSES Gh-3 fixed o/r#4 #5 o/r#6 fixing #7 http://host1.com/o/r/bugs/8 http://host2.com/o/r/bugs/9 Duplicate OF #10 Close:#11 Close* #12 !user @other'
),
{
actions: {
close: [{raw: 'Fix #1', action: 'Fix', slug: undefined, prefix: '#', issue: '1'}],
close: [
{raw: 'Fix #1', action: 'Fix', slug: undefined, prefix: '#', issue: '1'},
{raw: 'Close:#11', action: 'Close', slug: undefined, prefix: '#', issue: '11'},
{raw: 'Close* #12', action: 'Close', slug: undefined, prefix: '#', issue: '12'},
],
fix: [{raw: 'Fix #1', action: 'Fix', slug: undefined, prefix: '#', issue: '1'}],
block: [],
require: [],
Expand Down

0 comments on commit 55c5661

Please sign in to comment.