Refactor ClosingIssueExtractor.

This commit is contained in:
Douwe Maan 2015-04-03 18:03:26 +02:00
parent b492f0f86e
commit e33ddfebf2
3 changed files with 14 additions and 15 deletions

View file

@ -118,7 +118,7 @@ class Commit
# Discover issues should be closed when this commit is pushed to a project's # Discover issues should be closed when this commit is pushed to a project's
# default branch. # default branch.
def closes_issues(project, current_user = self.committer) def closes_issues(project, current_user = self.committer)
Gitlab::ClosingIssueExtractor.closed_by_message_in_project(safe_message, project, current_user) Gitlab::ClosingIssueExtractor.new(project, current_user).closed_by_message(safe_message)
end end
# Mentionable override. # Mentionable override.

View file

@ -260,8 +260,8 @@ class MergeRequest < ActiveRecord::Base
def closes_issues(current_user = self.author) def closes_issues(current_user = self.author)
if target_branch == project.default_branch if target_branch == project.default_branch
issues = commits.flat_map { |c| c.closes_issues(project, current_user) } issues = commits.flat_map { |c| c.closes_issues(project, current_user) }
issues.push(*Gitlab::ClosingIssueExtractor. issues.push(*Gitlab::ClosingIssueExtractor.new(project, current_user).
closed_by_message_in_project(description, project, current_user)) closed_by_message(description))
issues.uniq.sort_by(&:id) issues.uniq.sort_by(&:id)
else else
[] []

View file

@ -1,21 +1,20 @@
module Gitlab module Gitlab
module ClosingIssueExtractor class ClosingIssueExtractor
ISSUE_CLOSING_REGEX = Regexp.new(Gitlab.config.gitlab.issue_closing_pattern) ISSUE_CLOSING_REGEX = Regexp.new(Gitlab.config.gitlab.issue_closing_pattern)
def self.closed_by_message_in_project(message, project, current_user = nil) def initialize(project, current_user = nil)
issues = [] @extractor = Gitlab::ReferenceExtractor.new(project, current_user)
end
unless message.nil? def closed_by_message(message)
md = message.scan(ISSUE_CLOSING_REGEX) return [] if message.nil?
md.each do |ref| closing_statements = message.scan(ISSUE_CLOSING_REGEX).
extractor = Gitlab::ReferenceExtractor.new(project, current_user) map { |ref| ref[0] }.join(" ")
extractor.analyze(ref[0])
issues += extractor.issues
end
end
issues.uniq @extractor.analyze(closing_statements)
@extractor.issues
end end
end end
end end