Commit 6c3459978dff210af0066307f76800956cbec5a8

Authored by Dmitriy Zaporozhets
1 parent 5a4386a4

Add new methods to MR to check if source or target branch exists

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/controllers/projects/merge_requests_controller.rb
... ... @@ -160,14 +160,17 @@ class Projects::MergeRequestsController &lt; Projects::ApplicationController
160 160 end
161 161  
162 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 166 # Show git not found page
164 167 # if there is no saved commits between source & target branch
165 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 174 end
172 175 end
173 176  
... ...
app/models/merge_request.rb
... ... @@ -262,7 +262,7 @@ class MergeRequest &lt; ActiveRecord::Base
262 262 # Return the set of issues that will be closed if this merge request is accepted.
263 263 def closes_issues
264 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 266 else
267 267 []
268 268 end
... ... @@ -273,6 +273,34 @@ class MergeRequest &lt; ActiveRecord::Base
273 273 "merge request !#{iid}"
274 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 304 private
277 305  
278 306 def dump_commits(commits)
... ...