Commit 6c3459978dff210af0066307f76800956cbec5a8
1 parent
5a4386a4
Exists in
master
and in
4 other branches
Add new methods to MR to check if source or target branch exists
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
2 changed files
with
36 additions
and
5 deletions
Show diff stats
app/controllers/projects/merge_requests_controller.rb
@@ -160,14 +160,17 @@ class Projects::MergeRequestsController < Projects::ApplicationController | @@ -160,14 +160,17 @@ class Projects::MergeRequestsController < Projects::ApplicationController | ||
160 | end | 160 | end |
161 | 161 | ||
162 | def validates_merge_request | 162 | def validates_merge_request |
163 | + # If source project was removed (Ex. mr from fork to origin) | ||
164 | + return invalid_mr unless @merge_request.source_project | ||
165 | + | ||
163 | # Show git not found page | 166 | # Show git not found page |
164 | # if there is no saved commits between source & target branch | 167 | # if there is no saved commits between source & target branch |
165 | if @merge_request.commits.blank? | 168 | if @merge_request.commits.blank? |
166 | - # and if source target doesn't exist | ||
167 | - return invalid_mr unless @merge_request.target_project.repository.branch_names.include?(@merge_request.target_branch) | 169 | + # and if target branch doesn't exist |
170 | + return invalid_mr unless @merge_request.target_branch_exists? | ||
168 | 171 | ||
169 | - # or if source branch doesn't exist | ||
170 | - return invalid_mr unless @merge_request.source_project.repository.branch_names.include?(@merge_request.source_branch) | 172 | + # or if source branch doesn't exist |
173 | + return invalid_mr unless @merge_request.source_branch_exists? | ||
171 | end | 174 | end |
172 | end | 175 | end |
173 | 176 |
app/models/merge_request.rb
@@ -262,7 +262,7 @@ class MergeRequest < ActiveRecord::Base | @@ -262,7 +262,7 @@ class MergeRequest < ActiveRecord::Base | ||
262 | # Return the set of issues that will be closed if this merge request is accepted. | 262 | # Return the set of issues that will be closed if this merge request is accepted. |
263 | def closes_issues | 263 | def closes_issues |
264 | if target_branch == project.default_branch | 264 | if target_branch == project.default_branch |
265 | - unmerged_commits.map { |c| c.closes_issues(project) }.flatten.uniq.sort_by(&:id) | 265 | + commits.map { |c| c.closes_issues(project) }.flatten.uniq.sort_by(&:id) |
266 | else | 266 | else |
267 | [] | 267 | [] |
268 | end | 268 | end |
@@ -273,6 +273,34 @@ class MergeRequest < ActiveRecord::Base | @@ -273,6 +273,34 @@ class MergeRequest < ActiveRecord::Base | ||
273 | "merge request !#{iid}" | 273 | "merge request !#{iid}" |
274 | end | 274 | end |
275 | 275 | ||
276 | + def target_project_path | ||
277 | + if target_project | ||
278 | + target_project.path_with_namespace | ||
279 | + else | ||
280 | + "(removed)" | ||
281 | + end | ||
282 | + end | ||
283 | + | ||
284 | + def source_project_path | ||
285 | + if source_project | ||
286 | + source_project.path_with_namespace | ||
287 | + else | ||
288 | + "(removed)" | ||
289 | + end | ||
290 | + end | ||
291 | + | ||
292 | + def source_branch_exists? | ||
293 | + return false unless self.source_project | ||
294 | + | ||
295 | + self.source_project.repository.branch_names.include?(self.source_branch) | ||
296 | + end | ||
297 | + | ||
298 | + def target_branch_exists? | ||
299 | + return false unless self.target_project | ||
300 | + | ||
301 | + self.target_project.repository.branch_names.include?(self.target_branch) | ||
302 | + end | ||
303 | + | ||
276 | private | 304 | private |
277 | 305 | ||
278 | def dump_commits(commits) | 306 | def dump_commits(commits) |