lib/jira/plugin.rb in danger-jira-0.4.0 vs lib/jira/plugin.rb in danger-jira-0.4.1

- old
+ new

@@ -36,45 +36,59 @@ # def check(key: nil, url: nil, emoji: ":link:", search_title: true, search_commits: false, fail_on_warning: false, report_missing: true) 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]+)" regexp = Regexp.new(/#{jira_key_regex_string}/) 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 end