Skip to content

Commit

Permalink
fix(jiraIssue): properly resolve URL
Browse files Browse the repository at this point in the history
This commit fixes how URL's are resolve by using url.resolve() instead of path.join(). The URL must
end with a '/' in order for it to properly be resolved. Also, index.test.js wasn't referencing
index.js... 😳
  • Loading branch information
macklinu committed May 17, 2017
1 parent 1a67f99 commit af099b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from 'path'
import { resolve } from 'url'

const link = (href, text) => `<a href="${href}">${text}</a>`

Expand All @@ -21,8 +21,11 @@ export default function jiraIssue({ key, url, emoji = ':link:' } = {}) {
const jiraKeyRegex = new RegExp(`^.*(${key}-[0-9]+).*$`, 'g')
const match = jiraKeyRegex.exec(danger.github.pr.title)
if (match) {
const jiraIssue = match[1]
const jiraUrl = link(join(url, jiraIssue), jiraIssue)
const issue = match[1]
if (!url.endsWith('/')) {
url += '/'
}
const jiraUrl = link(resolve(url, issue), issue)
message(`${emoji} ${jiraUrl}`)
} else {
warn(`Please add the JIRA issue key to the PR title (e.g. ${key}-123)`)
Expand Down
24 changes: 12 additions & 12 deletions src/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import jiraIssue from '../'
import jiraIssue from './'

describe('jiraIssue()', () => {
beforeEach(() => {
Expand All @@ -14,13 +14,13 @@ describe('jiraIssue()', () => {
expect(() => jiraIssue()).toThrow()
expect(() => jiraIssue({})).toThrow()
expect(() => jiraIssue({ key: 'ABC' })).toThrow()
expect(() => jiraIssue({ url: 'http://my.jira/browse' })).toThrow()
expect(() => jiraIssue({ url: 'https://jira.net/browse' })).toThrow()
})
it('warns when PR title is missing JIRA issue key', () => {
global.danger = { github: { pr: { title: 'Change some things' } } }
jiraIssue({
key: 'ABC',
url: 'http://my.jira/browse',
url: 'https://jira.net/browse',
})
expect(global.warn).toHaveBeenCalledWith(
'Please add the JIRA issue key to the PR title (e.g. ABC-123)',
Expand All @@ -32,10 +32,10 @@ describe('jiraIssue()', () => {
}
jiraIssue({
key: 'ABC',
url: 'http://my.jira/browse',
url: 'https://jira.net/browse',
})
expect(global.message).toHaveBeenCalledWith(
':link: <a href="http:/my.jira/browse/ABC-808">ABC-808</a>',
':link: <a href="https://jira.net/browse/ABC-808">ABC-808</a>',
)
})
it('properly concatenates URL parts (trailing slash in url)', () => {
Expand All @@ -44,20 +44,20 @@ describe('jiraIssue()', () => {
}
jiraIssue({
key: 'ABC',
url: 'http://my.jira/browse/',
url: 'https://jira.net/browse/',
})
expect(global.message).toHaveBeenCalledWith(
':link: <a href="http:/my.jira/browse/ABC-808">ABC-808</a>',
':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' } } }
jiraIssue({
key: 'ABC',
url: 'http://my.jira/browse',
url: 'https://jira.net/browse',
})
expect(global.message).toHaveBeenCalledWith(
':link: <a href="http:/my.jira/browse/ABC-123">ABC-123</a>',
':link: <a href="https://jira.net/browse/ABC-123">ABC-123</a>',
)
})
it('does not match lowercase JIRA key in PR title', () => {
Expand All @@ -66,19 +66,19 @@ describe('jiraIssue()', () => {
}
jiraIssue({
key: 'ABC',
url: 'http://my.jira/browse',
url: 'https://jira.net/browse',
})
expect(global.warn).toHaveBeenCalled()
})
it('honors custom emoji configuration', () => {
global.danger = { github: { pr: { title: '(ABC-123) Change stuff' } } }
jiraIssue({
key: 'ABC',
url: 'http://my.jira/browse',
url: 'https://jira.net/browse',
emoji: ':paperclip:',
})
expect(global.message).toHaveBeenCalledWith(
':paperclip: <a href="http:/my.jira/browse/ABC-123">ABC-123</a>',
':paperclip: <a href="https://jira.net/browse/ABC-123">ABC-123</a>',
)
})
})

0 comments on commit af099b9

Please sign in to comment.