Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2018-09-06 14:23:22 +0300
committerDouwe Maan <douwe@gitlab.com>2018-09-06 14:23:22 +0300
commit98ae35a8b08c86f7a380aecfb967f092cc1e0ef0 (patch)
tree251cf64c3094839ae002b721098a89846aeeba64 /lib
parent4ad792dede4d9a68df6b4797a72c46f65bdcaccd (diff)
parentc826ecc3e76917d7b77701551e25425da9274b2e (diff)
Merge branch 'bvl-codeowners-file-ce' into 'master'
Port changes for CODEOWNERS to CE See merge request gitlab-org/gitlab-ce!21309
Diffstat (limited to 'lib')
-rw-r--r--lib/gitlab/user_extractor.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/gitlab/user_extractor.rb b/lib/gitlab/user_extractor.rb
new file mode 100644
index 00000000000..3ede0a5b5e6
--- /dev/null
+++ b/lib/gitlab/user_extractor.rb
@@ -0,0 +1,53 @@
+# frozen_string_literal: true
+
+# This class extracts all users found in a piece of text by the username or the
+# email adress
+
+module Gitlab
+ class UserExtractor
+ # Not using `Devise.email_regexp` to filter out any chars that an email
+ # does not end with and not pinning the email to a start of end of a string.
+ EMAIL_REGEXP = /(?<email>([^@\s]+@[^@\s]+(?<!\W)))/
+ USERNAME_REGEXP = User.reference_pattern
+
+ def initialize(text)
+ @text = text
+ end
+
+ def users
+ return User.none unless @text.present?
+
+ @users ||= User.from("(#{union.to_sql}) users")
+ end
+
+ def usernames
+ matches[:usernames]
+ end
+
+ def emails
+ matches[:emails]
+ end
+
+ def references
+ @references ||= matches.values.flatten
+ end
+
+ def matches
+ @matches ||= {
+ emails: @text.scan(EMAIL_REGEXP).flatten.uniq,
+ usernames: @text.scan(USERNAME_REGEXP).flatten.uniq
+ }
+ end
+
+ private
+
+ def union
+ relations = []
+
+ relations << User.by_any_email(emails) if emails.any?
+ relations << User.by_username(usernames) if usernames.any?
+
+ Gitlab::SQL::Union.new(relations)
+ end
+ end
+end