diff options
author | Stan Hu <stanhu@gmail.com> | 2015-10-15 01:41:46 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2015-10-15 23:54:13 -0700 |
commit | 2611d9f63cb6c22a00eccdac75535f93890b176c (patch) | |
tree | 2fd094664dc39103a7da10a2583572e22d7bb6c0 /app/services/merge_requests/refresh_service.rb | |
parent | bd3689e9e0aebe43f7c5f787e03a3bbaa8b2ef68 (diff) | |
download | gitlab-ce-2611d9f63cb6c22a00eccdac75535f93890b176c.tar.gz |
Add a system note and update relevant merge requests when a branch is deleted or re-added
If a branch is deleted with an open merge request, amended offline, and then pushed again,
GitLab doesn't bother to update the merge request even though the last commit ID and/or
code may have changed. This MR ensures that each push will update any relevant merge
requests and adds a system note if this happens as well.
Closes #2926
Diffstat (limited to 'app/services/merge_requests/refresh_service.rb')
-rw-r--r-- | app/services/merge_requests/refresh_service.rb | 55 |
1 files changed, 33 insertions, 22 deletions
diff --git a/app/services/merge_requests/refresh_service.rb b/app/services/merge_requests/refresh_service.rb index e903e48e3cd..802d02b0790 100644 --- a/app/services/merge_requests/refresh_service.rb +++ b/app/services/merge_requests/refresh_service.rb @@ -6,12 +6,22 @@ module MergeRequests @oldrev, @newrev = oldrev, newrev @branch_name = Gitlab::Git.ref_name(ref) @fork_merge_requests = @project.fork_merge_requests.opened - @commits = @project.repository.commits_between(oldrev, newrev) + @commits = [] + @merge_requests = merge_requests_for_branch + + # Leave a system note if a branch were deleted/added + if Gitlab::Git.blank_ref?(oldrev) or Gitlab::Git.blank_ref?(newrev) + presence = Gitlab::Git.blank_ref?(oldrev) ? 'added' : 'deleted' + comment_mr_branch_presence_changed(presence) + else + @commits = @project.repository.commits_between(oldrev, newrev) + + close_merge_requests + comment_mr_with_commits + end - close_merge_requests reload_merge_requests execute_mr_web_hooks - comment_mr_with_commits true end @@ -31,7 +41,6 @@ module MergeRequests commit_ids.include?(merge_request.last_commit.id) end - merge_requests.uniq.select(&:source_project).each do |merge_request| MergeRequests::PostMergeService. new(merge_request.target_project, @current_user). @@ -46,11 +55,7 @@ module MergeRequests # Refresh merge request diff if we push to source or target branch of merge request # Note: we should update merge requests from forks too def reload_merge_requests - merge_requests = @project.merge_requests.opened.by_branch(@branch_name).to_a - merge_requests += @fork_merge_requests.by_branch(@branch_name).to_a - merge_requests = filter_merge_requests(merge_requests) - - merge_requests.each do |merge_request| + @merge_requests.each do |merge_request| if merge_request.source_branch == @branch_name || force_push? merge_request.reload_code @@ -70,13 +75,20 @@ module MergeRequests end end - # Add comment about pushing new commits to merge requests - def comment_mr_with_commits - merge_requests = @project.origin_merge_requests.opened.where(source_branch: @branch_name).to_a - merge_requests += @fork_merge_requests.where(source_branch: @branch_name).to_a - merge_requests = filter_merge_requests(merge_requests) + # Add comment about branches being deleted or added to merge requests + def comment_mr_branch_presence_changed(presence) + merge_requests = merge_requests_for_branch merge_requests.each do |merge_request| + SystemNoteService.change_branch_presence( + merge_request, merge_request.project, @current_user, + 'source', @branch_name, presence) + end + end + + # Add comment about pushing new commits to merge requests + def comment_mr_with_commits + @merge_requests.each do |merge_request| mr_commit_ids = Set.new(merge_request.commits.map(&:id)) new_commits, existing_commits = @commits.partition do |commit| @@ -91,14 +103,7 @@ module MergeRequests # Call merge request webhook with update branches def execute_mr_web_hooks - merge_requests = @project.origin_merge_requests.opened - .where(source_branch: @branch_name) - .to_a - merge_requests += @fork_merge_requests.where(source_branch: @branch_name) - .to_a - merge_requests = filter_merge_requests(merge_requests) - - merge_requests.each do |merge_request| + @merge_requests.each do |merge_request| execute_hooks(merge_request, 'update') end end @@ -106,5 +111,11 @@ module MergeRequests def filter_merge_requests(merge_requests) merge_requests.uniq.select(&:source_project) end + + def merge_requests_for_branch + merge_requests = @project.origin_merge_requests.opened.where(source_branch: @branch_name).to_a + merge_requests += @fork_merge_requests.where(source_branch: @branch_name).to_a + filter_merge_requests(merge_requests) + end end end |