Skip to content

Commit f0a084f

Browse files
authored
feat: support multiple JIRA projects (#26)
resolves #24
1 parent c9be379 commit f0a084f

File tree

7 files changed

+65
-37
lines changed

7 files changed

+65
-37
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"prettier.trailingComma": "es5",
3+
"prettier.semi": false,
4+
"prettier.singleQuote": true
5+
}

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ With JIRA-123 in the PR title, Danger will comment with:
4949
Generated by :no_entry_sign: <a href="http://github.com/danger/danger-js/">dangerJS</a>
5050
</p>
5151

52+
If you work with multiple JIRA project boards, you can supply multiple project keys:
53+
54+
```js
55+
jiraIssue({
56+
key: ['ABC', 'DEF'],
57+
url: 'https://myjira.atlassian.net/browse',
58+
})
59+
```
60+
61+
This plugin will recognize issues starting with those keys (e.g. `ABC-123` and `DEF-234`).
62+
5263
See the [documentation](http://macklinu.github.io/danger-plugin-jira-issue) for detailed information.
5364

5465
## Changelog

package.json

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"description": "Danger plugin to link JIRA issue in pull request",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
7-
"files": [
8-
"dist"
9-
],
7+
"files": ["dist"],
108
"scripts": {
119
"precommit": "lint-staged",
1210
"commit": "git-cz",
@@ -19,17 +17,14 @@
1917
"test": "jest --env=node",
2018
"predocs": "rm -rf docs/",
2119
"prepublish": "npm run build",
22-
"semantic-release": "semantic-release pre && npm publish && semantic-release post"
20+
"semantic-release":
21+
"semantic-release pre && npm publish && semantic-release post"
2322
},
2423
"repository": {
2524
"type": "git",
2625
"url": "https://github.com/macklinu/danger-plugin-jira-issue.git"
2726
},
28-
"keywords": [
29-
"danger",
30-
"danger-plugin",
31-
"jira"
32-
],
27+
"keywords": ["danger", "danger-plugin", "jira"],
3328
"author": "Macklin Underdown <macklinu@gmail.com>",
3429
"license": "MIT",
3530
"bugs": {
@@ -48,9 +43,11 @@
4843
"husky": "^0.14.1",
4944
"jest": "^20.0.4",
5045
"lint-staged": "^4.0.0",
46+
"prettier": "^1.7.0",
5147
"semantic-release": "^6.3.6",
5248
"ts-jest": "^20.0.5",
5349
"tslint": "^5.4.2",
50+
"tslint-config-prettier": "^1.5.0",
5451
"typedoc": "^0.7.1",
5552
"typescript": "^2.5.2",
5653
"validate-commit-msg": "^2.12.1"
@@ -63,21 +60,14 @@
6360
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
6461
},
6562
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
66-
"moduleFileExtensions": [
67-
"ts",
68-
"tsx",
69-
"js"
70-
]
63+
"moduleFileExtensions": ["ts", "tsx", "js"]
7164
},
7265
"config": {
7366
"commitizen": {
7467
"path": "cz-conventional-changelog"
7568
}
7669
},
7770
"lint-staged": {
78-
"*.ts": [
79-
"tslint --fix",
80-
"git add"
81-
]
71+
"*.ts": ["tslint --fix", "git add"]
8272
}
8373
}

src/index.test.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('jiraIssue()', () => {
2626
url: 'https://jira.net/browse',
2727
})
2828
expect(global.warn).toHaveBeenCalledWith(
29-
'Please add the JIRA issue key to the PR title (e.g. ABC-123)',
29+
'Please add the JIRA issue key to the PR title (e.g. ABC-123)'
3030
)
3131
})
3232
it('adds the JIRA issue link to the messages table', () => {
@@ -38,7 +38,7 @@ describe('jiraIssue()', () => {
3838
url: 'https://jira.net/browse',
3939
})
4040
expect(global.message).toHaveBeenCalledWith(
41-
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>',
41+
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'
4242
)
4343
})
4444
it('properly concatenates URL parts (trailing slash in url)', () => {
@@ -50,7 +50,7 @@ describe('jiraIssue()', () => {
5050
url: 'https://jira.net/browse/',
5151
})
5252
expect(global.message).toHaveBeenCalledWith(
53-
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>',
53+
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'
5454
)
5555
})
5656
it('matches JIRA issue anywhere in title', () => {
@@ -60,7 +60,7 @@ describe('jiraIssue()', () => {
6060
url: 'https://jira.net/browse',
6161
})
6262
expect(global.message).toHaveBeenCalledWith(
63-
':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>',
63+
':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>'
6464
)
6565
})
6666
it('does not match lowercase JIRA key in PR title', () => {
@@ -81,7 +81,7 @@ describe('jiraIssue()', () => {
8181
url: 'https://jira.net/browse',
8282
})
8383
expect(global.message).toHaveBeenCalledWith(
84-
':paperclip: <a href="https://jira.net/browse/ABC-123">ABC-123</a>',
84+
':paperclip: <a href="https://jira.net/browse/ABC-123">ABC-123</a>'
8585
)
8686
})
8787
it('supports multiple JIRA keys in PR title', () => {
@@ -93,8 +93,19 @@ describe('jiraIssue()', () => {
9393
url: 'https://jira.net/browse',
9494
})
9595
expect(global.message).toHaveBeenCalledWith(
96-
// tslint:disable-next-line:max-line-length
97-
':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/ABC-456">ABC-456</a>',
96+
':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/ABC-456">ABC-456</a>'
97+
)
98+
})
99+
it('supports multiple JIRA boards in PR title', () => {
100+
global.danger = {
101+
github: { pr: { title: '[ABC-123][DEF-456] Change some things' } },
102+
}
103+
jiraIssue({
104+
key: ['ABC', 'DEF'],
105+
url: 'https://jira.net/browse',
106+
})
107+
expect(global.message).toHaveBeenCalledWith(
108+
':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/DEF-456">DEF-456</a>'
98109
)
99110
})
100111
})

