Commit b93744e3 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'merge-when-build-succeeds-unchecked' into 'master'

Get "Merge when build succeeds" to work when commits were pushed to MR
target branch while builds were running

The Merge when build succeeds service only merges when the MR is
mergeable (open, not WIP, no conflicts).

When the target branch is updated, all affected MRs have their merge
status set to `unchecked`, and the conflicts check will only happen
when `check_if_can_be_merged` is called, which happens when the MR page
is viewed.

When someone enables the automatic merge, the target branch is updated,
no-one views the MR page again, and the build succeeds, the mergeability
check will fail and the MR will not in fact be merged.

This MR makes sure `check_if_can_be_merged` is always called when MR
mergeability is checked.

See merge request !2304
parents 6865d3e5 c0849101
...@@ -23,6 +23,7 @@ v 8.4.0 (unreleased) ...@@ -23,6 +23,7 @@ v 8.4.0 (unreleased)
- Enable Microsoft Azure OAuth2 support (Janis Meybohm) - Enable Microsoft Azure OAuth2 support (Janis Meybohm)
v 8.3.3 (unreleased) v 8.3.3 (unreleased)
- Get "Merge when build succeeds" to work when commits were pushed to MR target branch while builds were running
- Fix project transfer e-mail sending incorrect paths in e-mail notification (Stan Hu) - Fix project transfer e-mail sending incorrect paths in e-mail notification (Stan Hu)
- Enable "Add key" button when user fills in a proper key (Stan Hu) - Enable "Add key" button when user fills in a proper key (Stan Hu)
......
...@@ -153,7 +153,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController ...@@ -153,7 +153,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
end end
def merge_check def merge_check
@merge_request.check_if_can_be_merged if @merge_request.unchecked? @merge_request.check_if_can_be_merged
render partial: "projects/merge_requests/widget/show.html.haml", layout: false render partial: "projects/merge_requests/widget/show.html.haml", layout: false
end end
......
...@@ -229,6 +229,8 @@ class MergeRequest < ActiveRecord::Base ...@@ -229,6 +229,8 @@ class MergeRequest < ActiveRecord::Base
end end
def check_if_can_be_merged def check_if_can_be_merged
return unless unchecked?
can_be_merged = can_be_merged =
project.repository.can_be_merged?(source_sha, target_branch) project.repository.can_be_merged?(source_sha, target_branch)
...@@ -252,7 +254,11 @@ class MergeRequest < ActiveRecord::Base ...@@ -252,7 +254,11 @@ class MergeRequest < ActiveRecord::Base
end end
def mergeable? def mergeable?
open? && !work_in_progress? && can_be_merged? return false unless open? && !work_in_progress?
check_if_can_be_merged
can_be_merged?
end end
def gitlab_merge_status def gitlab_merge_status
...@@ -452,6 +458,10 @@ class MergeRequest < ActiveRecord::Base ...@@ -452,6 +458,10 @@ class MergeRequest < ActiveRecord::Base
!source_branch_exists? || !target_branch_exists? !source_branch_exists? || !target_branch_exists?
end end
def broken?
self.commits.blank? || branch_missing? || cannot_be_merged?
end
def can_be_merged_by?(user) def can_be_merged_by?(user)
::Gitlab::GitAccess.new(user, project).can_push_to_branch?(target_branch) ::Gitlab::GitAccess.new(user, project).can_push_to_branch?(target_branch)
end end
...@@ -507,8 +517,4 @@ class MergeRequest < ActiveRecord::Base ...@@ -507,8 +517,4 @@ class MergeRequest < ActiveRecord::Base
def ci_commit def ci_commit
@ci_commit ||= source_project.ci_commit(last_commit.id) if last_commit && source_project @ci_commit ||= source_project.ci_commit(last_commit.id) if last_commit && source_project
end end
def broken?
self.commits.blank? || branch_missing? || cannot_be_merged?
end
end end
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
= render "projects/merge_requests/show/how_to_merge" = render "projects/merge_requests/show/how_to_merge"
= render "projects/merge_requests/widget/show.html.haml" = render "projects/merge_requests/widget/show.html.haml"
- if @merge_request.open? && @merge_request.source_branch_exists? && @merge_request.can_be_merged? && @merge_request.can_be_merged_by?(current_user) - if @merge_request.source_branch_exists? && @merge_request.mergeable? && @merge_request.can_be_merged_by?(current_user)
.light.prepend-top-default .light.prepend-top-default
You can also accept this merge request manually using the You can also accept this merge request manually using the
= succeed '.' do = succeed '.' do
......
...@@ -211,7 +211,7 @@ module API ...@@ -211,7 +211,7 @@ module API
unauthorized! unless merge_request.can_be_merged_by?(current_user) unauthorized! unless merge_request.can_be_merged_by?(current_user)
not_allowed! if !merge_request.open? || merge_request.work_in_progress? not_allowed! if !merge_request.open? || merge_request.work_in_progress?
merge_request.check_if_can_be_merged if merge_request.unchecked? merge_request.check_if_can_be_merged
render_api_error!('Branch cannot be merged', 406) unless merge_request.can_be_merged? render_api_error!('Branch cannot be merged', 406) unless merge_request.can_be_merged?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment