forked from macklinu/danger-plugin-jira-issue
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 1.0.0 (2023-04-13) ### Bug Fixes * **jiraIssue:** properly resolve URL ([af099b9](af099b9)) * **node:** support Node 4 ([d89d651](d89d651)) * only add one example JIRA key to warning ([macklinu#47](https://github.com/mcgrathg/danger-plugin-jira-integration/issues/47)) ([56dee38](56dee38)) ### Features * `key` is now optional field ([2acddae](2acddae)) * **$options:** Adds message format function to options ([macklinu#33](https://github.com/mcgrathg/danger-plugin-jira-integration/issues/33)) ([ecfead9](ecfead9)) * **jiraIssue:** add initial implementation ([#1](#1)) ([8638e2f](8638e2f)) * **options:** add location to options ([macklinu#44](https://github.com/mcgrathg/danger-plugin-jira-integration/issues/44)) ([96e6261](96e6261)) * support multiple JIRA issues in PR title ([86c01d0](86c01d0)), closes [macklinu#8](https://github.com/mcgrathg/danger-plugin-jira-integration/issues/8) * support multiple JIRA projects ([macklinu#26](https://github.com/mcgrathg/danger-plugin-jira-integration/issues/26)) ([f0a084f](f0a084f)), closes [macklinu#24](https://github.com/mcgrathg/danger-plugin-jira-integration/issues/24) ### BREAKING CHANGES * **jiraIssue:** this commit introduces functionality ready for a 1.0.0 release.
- Loading branch information
1 parent
1328826
commit 263b0b7
Showing
3 changed files
with
226 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const url_1 = require("url"); | ||
/** | ||
* Danger plugin to integrate your pull request with JIRA | ||
*/ | ||
function jiraIntegration({ key, url, format = defaultFormat, caseSensitive = false }) { | ||
if (!url) { | ||
throw Error(`'url' missing - must supply JIRA installation URL`); | ||
} | ||
// Support multiple JIRA projects. | ||
const keys = key ? (Array.isArray(key) ? `(${key.join('|')})` : key) : '[A-Za-z]{2,4}'; | ||
const jiraKeyRegex = new RegExp(`(${keys}-[0-9]+)`, `g${caseSensitive ? '' : 'i'}`); | ||
function findMatches(property) { | ||
const issues = []; | ||
let match = jiraKeyRegex.exec(property); | ||
while (match !== null) { | ||
issues.push(match[0].toLowerCase()); | ||
match = jiraKeyRegex.exec(property); | ||
} | ||
return issues; | ||
} | ||
const allIssues = new Set([ | ||
...findMatches(danger.github.pr.title), | ||
...findMatches(danger.github.pr.head ? danger.github.pr.head.ref : ''), | ||
...findMatches(danger.github.pr.body), | ||
]); | ||
if (allIssues.size > 0) { | ||
// URL must end with a slash before attempting to fully resolve the JIRA URL. | ||
url = ensureUrlEndsWithSlash(url); | ||
const jiraUrls = Array.from(allIssues).map(issue => { | ||
const formattedIssue = issue.toUpperCase(); | ||
const resolvedUrl = (0, url_1.resolve)(url, formattedIssue); | ||
return link(resolvedUrl, formattedIssue); | ||
}); | ||
message(format(jiraUrls)); | ||
} | ||
else { | ||
let warningKeys; | ||
if (key) { | ||
warningKeys = Array.isArray(key) ? key.map(k => `${k}-123`).join(', ') : key + '-123'; | ||
} | ||
else { | ||
warningKeys = 'ABC-123'; | ||
} | ||
warn(`No JIRA keys found in the PR title, branch name, or commit messages (e.g. ${warningKeys}).`); | ||
} | ||
} | ||
exports.default = jiraIntegration; | ||
function link(href, text) { | ||
return `<a href="${href}">${text}</a>`; | ||
} | ||
function ensureUrlEndsWithSlash(url) { | ||
return url.endsWith('/') ? url : url.concat('/'); | ||
} | ||
const defaultFormat = (urls) => `:link: ${urls.join(', ')}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const index_1 = require("./index"); | ||
describe('jiraIntegration()', () => { | ||
beforeEach(() => { | ||
global.warn = jest.fn(); | ||
global.message = jest.fn(); | ||
}); | ||
afterEach(() => { | ||
global.danger = undefined; | ||
global.warn = undefined; | ||
global.message = undefined; | ||
}); | ||
it('throws when supplied invalid configuration', () => { | ||
const anyJira = index_1.default; | ||
expect(() => anyJira()).toThrow(); | ||
expect(() => (0, index_1.default)({})).toThrow(); | ||
expect(() => (0, index_1.default)({ key: 'ABC' })).toThrow(); | ||
}); | ||
it('warns when PR title is missing JIRA issue key', () => { | ||
global.danger = { github: { pr: { title: 'Change some things' } } }; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.warn).toHaveBeenCalledWith(`No JIRA keys found in the PR title, branch name, or commit messages (e.g. ABC-123).`); | ||
}); | ||
it('warns when PR title is missing all of the multiple JIRA issue keys', () => { | ||
global.danger = { github: { pr: { title: 'Change some things' } } }; | ||
(0, index_1.default)({ | ||
key: ['ABC', 'DEF'], | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.warn).toHaveBeenCalledWith("No JIRA keys found in the PR title, branch name, or commit messages (e.g. ABC-123, DEF-123)."); | ||
}); | ||
it('adds the JIRA issue link from PR title to the messages table', () => { | ||
global.danger = { | ||
github: { pr: { title: '[ABC-808] Change some things' } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
it('properly concatenates URL parts (trailing slash in url)', () => { | ||
global.danger = { | ||
github: { pr: { title: '[ABC-808] Change some things' } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse/', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
it('matches JIRA issue anywhere in title', () => { | ||
global.danger = { github: { pr: { title: 'My changes - ABC-123' } } }; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>'); | ||
}); | ||
it('matches JIRA issue anywhere in title when no configuration key', () => { | ||
global.danger = { github: { pr: { title: 'My changes - ABC-123' } } }; | ||
(0, index_1.default)({ | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>'); | ||
}); | ||
it('matches lowercase JIRA key in the git branch', () => { | ||
global.danger = { | ||
github: { pr: { head: { ref: 'abc-808/some-things' } } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
it('matches lowercase JIRA key in PR title', () => { | ||
global.danger = { | ||
github: { pr: { title: '[abc-808] Change some things' } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
it('does not match lowercase JIRA key in PR title when case-sensitive', () => { | ||
global.danger = { | ||
github: { pr: { title: '[abc-808] Change some things' } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
caseSensitive: true, | ||
}); | ||
expect(global.warn).toHaveBeenCalled(); | ||
}); | ||
it('supports multiple JIRA keys in PR title', () => { | ||
global.danger = { | ||
github: { pr: { title: '[ABC-123][ABC-456] Change some things' } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/ABC-456">ABC-456</a>'); | ||
}); | ||
it('supports multiple JIRA boards in PR title', () => { | ||
global.danger = { | ||
github: { pr: { title: '[ABC-123][DEF-456] Change some things' } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: ['ABC', 'DEF'], | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/DEF-456">DEF-456</a>'); | ||
}); | ||
it('supports a custom format function', () => { | ||
global.danger = { | ||
github: { pr: { title: '[ABC-123][DEF-456] Change some things' } }, | ||
}; | ||
(0, index_1.default)({ | ||
format: (jiraUrls) => { | ||
return `JIRA Tickets: ${jiraUrls.join(', ')}`; | ||
}, | ||
key: ['ABC', 'DEF'], | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith('JIRA Tickets: <a href="https://jira.net/browse/ABC-123">ABC-123</a>, <a href="https://jira.net/browse/DEF-456">DEF-456</a>'); | ||
}); | ||
it('supports JIRA key in the git branch', () => { | ||
global.danger = { | ||
github: { pr: { head: { ref: 'ABC-808/some-things' } } }, | ||
}; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
it('supports JIRA key in the git branch when no configuration key', () => { | ||
global.danger = { | ||
github: { pr: { head: { ref: 'ABC-808/some-things' } } }, | ||
}; | ||
(0, index_1.default)({ | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
it('supports JIRA key in the PR body', () => { | ||
global.danger = { github: { pr: { body: '[ABC-808] Change some things' } } }; | ||
(0, index_1.default)({ | ||
key: 'ABC', | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
it('supports JIRA key in the PR body when no configuration key', () => { | ||
global.danger = { github: { pr: { body: '[ABC-808] Change some things' } } }; | ||
(0, index_1.default)({ | ||
url: 'https://jira.net/browse', | ||
}); | ||
expect(global.message).toHaveBeenCalledWith(':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters