2014-11-11 14:49:26 +00:00
|
|
|
module MergeRequests
|
|
|
|
class RefreshService < MergeRequests::BaseService
|
|
|
|
def execute(oldrev, newrev, ref)
|
2015-03-10 10:51:36 +00:00
|
|
|
return true unless Gitlab::Git.branch_ref?(ref)
|
2014-11-11 14:49:26 +00:00
|
|
|
|
2014-12-02 15:42:56 +00:00
|
|
|
@oldrev, @newrev = oldrev, newrev
|
2015-03-10 10:51:36 +00:00
|
|
|
@branch_name = Gitlab::Git.ref_name(ref)
|
2014-11-11 14:49:26 +00:00
|
|
|
@fork_merge_requests = @project.fork_merge_requests.opened
|
2015-10-15 08:41:46 +00:00
|
|
|
@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
|
2014-11-11 14:49:26 +00:00
|
|
|
|
|
|
|
reload_merge_requests
|
2015-02-26 08:17:25 +00:00
|
|
|
execute_mr_web_hooks
|
2014-11-11 14:49:26 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Collect open merge requests that target same branch we push into
|
|
|
|
# and close if push to master include last commit from merge request
|
|
|
|
# We need this to close(as merged) merge requests that were merged into
|
|
|
|
# target branch manually
|
|
|
|
def close_merge_requests
|
|
|
|
commit_ids = @commits.map(&:id)
|
|
|
|
merge_requests = @project.merge_requests.opened.where(target_branch: @branch_name).to_a
|
|
|
|
merge_requests = merge_requests.select(&:last_commit)
|
|
|
|
|
|
|
|
merge_requests = merge_requests.select do |merge_request|
|
|
|
|
commit_ids.include?(merge_request.last_commit.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
merge_requests.uniq.select(&:source_project).each do |merge_request|
|
2015-08-11 12:33:31 +00:00
|
|
|
MergeRequests::PostMergeService.
|
2015-01-07 00:24:47 +00:00
|
|
|
new(merge_request.target_project, @current_user).
|
2015-08-11 12:33:31 +00:00
|
|
|
execute(merge_request)
|
2014-11-11 14:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-02 15:42:56 +00:00
|
|
|
def force_push?
|
|
|
|
Gitlab::ForcePushCheck.force_push?(@project, @oldrev, @newrev)
|
|
|
|
end
|
|
|
|
|
2014-11-11 14:49:26 +00:00
|
|
|
# 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
|
2015-10-15 08:57:45 +00:00
|
|
|
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|
|
2014-12-02 14:22:23 +00:00
|
|
|
|
2014-12-02 15:42:56 +00:00
|
|
|
if merge_request.source_branch == @branch_name || force_push?
|
2014-12-02 14:22:23 +00:00
|
|
|
merge_request.reload_code
|
2015-03-23 16:30:19 +00:00
|
|
|
merge_request.mark_as_unchecked
|
2014-12-02 14:22:23 +00:00
|
|
|
else
|
|
|
|
mr_commit_ids = merge_request.commits.map(&:id)
|
|
|
|
push_commit_ids = @commits.map(&:id)
|
|
|
|
matches = mr_commit_ids & push_commit_ids
|
|
|
|
|
|
|
|
if matches.any?
|
|
|
|
merge_request.reload_code
|
2015-03-23 16:30:19 +00:00
|
|
|
merge_request.mark_as_unchecked
|
2014-12-02 14:22:23 +00:00
|
|
|
else
|
2015-03-23 16:30:19 +00:00
|
|
|
merge_request.mark_as_unchecked
|
2014-12-02 14:22:23 +00:00
|
|
|
end
|
|
|
|
end
|
2014-11-11 14:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-15 08:41:46 +00:00
|
|
|
# Add comment about branches being deleted or added to merge requests
|
|
|
|
def comment_mr_branch_presence_changed(presence)
|
|
|
|
merge_requests = merge_requests_for_branch
|
2014-11-11 14:49:26 +00:00
|
|
|
|
|
|
|
merge_requests.each do |merge_request|
|
2015-10-15 08:41:46 +00:00
|
|
|
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|
|
2015-03-06 16:14:19 +00:00
|
|
|
mr_commit_ids = Set.new(merge_request.commits.map(&:id))
|
|
|
|
|
|
|
|
new_commits, existing_commits = @commits.partition do |commit|
|
|
|
|
mr_commit_ids.include?(commit.id)
|
|
|
|
end
|
|
|
|
|
2015-05-09 22:18:50 +00:00
|
|
|
SystemNoteService.add_commits(merge_request, merge_request.project,
|
|
|
|
@current_user, new_commits,
|
|
|
|
existing_commits, @oldrev)
|
2014-11-11 14:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-26 08:17:25 +00:00
|
|
|
# Call merge request webhook with update branches
|
|
|
|
def execute_mr_web_hooks
|
2015-10-15 08:41:46 +00:00
|
|
|
@merge_requests.each do |merge_request|
|
2015-02-26 08:17:25 +00:00
|
|
|
execute_hooks(merge_request, 'update')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-11 14:49:26 +00:00
|
|
|
def filter_merge_requests(merge_requests)
|
|
|
|
merge_requests.uniq.select(&:source_project)
|
|
|
|
end
|
2015-10-15 08:41:46 +00:00
|
|
|
|
|
|
|
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
|
2014-11-11 14:49:26 +00:00
|
|
|
end
|
|
|
|
end
|