src/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { resolve } from 'url'
22

33
export interface Options {
4-
/** The JIRA issue key (e.g. the ABC in ABC-123). */
5-
key: string
4+
/**
5+
* The JIRA issue key(s) (e.g. the ABC in ABC-123).
6+
* Supports multiple JIRA projects (e.g. `['ABC', 'DEF']`).
7+
*/
8+
key: string | string[]
69
/** The JIRA instance issue base URL (e.g. https://jira.atlassian.com/browse/). */
710
url: string
811
/**
@@ -14,11 +17,12 @@ export interface Options {
1417
emoji?: string
1518
}
1619

17-
const link = (href: string, text: string): string => `<a href="${href}">${text}</a>`
20+
const link = (href: string, text: string): string =>
21+
`<a href="${href}">${text}</a>`
1822

1923
const ensureUrlEndsWithSlash = (url: string) => {
2024
if (!url.endsWith('/')) {
21-
return url.concat('/')
25+
return url.concat('/')
2226
}
2327
return url
2428
}
@@ -38,7 +42,10 @@ export default function jiraIssue(options: Options) {
3842
throw Error(`'key' missing - must supply JIRA issue key`)
3943
}
4044

41-
const jiraKeyRegex = new RegExp(`(${key}-[0-9]+)`, 'g')
45+
// Support multiple JIRA projects.
46+
const keys = Array.isArray(key) ? `(${key.join('|')})` : key
47+
48+
const jiraKeyRegex = new RegExp(`(${keys}-[0-9]+)`, 'g')
4249
let match
4350
const jiraIssues = []
4451
// tslint:disable-next-line:no-conditional-assignment
@@ -47,7 +54,7 @@ export default function jiraIssue(options: Options) {
4754
}
4855
if (jiraIssues.length > 0) {
4956
const jiraUrls = jiraIssues
50-
.map((issue) => link(resolve(ensureUrlEndsWithSlash(url), issue), issue))
57+
.map(issue => link(resolve(ensureUrlEndsWithSlash(url), issue), issue))
5158
.join(', ')
5259
message(`${emoji} ${jiraUrls}`)
5360
} else {

tslint.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
{
2-
"extends": [
3-
"tslint:recommended"
4-
],
2+
"extends": ["tslint:recommended", "tslint-config-prettier"],
53
"rules": {
6-
"interface-name": [true, "never-prefix"],
7-
"semicolon": [true, "never"],
8-
"quotemark": [true, "single"]
4+
"interface-name": [true, "never-prefix"]
95
}
106
}

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3249,6 +3249,10 @@ preserve@^0.2.0:
32493249
version "0.2.0"
32503250
resolved "https://registry.yarnpkg.com/preserve/-/preserve-0.2.0.tgz#815ed1f6ebc65926f865b310c0713bcb3315ce4b"
32513251

3252+
prettier@^1.7.0:
3253+
version "1.7.0"
3254+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.7.0.tgz#47481588f41f7c90f63938feb202ac82554e7150"
3255+
32523256
pretty-format@^20.0.1:
32533257
version "20.0.1"
32543258
resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-20.0.1.tgz#ba95329771907c189643dd251e244061ff642350"
@@ -4056,6 +4060,10 @@ tslib@^1.7.1:
40564060
version "1.7.1"
40574061
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.7.1.tgz#bc8004164691923a79fe8378bbeb3da2017538ec"
40584062

4063+
tslint-config-prettier@^1.5.0:
4064+
version "1.5.0"
4065+
resolved "https://registry.yarnpkg.com/tslint-config-prettier/-/tslint-config-prettier-1.5.0.tgz#76645425edcc34d9b6835ba58266eaf90fdbfeda"
4066+
40594067
tslint@^5.4.2:
40604068
version "5.4.2"
40614069
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.4.2.tgz#609b6640cc0424f4a395a9adf68c375563c549c7"

0 commit comments

Comments
 (0)