diff options
author | Alejandro RodrÃguez <alejorro70@gmail.com> | 2017-04-28 17:10:42 -0300 |
---|---|---|
committer | Alejandro RodrÃguez <alejorro70@gmail.com> | 2017-05-11 12:21:31 -0300 |
commit | ee259653e7a00359740ca36cef606f9c3cc1a7cb (patch) | |
tree | 6f53403454defd386c4455a6a9ffcf052d4c6695 /lib | |
parent | 1cf14770f3bf5c3255f4c3a8f69ccfab74317fcd (diff) | |
download | gitlab-shell-ee259653e7a00359740ca36cef606f9c3cc1a7cb.tar.gz |
Handle GL_REPOSITORY env variable and use it in api calls
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab_access.rb | 7 | ||||
-rw-r--r-- | lib/gitlab_access_status.rb | 7 | ||||
-rw-r--r-- | lib/gitlab_net.rb | 17 | ||||
-rw-r--r-- | lib/gitlab_post_receive.rb | 9 | ||||
-rw-r--r-- | lib/gitlab_shell.rb | 8 |
5 files changed, 29 insertions, 19 deletions
diff --git a/lib/gitlab_access.rb b/lib/gitlab_access.rb index 1b2a065..3cdeb3e 100644 --- a/lib/gitlab_access.rb +++ b/lib/gitlab_access.rb @@ -10,10 +10,11 @@ class GitlabAccess include NamesHelper - attr_reader :config, :repo_path, :changes, :protocol + attr_reader :config, :gl_repository, :repo_path, :changes, :protocol - def initialize(repo_path, actor, changes, protocol) + def initialize(gl_repository, repo_path, actor, changes, protocol) @config = GitlabConfig.new + @gl_repository = gl_repository @repo_path = repo_path.strip @actor = actor @changes = changes.lines @@ -27,7 +28,7 @@ class GitlabAccess "GIT_OBJECT_DIRECTORY" => ENV["GIT_OBJECT_DIRECTORY"] } - api.check_access('git-receive-pack', @repo_path, @actor, @changes, @protocol, env: env.to_json) + api.check_access('git-receive-pack', @gl_repository, @repo_path, @actor, @changes, @protocol, env: env.to_json) end raise AccessDeniedError, status.message unless status.allowed? diff --git a/lib/gitlab_access_status.rb b/lib/gitlab_access_status.rb index 7fb88be..6a906f1 100644 --- a/lib/gitlab_access_status.rb +++ b/lib/gitlab_access_status.rb @@ -1,17 +1,18 @@ require 'json' class GitAccessStatus - attr_reader :message, :repository_path + attr_reader :message, :gl_repository, :repository_path - def initialize(status, message, repository_path) + def initialize(status, message, gl_repository, repository_path) @status = status @message = message + @gl_repository = gl_repository @repository_path = repository_path end def self.create_from_json(json) values = JSON.parse(json) - self.new(values["status"], values["message"], values["repository_path"]) + self.new(values["status"], values["message"], values["gl_repository"], values["repository_path"]) end def allowed? diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index 438c626..1e60e24 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -15,17 +15,19 @@ class GitlabNet CHECK_TIMEOUT = 5 READ_TIMEOUT = 300 - def check_access(cmd, repo, actor, changes, protocol, env: {}) + def check_access(cmd, gl_repository, repo, actor, changes, protocol, env: {}) changes = changes.join("\n") unless changes.kind_of?(String) params = { action: cmd, changes: changes, + gl_repository: gl_repository, project: sanitize_path(repo), protocol: protocol, env: env } + if actor =~ /\Akey\-\d+\Z/ params.merge!(key_id: actor.gsub("key-", "")) elsif actor =~ /\Auser\-\d+\Z/ @@ -38,7 +40,7 @@ class GitlabNet if resp.code == '200' GitAccessStatus.create_from_json(resp.body) else - GitAccessStatus.new(false, 'API is not accessible', nil) + GitAccessStatus.new(false, 'API is not accessible', nil, nil) end end @@ -66,10 +68,12 @@ class GitlabNet JSON.parse(resp.body) rescue {} end - def merge_request_urls(repo_path, changes) + def merge_request_urls(gl_repository, repo_path, changes) changes = changes.join("\n") unless changes.kind_of?(String) changes = changes.encode('UTF-8', 'ASCII', invalid: :replace, replace: '') - resp = get("#{host_v3}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}") + url = "#{host_v3}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}" + url += "&gl_repository=#{URI.escape(gl_repository)}" if gl_repository + resp = get(url) JSON.parse(resp.body) rescue [] end @@ -93,8 +97,9 @@ class GitlabNet {} end - def notify_post_receive(repo_path) - resp = post("#{host}/notify_post_receive", repo_path: repo_path) + def notify_post_receive(gl_repository, repo_path) + params = { gl_repository: gl_repository, project: repo_path } + resp = post("#{host}/notify_post_receive", params) resp.code == '200' rescue diff --git a/lib/gitlab_post_receive.rb b/lib/gitlab_post_receive.rb index 8383135..403d281 100644 --- a/lib/gitlab_post_receive.rb +++ b/lib/gitlab_post_receive.rb @@ -9,10 +9,11 @@ require 'securerandom' class GitlabPostReceive include NamesHelper - attr_reader :config, :repo_path, :changes, :jid + attr_reader :config, :gl_repository, :repo_path, :changes, :jid - def initialize(repo_path, actor, changes) + def initialize(gl_repository, repo_path, actor, changes) @config = GitlabConfig.new + @gl_repository = gl_repository @repo_path, @actor = repo_path.strip, actor @changes = changes @jid = SecureRandom.hex(12) @@ -32,11 +33,11 @@ class GitlabPostReceive end merge_request_urls = GitlabMetrics.measure("merge-request-urls") do - api.merge_request_urls(@repo_path, @changes) + api.merge_request_urls(@gl_repository, @repo_path, @changes) end print_merge_request_links(merge_request_urls) - api.notify_post_receive(repo_path) + api.notify_post_receive(gl_repository, repo_path) rescue GitlabNet::ApiUnreachableError nil end diff --git a/lib/gitlab_shell.rb b/lib/gitlab_shell.rb index f2e7465..355d493 100644 --- a/lib/gitlab_shell.rb +++ b/lib/gitlab_shell.rb @@ -13,7 +13,7 @@ class GitlabShell API_COMMANDS = %w(2fa_recovery_codes) GL_PROTOCOL = 'ssh'.freeze - attr_accessor :key_id, :repo_name, :command, :git_access + attr_accessor :key_id, :gl_repository, :repo_name, :command, :git_access attr_reader :repo_path def initialize(key_id) @@ -89,11 +89,12 @@ class GitlabShell end def verify_access - status = api.check_access(@git_access, @repo_name, @key_id, '_any', GL_PROTOCOL) + status = api.check_access(@git_access, nil, @repo_name, @key_id, '_any', GL_PROTOCOL) raise AccessDeniedError, status.message unless status.allowed? self.repo_path = status.repository_path + @gl_repository = status.gl_repository end def process_cmd(args) @@ -125,7 +126,8 @@ class GitlabShell 'LD_LIBRARY_PATH' => ENV['LD_LIBRARY_PATH'], 'LANG' => ENV['LANG'], 'GL_ID' => @key_id, - 'GL_PROTOCOL' => GL_PROTOCOL + 'GL_PROTOCOL' => GL_PROTOCOL, + 'GL_REPOSITORY' => @gl_repository } if git_trace_available? |