2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
require 'gitlab/email/handler/base_handler'
|
2017-08-17 22:00:37 +05:30
|
|
|
require 'gitlab/email/handler/reply_processing'
|
2016-09-13 17:45:13 +05:30
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
# handles note/reply creation emails with these formats:
|
|
|
|
# incoming+1234567890abcdef1234567890abcdef@incoming.gitlab.com
|
2021-10-27 15:23:28 +05:30
|
|
|
# Quoted material is _not_ stripped but appended as a `details` section
|
2016-09-13 17:45:13 +05:30
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
class CreateNoteHandler < BaseHandler
|
2017-08-17 22:00:37 +05:30
|
|
|
include ReplyProcessing
|
|
|
|
|
|
|
|
delegate :project, to: :sent_notification, allow_nil: true
|
2018-10-15 14:42:47 +05:30
|
|
|
delegate :noteable, to: :sent_notification
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
def can_handle?
|
|
|
|
mail_key =~ /\A\w+\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
raise SentNotificationNotFoundError unless sent_notification
|
|
|
|
|
|
|
|
validate_permission!(:create_note)
|
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
validate_from_address!
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
raise NoteableNotFoundError unless noteable
|
2021-10-27 15:23:28 +05:30
|
|
|
raise EmptyEmailError if note_message.blank?
|
2016-09-13 17:45:13 +05:30
|
|
|
|
|
|
|
verify_record!(
|
|
|
|
record: create_note,
|
|
|
|
invalid_exception: InvalidNoteError,
|
|
|
|
record_name: 'comment')
|
|
|
|
end
|
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
def metrics_event
|
|
|
|
:receive_email_create_note
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
def author
|
|
|
|
sent_notification.recipient
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def sent_notification
|
|
|
|
@sent_notification ||= SentNotification.for(mail_key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_note
|
2021-10-27 15:23:28 +05:30
|
|
|
sent_notification.create_reply(note_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def note_message
|
|
|
|
return message unless sent_notification.noteable_type == "Issue"
|
|
|
|
|
|
|
|
message_with_appended_reply
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2022-04-04 11:22:00 +05:30
|
|
|
|
|
|
|
def from_address
|
|
|
|
mail.from&.first
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_from_address!
|
|
|
|
# Recipieint is always set to Support bot for ServiceDesk issues so we should exclude those.
|
|
|
|
return if author == User.support_bot
|
|
|
|
|
|
|
|
raise UserNotFoundError unless from_address && author.verified_email?(from_address)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|