Skip to content

Commit

Permalink
Merge pull request #9 from RestlessThinker/issue-key-format
Browse files Browse the repository at this point in the history
Fix issue key formatting
  • Loading branch information
RestlessThinker authored Aug 4, 2018
2 parents 2803863 + 8bf6198 commit 7efcc5a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/jira/gem_version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Jira
VERSION = "0.4.0".freeze
VERSION = "0.4.1".freeze
end
50 changes: 32 additions & 18 deletions lib/jira/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@ def check(key: nil, url: nil, emoji: ":link:", search_title: true, search_commit
throw Error("'key' missing - must supply JIRA issue key") if key.nil?
throw Error("'url' missing - must supply JIRA installation URL") if url.nil?

jira_issues = find_jira_issues(
key: key,
search_title: search_title,
search_commits: search_commits
)

if !jira_issues.empty?
jira_urls = jira_issues.map { |issue| link(href: ensure_url_ends_with_slash(url), issue: issue) }.join(", ")
message("#{emoji} #{jira_urls}")
elsif report_missing
msg = "This PR does not contain any JIRA issue keys in the PR title or commit messages (e.g. KEY-123)"
if fail_on_warning
fail(msg)
else
warn(msg)
end
end
end

private

def find_jira_issues(key: nil, search_title: true, search_commits: false)
# Support multiple JIRA projects
keys = key.kind_of?(Array) ? key.join("|") : key
jira_key_regex_string = "((?:#{keys})-[0-9]+)"
Expand All @@ -46,35 +68,27 @@ def check(key: nil, url: nil, emoji: ":link:", search_title: true, search_commit
jira_issues = []

if search_title
jira_issues << github.pr_title.scan(regexp)
github.pr_title.gsub(regexp) do |match|
jira_issues << match
end
end

if search_commits
jira_issues << git.commits.map { |commit| commit.message.scan(regexp) }.compact
git.commits.map do |commit|
commit.message.gsub(regexp) do |match|
jira_issues << match
end
end
end

jira_issues.flatten.uniq

if jira_issues.empty?
github.pr_body.gsub(regexp) do |match|
jira_issues << match
end
end

if !jira_issues.empty?
jira_urls = jira_issues.map { |issue| link(href: ensure_url_ends_with_slash(url), issue: issue) }.join(", ")
message("#{emoji} #{jira_urls}")
elsif report_missing
msg = "This PR does not contain any JIRA issue keys in the PR title or commit messages (e.g. KEY-123)"
if fail_on_warning
fail(msg)
else
warn(msg)
end
end
return jira_issues
end

private

def ensure_url_ends_with_slash(url)
return "#{url}/" unless url.end_with?("/")
return url
Expand Down
35 changes: 33 additions & 2 deletions spec/jira_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,41 @@ module Danger
#
describe "with Dangerfile" do
before do
@jira = testing_dangerfile.jira
DangerJira.send(:public, *DangerJira.private_instance_methods)
github = Danger::RequestSources::GitHub.new({}, testing_env)
end

# Some examples for writing tests
# You should replace these with your own.
it "can find jira issues via title" do
allow(@jira).to receive_message_chain("github.pr_title").and_return("Ticket [WEB-123] and WEB-124")
issues = @jira.find_jira_issues(key: "WEB")
expect((issues <=> ["WEB-123", "WEB-124"]) == 0)
end

it "can find jira issues in commits" do
single_commit = Object.new
def single_commit.message
"WIP [WEB-125]"
end
commits = [single_commit]
allow(@jira).to receive_message_chain("git.commits").and_return(commits)
issues = @jira.find_jira_issues(
key: "WEB",
search_title: false,
search_commits: true
)
expect((issues <=> ["WEB-125"]) == 0)
end

it "can find jira issues in pr body" do
allow(@jira).to receive_message_chain("github.pr_body").and_return("[WEB-126]")
issues = @jira.find_jira_issues(
key: "WEB",
search_title: false,
search_commits: false
)
expect((issues <=> ["WEB-126"]) == 0)
end
end
end
end

0 comments on commit 7efcc5a

Please sign in to comment.