Commit 3f1ece26909f90e538a50cd724b64da28f0b7308

Authored by Jacob Vosmaer
1 parent 5fdcaadf

Detect closing issues in Merge Request description

@@ -35,6 +35,7 @@ v 7.0.0 @@ -35,6 +35,7 @@ v 7.0.0
35 - Be more selective when killing stray Sidekiqs 35 - Be more selective when killing stray Sidekiqs
36 - Check LDAP user filter during sign-in 36 - Check LDAP user filter during sign-in
37 - Remove wall feature (no data loss - you can take it from database) 37 - Remove wall feature (no data loss - you can take it from database)
  38 + - Detect issues closed by Merge Request description
38 39
39 v 6.9.2 40 v 6.9.2
40 - Revert the commit that broke the LDAP user filter 41 - Revert the commit that broke the LDAP user filter
app/models/commit.rb
@@ -111,22 +111,10 @@ class Commit @@ -111,22 +111,10 @@ class Commit
111 description.present? 111 description.present?
112 end 112 end
113 113
114 - # Regular expression that identifies commit message clauses that trigger issue closing.  
115 - def issue_closing_regex  
116 - @issue_closing_regex ||= Regexp.new(Gitlab.config.gitlab.issue_closing_pattern)  
117 - end  
118 -  
119 # Discover issues should be closed when this commit is pushed to a project's 114 # Discover issues should be closed when this commit is pushed to a project's
120 # default branch. 115 # default branch.
121 def closes_issues project 116 def closes_issues project
122 - md = issue_closing_regex.match(safe_message)  
123 - if md  
124 - extractor = Gitlab::ReferenceExtractor.new  
125 - extractor.analyze(md[0])  
126 - extractor.issues_for(project)  
127 - else  
128 - []  
129 - end 117 + Gitlab::ClosingIssueExtractor.closed_by_message_in_project(safe_message, project)
130 end 118 end
131 119
132 # Mentionable override. 120 # Mentionable override.
app/models/merge_request.rb
@@ -220,7 +220,9 @@ class MergeRequest < ActiveRecord::Base @@ -220,7 +220,9 @@ class MergeRequest < ActiveRecord::Base
220 # Return the set of issues that will be closed if this merge request is accepted. 220 # Return the set of issues that will be closed if this merge request is accepted.
221 def closes_issues 221 def closes_issues
222 if target_branch == project.default_branch 222 if target_branch == project.default_branch
223 - commits.map { |c| c.closes_issues(project) }.flatten.uniq.sort_by(&:id) 223 + issues = commits.flat_map { |c| c.closes_issues(project) }
  224 + issues += Gitlab::ClosingIssueExtractor.closed_by_message_in_project(description, project)
  225 + issues.uniq.sort_by(&:id)
224 else 226 else
225 [] 227 []
226 end 228 end
lib/gitlab/closing_issue_extractor.rb 0 → 100644
@@ -0,0 +1,16 @@ @@ -0,0 +1,16 @@
  1 +module Gitlab
  2 + module ClosingIssueExtractor
  3 + ISSUE_CLOSING_REGEX = Regexp.new(Gitlab.config.gitlab.issue_closing_pattern)
  4 +
  5 + def self.closed_by_message_in_project(message, project)
  6 + md = ISSUE_CLOSING_REGEX.match(message)
  7 + if md
  8 + extractor = Gitlab::ReferenceExtractor.new
  9 + extractor.analyze(md[0])
  10 + extractor.issues_for(project)
  11 + else
  12 + []
  13 + end
  14 + end
  15 + end
  16 +end
spec/models/merge_request_spec.rb
@@ -105,6 +105,14 @@ describe MergeRequest do @@ -105,6 +105,14 @@ describe MergeRequest do
105 105
106 subject.closes_issues.should be_empty 106 subject.closes_issues.should be_empty
107 end 107 end
  108 +
  109 + it 'detects issues mentioned in the description' do
  110 + issue2 = create(:issue, project: subject.project)
  111 + subject.description = "Closes ##{issue2.iid}"
  112 + subject.project.stub(default_branch: subject.target_branch)
  113 +
  114 + subject.closes_issues.should include(issue2)
  115 + end
108 end 116 end
109 117
110 it_behaves_like 'an editable mentionable' do 118 it_behaves_like 'an editable mentionable' do