1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# frozen_string_literal: true
module MergeRequests
class PushOptionsHandlerService
Error = Class.new(StandardError)
LIMIT = 10
attr_reader :branches, :changes_by_branch, :current_user, :errors,
:project, :push_options, :target
def initialize(project, current_user, changes, push_options)
@project = project
@current_user = current_user
@changes_by_branch = parse_changes(changes)
@push_options = push_options
@errors = []
@branches = @changes_by_branch.keys
@target = @push_options[:target] || @project.default_branch
raise Error, 'User is required' if @current_user.nil?
unless @project.merge_requests_enabled?
raise Error, 'Merge requests are not enabled for project'
end
if @branches.size > LIMIT
raise Error, "Too many branches pushed (#{@branches.size} were pushed, limit is #{LIMIT})"
end
if @push_options[:target] && !@project.repository.branch_exists?(@target)
raise Error, "Branch #{@target} does not exist"
end
end
def execute
branches.each do |branch|
execute_for_branch(branch)
end
self
end
private
# Parses changes in the push.
# Returns a hash of branch => changes_list
def parse_changes(raw_changes)
Gitlab::ChangesList.new(raw_changes).each_with_object({}) do |change, changes|
next unless Gitlab::Git.branch_ref?(change[:ref])
# Deleted branch
next if Gitlab::Git.blank_ref?(change[:newrev])
# Default branch
branch_name = Gitlab::Git.branch_name(change[:ref])
next if branch_name == project.default_branch
changes[branch_name] = change
end
end
def merge_requests
@merge_requests ||= MergeRequest.from_project(@project)
.opened
.from_source_branches(@branches)
.to_a # fetch now
end
def execute_for_branch(branch)
merge_request = merge_requests.find { |mr| mr.source_branch == branch }
if merge_request
update!(merge_request)
else
create!(branch)
end
end
def create!(branch)
unless push_options[:create]
errors << "A merge_request.create push option is required to create a merge request for branch #{branch}"
return
end
merge_request = ::MergeRequests::CreateService.new(
project,
current_user,
create_params(branch)
).execute
collect_errors_from_merge_request(merge_request) unless merge_request.persisted?
end
def update!(merge_request)
return if target == merge_request.target_branch
merge_request = ::MergeRequests::UpdateService.new(
project,
current_user,
{ target_branch: target }
).execute(merge_request)
collect_errors_from_merge_request(merge_request) unless merge_request.valid?
end
def create_params(branch)
change = changes_by_branch.fetch(branch)
commits = project.repository.commits_between(project.default_branch, change[:newrev])
commits = CommitCollection.new(project, commits)
commit = commits.without_merge_commits.first
{
assignee: current_user,
source_branch: branch,
target_branch: target,
title: commit&.title&.strip || 'New Merge Request',
description: commit&.description&.strip
}
end
def collect_errors_from_merge_request(merge_request)
errors << merge_request.errors.full_messages.to_sentence
end
end
end
|