diff options
| author | Ash McKenzie <amckenzie@gitlab.com> | 2018-07-25 13:19:59 +1000 |
|---|---|---|
| committer | Ash McKenzie <amckenzie@gitlab.com> | 2018-07-25 13:42:54 +1000 |
| commit | f4cde9911ac825b6a94a9cc62fbfb5f9a6818500 (patch) | |
| tree | e88196fd21973f89f5a3ed4982a52797ab9cbe96 | |
| parent | 7515c9d3c1659f43cf78eca3300244f1dd26219d (diff) | |
| download | gitlab-shell-f4cde9911ac825b6a94a9cc62fbfb5f9a6818500.tar.gz | |
Move User related logic into CurrentUserHelper
| -rw-r--r-- | lib/current_user_helper.rb | 9 | ||||
| -rw-r--r-- | lib/gitlab_shell.rb | 25 |
2 files changed, 13 insertions, 21 deletions
diff --git a/lib/current_user_helper.rb b/lib/current_user_helper.rb new file mode 100644 index 0000000..23e51d3 --- /dev/null +++ b/lib/current_user_helper.rb @@ -0,0 +1,9 @@ +module CurrentUserHelper + def username + @username ||= begin + api.discover(key_id)['username'] + rescue GitlabNet::ApiUnreachableError + 'Anonymous' + end + end +end diff --git a/lib/gitlab_shell.rb b/lib/gitlab_shell.rb index c1f3292..180e5eb 100644 --- a/lib/gitlab_shell.rb +++ b/lib/gitlab_shell.rb @@ -3,9 +3,11 @@ require 'pathname' require_relative 'gitlab_net' require_relative 'gitlab_metrics' +require_relative 'current_user_helper' require_relative 'api_command_helper' class GitlabShell # rubocop:disable Metrics/ClassLength + include CurrentUserHelper include APICommandHelper class AccessDeniedError < StandardError; end @@ -137,7 +139,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength 'repository' => @gitaly['repository'], 'gl_repository' => @gl_repository, 'gl_id' => key_id, - 'gl_username' => @username + 'gl_username' => username } args = [gitaly_address, JSON.dump(gitaly_request)] @@ -165,7 +167,7 @@ class GitlabShell # rubocop:disable Metrics/ClassLength 'GL_ID' => key_id, 'GL_PROTOCOL' => GL_PROTOCOL, 'GL_REPOSITORY' => @gl_repository, - 'GL_USERNAME' => @username + 'GL_USERNAME' => username } if @gitaly && @gitaly.include?('token') env['GITALY_TOKEN'] = @gitaly['token'] @@ -187,25 +189,6 @@ class GitlabShell # rubocop:disable Metrics/ClassLength GitlabNet.new end - def user - return @user if defined?(@user) - - begin - @user = api.discover(key_id) - rescue GitlabNet::ApiUnreachableError - @user = nil - end - end - - def username_from_discover - return nil unless user && user['username'] - - "@#{user['username']}" - end - - def username - @username ||= username_from_discover || 'Anonymous' - end # User identifier to be used in log messages. def log_username |
