gitlab-org--gitlab-foss/lib/gitlab/incoming_email.rb

69 lines
1.7 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2015-08-18 20:02:26 -04:00
module Gitlab
module IncomingEmail
UNSUBSCRIBE_SUFFIX = '-unsubscribe'
UNSUBSCRIBE_SUFFIX_LEGACY = '+unsubscribe'
WILDCARD_PLACEHOLDER = '%{key}'
2015-08-18 20:02:26 -04:00
class << self
def enabled?
config.enabled && config.address
2015-08-18 20:02:26 -04:00
end
def supports_wildcard?
config.address && config.address.include?(WILDCARD_PLACEHOLDER)
end
def supports_issue_creation?
enabled? && supports_wildcard?
end
def reply_address(key)
config.address.sub(WILDCARD_PLACEHOLDER, key)
end
# example: incoming+1234567890abcdef1234567890abcdef-unsubscribe@incoming.gitlab.com
def unsubscribe_address(key)
config.address.sub(WILDCARD_PLACEHOLDER, "#{key}#{UNSUBSCRIBE_SUFFIX}")
2015-08-18 20:02:26 -04:00
end
def key_from_address(address, wildcard_address: nil)
wildcard_address ||= config.address
regex = address_regex(wildcard_address)
2015-08-20 19:44:44 -04:00
return unless regex
2015-08-18 20:02:26 -04:00
2015-08-20 19:44:44 -04:00
match = address.match(regex)
2015-08-18 20:02:26 -04:00
return unless match
match[1]
end
def key_from_fallback_message_id(mail_id)
message_id_regexp = /\Areply\-(.+)@#{Gitlab.config.gitlab.host}\z/
mail_id[message_id_regexp, 1]
end
def scan_fallback_references(references)
# It's looking for each <...>
references.scan(/(?!<)[^<>]+(?=>)/)
end
2015-08-18 20:02:26 -04:00
def config
Gitlab.config.incoming_email
2015-08-18 20:02:26 -04:00
end
private
def address_regex(wildcard_address)
return unless wildcard_address
2015-08-20 19:21:31 -04:00
regex = Regexp.escape(wildcard_address)
regex = regex.sub(Regexp.escape(WILDCARD_PLACEHOLDER), '(.+)')
Regexp.new(/\A<?#{regex}>?\z/).freeze
2015-08-18 20:02:26 -04:00
end
end
end
end