diff options
author | Jacob Vosmaer <jacob@gitlab.com> | 2020-03-04 00:58:25 +0300 |
---|---|---|
committer | James Fargher <proglottis@gmail.com> | 2020-03-04 00:58:25 +0300 |
commit | d80aa7f06043ceec91a53cfd176d3828ed971b15 (patch) | |
tree | 73b9589e8e19578237114ea8163d332d5f89d437 | |
parent | 4a252d129526874e33e10ce365430b7134e11bcf (diff) |
Clarify flow of GL_ID in Ruby Git hooks
-rwxr-xr-x | ruby/gitlab-shell/hooks/post-receive | 6 | ||||
-rwxr-xr-x | ruby/gitlab-shell/hooks/pre-receive | 6 | ||||
-rwxr-xr-x | ruby/gitlab-shell/hooks/update | 4 | ||||
-rw-r--r-- | ruby/gitlab-shell/lib/gitlab_access.rb | 6 | ||||
-rw-r--r-- | ruby/gitlab-shell/lib/gitlab_custom_hook.rb | 4 | ||||
-rw-r--r-- | ruby/gitlab-shell/lib/gitlab_net.rb | 36 | ||||
-rw-r--r-- | ruby/gitlab-shell/lib/gitlab_post_receive.rb | 6 | ||||
-rw-r--r-- | ruby/gitlab-shell/spec/gitlab_post_receive_spec.rb | 4 |
8 files changed, 36 insertions, 36 deletions
diff --git a/ruby/gitlab-shell/hooks/post-receive b/ruby/gitlab-shell/hooks/post-receive index 2b6538f03..384e8a229 100755 --- a/ruby/gitlab-shell/hooks/post-receive +++ b/ruby/gitlab-shell/hooks/post-receive @@ -4,7 +4,7 @@ # will be processed properly. refs = $stdin.read -key_id = ENV.delete('GL_ID') +gl_id = ENV.delete('GL_ID') gl_repository = ENV['GL_REPOSITORY'] repo_path = Dir.pwd @@ -14,8 +14,8 @@ require_relative '../lib/gitlab_post_receive' push_options = HooksUtils.get_push_options -if GitlabPostReceive.new(gl_repository, repo_path, key_id, refs, push_options).exec && - GitlabCustomHook.new(repo_path, key_id).post_receive(refs) +if GitlabPostReceive.new(gl_repository, repo_path, gl_id, refs, push_options).exec && + GitlabCustomHook.new(repo_path, gl_id).post_receive(refs) exit 0 else exit 1 diff --git a/ruby/gitlab-shell/hooks/pre-receive b/ruby/gitlab-shell/hooks/pre-receive index 6ce587951..66c61d98c 100755 --- a/ruby/gitlab-shell/hooks/pre-receive +++ b/ruby/gitlab-shell/hooks/pre-receive @@ -4,7 +4,7 @@ # will be processed properly. refs = $stdin.read -key_id = ENV.delete('GL_ID') +gl_id = ENV.delete('GL_ID') protocol = ENV.delete('GL_PROTOCOL') repo_path = Dir.pwd gl_repository = ENV['GL_REPOSITORY'] @@ -23,8 +23,8 @@ require_relative '../lib/gitlab_net' # last so that it only runs if everything else succeeded. On post-receive on the # other hand, we run GitlabPostReceive first because the push is already done # and we don't want to skip it if the custom hook fails. -if GitlabAccess.new(gl_repository, repo_path, key_id, refs, protocol).exec && - GitlabCustomHook.new(repo_path, key_id).pre_receive(refs) && +if GitlabAccess.new(gl_repository, repo_path, gl_id, refs, protocol).exec && + GitlabCustomHook.new(repo_path, gl_id).pre_receive(refs) && increase_reference_counter(gl_repository, repo_path) exit 0 else diff --git a/ruby/gitlab-shell/hooks/update b/ruby/gitlab-shell/hooks/update index 4c2fc08b0..e723754d4 100755 --- a/ruby/gitlab-shell/hooks/update +++ b/ruby/gitlab-shell/hooks/update @@ -7,11 +7,11 @@ ref_name = ARGV[0] old_value = ARGV[1] new_value = ARGV[2] repo_path = Dir.pwd -key_id = ENV.delete('GL_ID') +gl_id = ENV.delete('GL_ID') require_relative '../lib/gitlab_custom_hook' -if GitlabCustomHook.new(repo_path, key_id).update(ref_name, old_value, new_value) +if GitlabCustomHook.new(repo_path, gl_id).update(ref_name, old_value, new_value) exit 0 else exit 1 diff --git a/ruby/gitlab-shell/lib/gitlab_access.rb b/ruby/gitlab-shell/lib/gitlab_access.rb index 72abd14d9..caeb3fb39 100644 --- a/ruby/gitlab-shell/lib/gitlab_access.rb +++ b/ruby/gitlab-shell/lib/gitlab_access.rb @@ -10,18 +10,18 @@ class GitlabAccess attr_reader :config, :gl_repository, :repo_path, :changes, :protocol - def initialize(gl_repository, repo_path, actor, changes, protocol) + def initialize(gl_repository, repo_path, gl_id, changes, protocol) @config = GitlabConfig.new @gl_repository = gl_repository @repo_path = repo_path.strip - @actor = actor + @gl_id = gl_id @changes = changes.lines @protocol = protocol end def exec status = GitlabMetrics.measure('check-access:git-receive-pack') do - api.check_access('git-receive-pack', @gl_repository, @repo_path, @actor, @changes, @protocol, env: ObjectDirsHelper.all_attributes.to_json) + api.check_access('git-receive-pack', @gl_repository, @repo_path, @gl_id, @changes, @protocol, env: ObjectDirsHelper.all_attributes.to_json) end raise AccessDeniedError, status.message unless status.allowed? diff --git a/ruby/gitlab-shell/lib/gitlab_custom_hook.rb b/ruby/gitlab-shell/lib/gitlab_custom_hook.rb index 53069df87..ba3f760d2 100644 --- a/ruby/gitlab-shell/lib/gitlab_custom_hook.rb +++ b/ruby/gitlab-shell/lib/gitlab_custom_hook.rb @@ -5,9 +5,9 @@ require_relative 'gitlab_metrics' class GitlabCustomHook attr_reader :vars, :config - def initialize(repo_path, key_id) + def initialize(repo_path, gl_id) @repo_path = repo_path - @vars = { 'GL_ID' => key_id } + @vars = { 'GL_ID' => gl_id } @config = GitlabConfig.new end diff --git a/ruby/gitlab-shell/lib/gitlab_net.rb b/ruby/gitlab-shell/lib/gitlab_net.rb index 27c49d14b..74996e712 100644 --- a/ruby/gitlab-shell/lib/gitlab_net.rb +++ b/ruby/gitlab-shell/lib/gitlab_net.rb @@ -12,7 +12,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength CHECK_TIMEOUT = 5 API_INACCESSIBLE_MESSAGE = 'API is not accessible'.freeze - def check_access(cmd, gl_repository, repo, who, changes, protocol, env: {}) + def check_access(cmd, gl_repository, repo, gl_id, changes, protocol, env: {}) changes = changes.join("\n") unless changes.is_a?(String) params = { @@ -24,8 +24,8 @@ class GitlabNet # rubocop:disable Metrics/ClassLength env: env } - who_sym, _, who_v = self.class.parse_who(who) - params[who_sym] = who_v + gl_id_sym, gl_id_value = self.class.parse_gl_id(gl_id) + params[gl_id_sym] = gl_id_value url = "#{internal_api_endpoint}/allowed" resp = post(url, params) @@ -75,12 +75,12 @@ class GitlabNet # rubocop:disable Metrics/ClassLength false end - def post_receive(gl_repository, identifier, changes, push_options) + def post_receive(gl_repository, gl_id, changes, push_options) params = { gl_repository: gl_repository, - identifier: identifier, + identifier: gl_id, changes: changes, - :"push_options[]" => push_options, # rubocop:disable Style/HashSyntax + :'push_options[]' => push_options, # rubocop:disable Style/HashSyntax } resp = post("#{internal_api_endpoint}/post_receive", params) @@ -97,19 +97,19 @@ class GitlabNet # rubocop:disable Metrics/ClassLength JSON.parse(resp.body) if resp.code == '200' end - def self.parse_who(who) - if who.start_with?("key-") - value = who.gsub("key-", "") - raise ArgumentError, "who='#{who}' is invalid!" unless value =~ /\A[0-9]+\z/ - [:key_id, 'key_id', value] - elsif who.start_with?("user-") - value = who.gsub("user-", "") - raise ArgumentError, "who='#{who}' is invalid!" unless value =~ /\A[0-9]+\z/ - [:user_id, 'user_id', value] - elsif who.start_with?("username-") - [:username, 'username', who.gsub("username-", "")] + def self.parse_gl_id(gl_id) + if gl_id.start_with?('key-') + value = gl_id.gsub('key-', '') + raise ArgumentError, "gl_id='#{gl_id}' is invalid!" unless value =~ /\A[0-9]+\z/ + [:key_id, value] + elsif gl_id.start_with?('user-') + value = gl_id.gsub('user-', '') + raise ArgumentError, "gl_id='#{gl_id}' is invalid!" unless value =~ /\A[0-9]+\z/ + [:user_id, value] + elsif gl_id.start_with?('username-') + [:username, gl_id.gsub('username-', '')] else - raise ArgumentError, "who='#{who}' is invalid!" + raise ArgumentError, "gl_id='#{gl_id}' is invalid!" end end diff --git a/ruby/gitlab-shell/lib/gitlab_post_receive.rb b/ruby/gitlab-shell/lib/gitlab_post_receive.rb index df70c49a2..a6bea0f9b 100644 --- a/ruby/gitlab-shell/lib/gitlab_post_receive.rb +++ b/ruby/gitlab-shell/lib/gitlab_post_receive.rb @@ -9,11 +9,11 @@ require 'securerandom' class GitlabPostReceive attr_reader :config, :gl_repository, :repo_path, :changes, :jid, :output_stream - def initialize(gl_repository, repo_path, actor, changes, push_options, output_stream = $stdout) + def initialize(gl_repository, repo_path, gl_id, changes, push_options, output_stream = $stdout) @config = GitlabConfig.new @gl_repository = gl_repository @repo_path = repo_path.strip - @actor = actor + @gl_id = gl_id @changes = changes @push_options = push_options @jid = SecureRandom.hex(12) @@ -22,7 +22,7 @@ class GitlabPostReceive def exec response = GitlabMetrics.measure("post-receive") do - api.post_receive(gl_repository, @actor, changes, @push_options) + api.post_receive(gl_repository, @gl_id, changes, @push_options) end return false unless response diff --git a/ruby/gitlab-shell/spec/gitlab_post_receive_spec.rb b/ruby/gitlab-shell/spec/gitlab_post_receive_spec.rb index ca2d7fae9..f8c42cfdd 100644 --- a/ruby/gitlab-shell/spec/gitlab_post_receive_spec.rb +++ b/ruby/gitlab-shell/spec/gitlab_post_receive_spec.rb @@ -5,7 +5,7 @@ require 'gitlab_post_receive' describe GitlabPostReceive do let(:repository_path) { "/home/git/repositories" } let(:repo_name) { 'dzaporozhets/gitlab-ci' } - let(:actor) { 'key-123' } + let(:gl_id) { 'key-123' } let(:changes) { "123456 789012 refs/heads/tést\n654321 210987 refs/tags/tag" } let(:wrongly_encoded_changes) { changes.encode("ISO-8859-1").force_encoding("UTF-8") } let(:base64_changes) { Base64.encode64(wrongly_encoded_changes) } @@ -13,7 +13,7 @@ describe GitlabPostReceive do let(:gl_repository) { "project-1" } let(:push_options) { [] } let(:output_stream) { double('output_stream') } - let(:gitlab_post_receive) { GitlabPostReceive.new(gl_repository, repo_path, actor, wrongly_encoded_changes, push_options, output_stream) } + let(:gitlab_post_receive) { GitlabPostReceive.new(gl_repository, repo_path, gl_id, wrongly_encoded_changes, push_options, output_stream) } let(:broadcast_message) { "test " * 10 + "message " * 10 } let(:enqueued_at) { Time.new(2016, 6, 23, 6, 59) } let(:new_merge_request_message) do |