Class: Gerrit::Client
- Inherits:
-
Object
- Object
- Gerrit::Client
- Defined in:
- lib/gerrit/client.rb
Overview
Client for executing commands against the Gerrit server.
Instance Method Summary collapse
-
#change(change_id_or_number) ⇒ Object
Returns basic information about a change.
-
#execute(command) ⇒ String
Executes a command against the Gerrit server, returning the output.
-
#group_members(group, recursive: true) ⇒ Array<Hash>
Returns members associated with a group.
-
#groups ⇒ Array<String>
Returns all groups visible to the user.
-
#initialize(config) ⇒ Client
constructor
Create a client using the given configuration settings.
- #query_changes(query) ⇒ Object
-
#users ⇒ Object
Returns a list of all users to include in the default search scope.
Constructor Details
#initialize(config) ⇒ Client
Create a client using the given configuration settings.
9 10 11 |
# File 'lib/gerrit/client.rb', line 9 def initialize(config) @config = config end |
Instance Method Details
#change(change_id_or_number) ⇒ Object
Returns basic information about a change.
60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/gerrit/client.rb', line 60 def change(change_id_or_number) rows = execute(%W[query --format=JSON --current-patch-set change:#{change_id_or_number}]).split("\n")[0..-2] if rows.empty? raise Errors::CommandFailedError, "No change matches the id '#{change_id_or_number}'" else JSON.parse(rows.first) end end |
#execute(command) ⇒ String
Executes a command against the Gerrit server, returning the output.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gerrit/client.rb', line 17 def execute(command) user = @config[:user] host = @config[:host] port = @config[:port] ssh_cmd = %W[ssh -p #{port} #{user}@#{host} gerrit] + command result = Subprocess.spawn(ssh_cmd) unless result.success? raise Errors::GerritCommandFailedError, "Command `#{ssh_cmd.join(' ')}` failed:\n" \ "STATUS: #{result.status}\n" \ "STDOUT: #{result.stdout.inspect}\n" \ "STDERR: #{result.stderr.inspect}\n" end result.stdout end |
#group_members(group, recursive: true) ⇒ Array<Hash>
Returns members associated with a group.
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gerrit/client.rb', line 47 def group_members(group, recursive: true) flags = [] flags << '--recursive' if recursive rows = execute(%w[ls-members] + ["'#{group}'"] + flags).split("\n")[1..-1] rows.map do |row| id, username, full_name, email = row.split("\t") { id: id, username: username, full_name: full_name, email: email } end end |
#groups ⇒ Array<String>
Returns all groups visible to the user.
38 39 40 |
# File 'lib/gerrit/client.rb', line 38 def groups execute(%w[ls-groups]).split("\n") end |
#query_changes(query) ⇒ Object
73 74 75 76 77 78 79 80 |
# File 'lib/gerrit/client.rb', line 73 def query_changes(query) rows = execute(%W[query --format=JSON --current-patch-set --submit-records] + [query]).split("\n")[0..-2] rows.map { |row| JSON.parse(row) } end |
#users ⇒ Object
Returns a list of all users to include in the default search scope.
Gerrit doesn’t actually have an endpoint to return all visible users, so we do the next best thing which is to get users for all groups the user is a part of, which for all practical purposes is probably good enough.
Set the ‘user_search_groups` configuration option to speed this up, ideally to just one group so we don’t have to make parallel calls.
90 91 92 93 94 95 96 |
# File 'lib/gerrit/client.rb', line 90 def users search_groups = Array(@config.fetch(:user_search_groups, groups)) Utils.map_in_parallel(search_groups) do |group| group_members(group).map{ |user| user[:username] } end.flatten.uniq end |