From e33ddfebf2248fe31bb27fe1d34048df97ed61b0 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Fri, 3 Apr 2015 18:03:26 +0200 Subject: [PATCH] Refactor ClosingIssueExtractor. --- app/models/commit.rb | 2 +- app/models/merge_request.rb | 4 ++-- lib/gitlab/closing_issue_extractor.rb | 23 +++++++++++------------ 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/app/models/commit.rb b/app/models/commit.rb index 084cf420079..7a0ad137650 100644 --- a/app/models/commit.rb +++ b/app/models/commit.rb @@ -118,7 +118,7 @@ class Commit # Discover issues should be closed when this commit is pushed to a project's # default branch. 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 # Mentionable override. diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb index 167d7f9c71f..35cb920d8bc 100644 --- a/app/models/merge_request.rb +++ b/app/models/merge_request.rb @@ -260,8 +260,8 @@ class MergeRequest < ActiveRecord::Base def closes_issues(current_user = self.author) if target_branch == project.default_branch issues = commits.flat_map { |c| c.closes_issues(project, current_user) } - issues.push(*Gitlab::ClosingIssueExtractor. - closed_by_message_in_project(description, project, current_user)) + issues.push(*Gitlab::ClosingIssueExtractor.new(project, current_user). + closed_by_message(description)) issues.uniq.sort_by(&:id) else [] diff --git a/lib/gitlab/closing_issue_extractor.rb b/lib/gitlab/closing_issue_extractor.rb index 6425193d85f..ab184d95c05 100644 --- a/lib/gitlab/closing_issue_extractor.rb +++ b/lib/gitlab/closing_issue_extractor.rb @@ -1,21 +1,20 @@ module Gitlab - module ClosingIssueExtractor + class ClosingIssueExtractor ISSUE_CLOSING_REGEX = Regexp.new(Gitlab.config.gitlab.issue_closing_pattern) - def self.closed_by_message_in_project(message, project, current_user = nil) - issues = [] + def initialize(project, current_user = nil) + @extractor = Gitlab::ReferenceExtractor.new(project, current_user) + end - unless message.nil? - md = message.scan(ISSUE_CLOSING_REGEX) + def closed_by_message(message) + return [] if message.nil? + + closing_statements = message.scan(ISSUE_CLOSING_REGEX). + map { |ref| ref[0] }.join(" ") - md.each do |ref| - extractor = Gitlab::ReferenceExtractor.new(project, current_user) - extractor.analyze(ref[0]) - issues += extractor.issues - end - end + @extractor.analyze(closing_statements) - issues.uniq + @extractor.issues end end end