diff options
author | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-07 13:52:56 +0300 |
---|---|---|
committer | Patrick Steinhardt <psteinhardt@gitlab.com> | 2020-12-10 10:16:42 +0300 |
commit | e657f69e8bb63f3b41f54b33072dab52c8669083 (patch) | |
tree | d974ef8a1207965178d22a3eb326b5f751442da1 | |
parent | 3f7f20a1856a943a5c85cf9a54a8d2679ee8e9a0 (diff) |
hooks: Convert Ruby to inject transaction info via payload
Now that all of the Go code injects and extracts all transaction
information via the HooksPayload, this commit also converts the Ruby
implementation to do so. While we don't have any explicit tests to
verify it works, it gets implicitly tested by `make test-with-praefect`
for any transaction-enabled Ruby RPC.
-rw-r--r-- | ruby/lib/gitlab/git/hook.rb | 20 | ||||
-rw-r--r-- | ruby/lib/praefect/transaction.rb | 27 |
2 files changed, 30 insertions, 17 deletions
diff --git a/ruby/lib/gitlab/git/hook.rb b/ruby/lib/gitlab/git/hook.rb index 337969b77..1829222b4 100644 --- a/ruby/lib/gitlab/git/hook.rb +++ b/ruby/lib/gitlab/git/hook.rb @@ -80,9 +80,8 @@ module Gitlab def call_receive_hook(gl_id, gl_username, oldrev, newrev, ref, push_options, transaction) changes = [oldrev, newrev, ref].join(" ") - vars = env_base_vars(gl_id, gl_username) + vars = env_base_vars(gl_id, gl_username, transaction) vars.merge!(push_options.env_data) if push_options - vars.merge!(transaction.env_vars) if transaction call_stdin_hook([], changes, vars) end @@ -90,8 +89,7 @@ module Gitlab def call_reference_transaction_hook(gl_id, gl_username, oldrev, newrev, ref, transaction) changes = [oldrev, newrev, ref].join(" ") - vars = env_base_vars(gl_id, gl_username) - vars.merge!(transaction.env_vars) if transaction + vars = env_base_vars(gl_id, gl_username, transaction) call_stdin_hook(["prepared"], changes, vars) end @@ -115,18 +113,22 @@ module Gitlab err_message end - def hooks_payload - Base64.strict_encode64({ + def hooks_payload(transaction) + payload = { repository: repository.gitaly_repository.to_json, binary_directory: Gitlab.config.gitaly.bin_dir, internal_socket: Gitlab.config.gitaly.internal_socket, internal_socket_token: ENV['GITALY_TOKEN'] - }.to_json) + } + + payload.merge!(transaction.payload) if transaction + + Base64.strict_encode64(payload.to_json) end - def env_base_vars(gl_id, gl_username) + def env_base_vars(gl_id, gl_username, transaction = nil) { - 'GITALY_HOOKS_PAYLOAD' => hooks_payload, + 'GITALY_HOOKS_PAYLOAD' => hooks_payload(transaction), 'GITALY_GITLAB_SHELL_DIR' => Gitlab.config.gitlab_shell.path, 'GITLAB_SHELL_DIR' => Gitlab.config.gitlab_shell.path, 'GITALY_LOG_DIR' => Gitlab.config.logging.dir, diff --git a/ruby/lib/praefect/transaction.rb b/ruby/lib/praefect/transaction.rb index 608839a48..073ab83cc 100644 --- a/ruby/lib/praefect/transaction.rb +++ b/ruby/lib/praefect/transaction.rb @@ -1,12 +1,23 @@ module Praefect class Transaction - PRAEFECT_SERVER_KEY = "gitaly-praefect-server".freeze - PRAEFECT_SERVER_ENV = "GITALY_PRAEFECT_SERVER".freeze - TRANSACTION_KEY = "gitaly-reference-transaction".freeze - TRANSACTION_ENV = "GITALY_REFERENCE_TRANSACTION".freeze + PRAEFECT_SERVER_METADATA_KEY = "gitaly-praefect-server".freeze + PRAEFECT_SERVER_PAYLOAD_KEY = "praefect".freeze + TRANSACTION_METADATA_KEY = "gitaly-reference-transaction".freeze + TRANSACTION_PAYLOAD_KEY = "transaction".freeze + + MissingPraefectMetadataError = Class.new(StandardError) def self.from_metadata(metadata) - new(metadata[PRAEFECT_SERVER_KEY], metadata[TRANSACTION_KEY]) + transaction_metadata = metadata[TRANSACTION_METADATA_KEY] + return new(nil, nil) unless transaction_metadata + + praefect_metadata = metadata[PRAEFECT_SERVER_METADATA_KEY] + raise MissingPraefectMetadataError, "missing praefect server metadata" unless praefect_metadata + + praefect = JSON.parse(Base64.decode64(praefect_metadata)) + transaction = JSON.parse(Base64.decode64(transaction_metadata)) + + new(praefect, transaction) end def initialize(server, transaction) @@ -14,10 +25,10 @@ module Praefect @transaction = transaction end - def env_vars + def payload { - TRANSACTION_ENV => @transaction, - PRAEFECT_SERVER_ENV => @server + TRANSACTION_PAYLOAD_KEY => @transaction, + PRAEFECT_SERVER_PAYLOAD_KEY => @server }.reject { |_, v| v.nil? } end end |