summaryrefslogtreecommitdiff
path: root/lib/gitlab/private_commit_email.rb
blob: 886c2c32d360ece33a3646fb7cbffb141f3b65a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

module Gitlab
  module PrivateCommitEmail
    TOKEN = "_private"

    class << self
      def regex
        hostname_regexp = Regexp.escape(Gitlab::CurrentSettings.current_application_settings.commit_email_hostname)

        /\A(?<id>([0-9]+))\-([^@]+)@#{hostname_regexp}\z/
      end

      def user_id_for_email(email)
        match = email&.match(regex)
        return unless match

        match[:id].to_i
      end

      def user_ids_for_emails(emails)
        emails.map { |email| user_id_for_email(email) }.compact.uniq
      end

      def for_user(user)
        hostname = Gitlab::CurrentSettings.current_application_settings.commit_email_hostname

        "#{user.id}-#{user.username}@#{hostname}"
      end
    end
  end
end