2014-09-02 18:07:02 +05:30
|
|
|
# Detect user based on identifier like
|
|
|
|
# key-13 or user-36 or last commit
|
|
|
|
module Gitlab
|
|
|
|
module Identifier
|
2018-03-17 18:26:18 +05:30
|
|
|
def identify(identifier, project = nil, newrev = nil)
|
2014-09-02 18:07:02 +05:30
|
|
|
if identifier.blank?
|
2016-11-03 12:29:30 +05:30
|
|
|
identify_using_commit(project, newrev)
|
2014-09-02 18:07:02 +05:30
|
|
|
elsif identifier =~ /\Auser-\d+\Z/
|
|
|
|
# git push over http
|
2016-11-03 12:29:30 +05:30
|
|
|
identify_using_user(identifier)
|
2014-09-02 18:07:02 +05:30
|
|
|
elsif identifier =~ /\Akey-\d+\Z/
|
|
|
|
# git push over ssh
|
2016-11-03 12:29:30 +05:30
|
|
|
identify_using_ssh_key(identifier)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tries to identify a user based on a commit SHA.
|
|
|
|
def identify_using_commit(project, ref)
|
2018-03-17 18:26:18 +05:30
|
|
|
return if project.nil? && ref.nil?
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
commit = project.commit(ref)
|
|
|
|
|
|
|
|
return if !commit || !commit.author_email
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
identify_with_cache(:email, commit.author_email) do
|
|
|
|
commit.author
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|
2016-11-03 12:29:30 +05:30
|
|
|
|
|
|
|
# Tries to identify a user based on a user identifier (e.g. "user-123").
|
|
|
|
def identify_using_user(identifier)
|
|
|
|
user_id = identifier.gsub("user-", "")
|
|
|
|
|
|
|
|
identify_with_cache(:user, user_id) do
|
|
|
|
User.find_by(id: user_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Tries to identify a user based on an SSH key identifier (e.g. "key-123").
|
|
|
|
def identify_using_ssh_key(identifier)
|
|
|
|
key_id = identifier.gsub("key-", "")
|
|
|
|
|
|
|
|
identify_with_cache(:ssh_key, key_id) do
|
|
|
|
User.find_by_ssh_key_id(key_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def identify_with_cache(category, key)
|
|
|
|
if identification_cache[category].key?(key)
|
|
|
|
identification_cache[category][key]
|
|
|
|
else
|
|
|
|
identification_cache[category][key] = yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def identification_cache
|
|
|
|
@identification_cache ||= {
|
|
|
|
email: {},
|
|
|
|
user: {},
|
|
|
|
ssh_key: {}
|
|
|
|
}
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
end
|