2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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)
|
|
|
|
|
|
|
|
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-05-09 12:01:36 +05:30
|
|
|
validate_permission!(:create_merge_request_in)
|
|
|
|
validate_permission!(:create_merge_request_from)
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
verify_record!(
|
|
|
|
record: create_merge_request,
|
|
|
|
invalid_exception: InvalidMergeRequestError,
|
|
|
|
record_name: 'merge_request')
|
|
|
|
end
|
|
|
|
|
|
|
|
def author
|
|
|
|
@author ||= User.find_by(incoming_email_token: incoming_email_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
params = {
|
|
|
|
source_project_id: project.id,
|
|
|
|
source_branch: mail.subject,
|
|
|
|
target_project_id: project.id
|
|
|
|
}
|
|
|
|
params[:description] = message if message.present?
|
|
|
|
params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|