Class: Gitlab::Email::Handler::CreateMergeRequestHandler

Inherits:
BaseHandler
  • Object
show all
Includes:
ReplyProcessing
Defined in:
lib/gitlab/email/handler/create_merge_request_handler.rb

Constant Summary collapse

HANDLER_REGEX =
/\A#{HANDLER_ACTION_BASE_REGEX}-(?<incoming_email_token>.+)-merge-request\z/.freeze
HANDLER_REGEX_LEGACY =
/\A(?<project_path>[^\+]*)\+merge-request\+(?<incoming_email_token>.*)/.freeze

Instance Attribute Summary

Attributes inherited from BaseHandler

#mail, #mail_key

Instance Method Summary collapse

Constructor Details

#initialize(mail, mail_key) ⇒ CreateMergeRequestHandler

Returns a new instance of CreateMergeRequestHandler.


18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gitlab/email/handler/create_merge_request_handler.rb', line 18

def initialize(mail, mail_key)
  super(mail, mail_key)

  if !mail_key&.include?('/') && (matched = HANDLER_REGEX.match(mail_key.to_s))
    @project_slug         = matched[:project_slug]
    @project_id           = matched[:project_id]&.to_i
    @incoming_email_token = matched[:incoming_email_token]
  elsif matched = HANDLER_REGEX_LEGACY.match(mail_key.to_s)
    @project_path         = matched[:project_path]
    @incoming_email_token = matched[:incoming_email_token]
  end
end

Instance Method Details

#authorObject

rubocop: disable CodeReuse/ActiveRecord


48
49
50
# File 'lib/gitlab/email/handler/create_merge_request_handler.rb', line 48

def author
  @author ||= User.find_by(incoming_email_token: incoming_email_token)
end

#can_handle?Boolean

Returns:

  • (Boolean)

31
32
33
# File 'lib/gitlab/email/handler/create_merge_request_handler.rb', line 31

def can_handle?
  incoming_email_token && (project_id || project_path)
end

#executeObject

Raises:


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gitlab/email/handler/create_merge_request_handler.rb', line 35

def execute
  raise ProjectNotFound unless project

  validate_permission!(:create_merge_request_in)
  validate_permission!(:create_merge_request_from)

  verify_record!(
    record: create_merge_request,
    invalid_exception: InvalidMergeRequestError,
    record_name: 'merge_request')
end

#metrics_eventObject


57
58
59
# File 'lib/gitlab/email/handler/create_merge_request_handler.rb', line 57

def metrics_event
  :receive_email_create_merge_request
end

#metrics_paramsObject

rubocop: enable CodeReuse/ActiveRecord


53
54
55
# File 'lib/gitlab/email/handler/create_merge_request_handler.rb', line 53

def metrics_params
  super.merge(includes_patches: patch_attachments.any?)
end