2018-08-23 09:22:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-24 06:20:36 +00:00
|
|
|
require 'gitlab/email/handler/base_handler'
|
|
|
|
require 'gitlab/email/handler/reply_processing'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
class CreateMergeRequestHandler < BaseHandler
|
|
|
|
include ReplyProcessing
|
|
|
|
attr_reader :project_path, :incoming_email_token
|
|
|
|
|
|
|
|
def initialize(mail, mail_key)
|
|
|
|
super(mail, mail_key)
|
2018-01-11 16:34:01 +00:00
|
|
|
|
2017-08-24 06:20:36 +00:00
|
|
|
if m = /\A([^\+]*)\+merge-request\+(.*)/.match(mail_key.to_s)
|
|
|
|
@project_path, @incoming_email_token = m.captures
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_handle?
|
|
|
|
@project_path && @incoming_email_token
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
raise ProjectNotFound unless project
|
|
|
|
|
2018-04-06 17:58:53 +00:00
|
|
|
validate_permission!(:create_merge_request_in)
|
|
|
|
validate_permission!(:create_merge_request_from)
|
2017-08-24 06:20:36 +00:00
|
|
|
|
|
|
|
verify_record!(
|
|
|
|
record: create_merge_request,
|
|
|
|
invalid_exception: InvalidMergeRequestError,
|
|
|
|
record_name: 'merge_request')
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-24 06:20:36 +00:00
|
|
|
def author
|
|
|
|
@author ||= User.find_by(incoming_email_token: incoming_email_token)
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-24 06:20:36 +00:00
|
|
|
|
|
|
|
def project
|
|
|
|
@project ||= Project.find_by_full_path(project_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_merge_request
|
|
|
|
merge_request = MergeRequests::BuildService.new(project, author, merge_request_params).execute
|
|
|
|
|
|
|
|
if merge_request.errors.any?
|
|
|
|
merge_request
|
|
|
|
else
|
|
|
|
MergeRequests::CreateService.new(project, author).create(merge_request)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_request_params
|
2017-12-11 09:14:11 +00:00
|
|
|
params = {
|
2017-08-24 06:20:36 +00:00
|
|
|
source_project_id: project.id,
|
|
|
|
source_branch: mail.subject,
|
|
|
|
target_project_id: project.id
|
|
|
|
}
|
2017-12-11 09:14:11 +00:00
|
|
|
params[:description] = message if message.present?
|
|
|
|
params
|
2017-08-24 06:20:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|