Class: CiderClient
- Inherits:
-
Object
- Object
- CiderClient
- Defined in:
- lib/cider_client.rb
Overview
Allows you to query a Cider CI server using Ruby. Wraps the responses in Ruby data structures (usually hashes)
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#execution_id ⇒ Object
Returns the value of attribute execution_id.
-
#host ⇒ Object
Returns the value of attribute host.
-
#password ⇒ Object
writeonly
Sets the attribute password.
-
#username ⇒ Object
writeonly
Sets the attribute username.
Instance Method Summary collapse
- #api_compatible? ⇒ Boolean
- #api_url(path = '') ⇒ Object
- #attachment_data(href) ⇒ Object
-
#execution_url(path) ⇒ Object
URL starting from the execution, with the passed path appended TODO: Stick these *_url methods into something like url_for(:execution, ‘foo’).
- #get(url) ⇒ Object
-
#initialize(options = {}) ⇒ CiderClient
constructor
A new instance of CiderClient.
- #recurse_tasks(tasks, data) ⇒ Object
- #tasks ⇒ Object
-
#trial_attachment_groups ⇒ Object
Misguided idea: We thought we could retrieve all attachments based on a commit SHA traced to its tree id, but you do need an execution ID def tree_id_from_commit(commit_sha) ‘git show #commit_sha –format=%T | head -1`.chomp end.
-
#trial_attachment_hrefs(pattern = /.*/) ⇒ Object
Takes a regex pattern and returns only hrefs of the attachments that matched the regex.
-
#trials ⇒ Object
I’ve got a long thing, what can I say…
Constructor Details
#initialize(options = {}) ⇒ CiderClient
Returns a new instance of CiderClient.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/cider_client.rb', line 12 def initialize( = {}) @host = .fetch(:host) @username = .fetch(:username) @password = .fetch(:password) @base_url = if @host =~ /^https?:\/\// @host else "http://" + @host end fail "The server at #{@host} does not provide the\ correct API version. v2 is required." unless api_compatible? end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
10 11 12 |
# File 'lib/cider_client.rb', line 10 def base_url @base_url end |
#execution_id ⇒ Object
Returns the value of attribute execution_id.
8 9 10 |
# File 'lib/cider_client.rb', line 8 def execution_id @execution_id end |
#host ⇒ Object
Returns the value of attribute host.
8 9 10 |
# File 'lib/cider_client.rb', line 8 def host @host end |
#password=(value) ⇒ Object (writeonly)
Sets the attribute password
9 10 11 |
# File 'lib/cider_client.rb', line 9 def password=(value) @password = value end |
#username=(value) ⇒ Object (writeonly)
Sets the attribute username
9 10 11 |
# File 'lib/cider_client.rb', line 9 def username=(value) @username = value end |
Instance Method Details
#api_compatible? ⇒ Boolean
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cider_client.rb', line 37 def api_compatible? begin # Try to get the API root URL. If it 404s out, this server probably # doesn't offer that API version. get(api_url) api_version_matches = true rescue RestClient::ResourceNotFound api_version_matches = false end api_version_matches end |
#api_url(path = '') ⇒ Object
27 28 29 |
# File 'lib/cider_client.rb', line 27 def api_url(path = '') "/cider-ci/api/v2/#{path}" end |
#attachment_data(href) ⇒ Object
118 119 120 121 122 |
# File 'lib/cider_client.rb', line 118 def (href) = JSON.parse(get(href)) stream_url = ['_links']['data-stream']['href'] get(stream_url) end |
#execution_url(path) ⇒ Object
URL starting from the execution, with the passed path appended TODO: Stick these *_url methods into something like url_for(:execution, ‘foo’)
33 34 35 |
# File 'lib/cider_client.rb', line 33 def execution_url(path) api_url("execution/#{@execution_id}/#{path}") end |
#get(url) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cider_client.rb', line 125 def get(url) full_url = if url =~ /^https?:\/\// url else @base_url + url end RestClient::Request.new( method: :get, url: full_url, user: @username, password: @password ).execute end |
#recurse_tasks(tasks, data) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/cider_client.rb', line 49 def recurse_tasks(tasks, data) if data['_links']['cici:task'] tasks = tasks.concat(data['_links']['cici:task']) end if data['_links']['next'] puts "Retrieved #{tasks.count} tasks total so far." data = JSON.parse(get(data['_links']['next']['href'])) tasks = recurse_tasks(tasks, data) end tasks end |
#tasks ⇒ Object
61 62 63 64 65 |
# File 'lib/cider_client.rb', line 61 def tasks tasks = [] recurse_tasks(tasks, JSON.parse(get(execution_url('tasks')))) end |
#trial_attachment_groups ⇒ Object
Misguided idea: We thought we could retrieve all attachments based on a commit SHA traced to its tree id, but you do need an execution ID def tree_id_from_commit(commit_sha)
`git show #{commit_sha} --format=%T | head -1`.chomp
end
91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cider_client.rb', line 91 def puts 'Retrieving trial details to find all attachments, this may take a long time.' = [] trials.each do |trial| trial_url = trial['href'] puts "Retrieving trial details for #{trial_url}." single_trial = JSON.parse(get(trial_url)) << \ single_trial['_links']['cici:trial-attachments'] end end |
#trial_attachment_hrefs(pattern = /.*/) ⇒ Object
Takes a regex pattern and returns only hrefs of the attachments that matched the regex.
106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/cider_client.rb', line 106 def (pattern = /.*/) matching_tas = [] .each do |tag| = tag['href'] = JSON.parse(get()) matching_tas << ['_links']['cici:trial-attachment'].select do |ta| ta if ta['href'].match(pattern) end end matching_tas.flatten.map { |ta| ta['href'] } end |
#trials ⇒ Object
I’ve got a long thing, what can I say… rubocop:disable Metrics/MethodLength
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/cider_client.rb', line 69 def trials trials = [] tasks.each do |task| task_url = task['href'] details = JSON.parse(get(task_url)) trials_url = details['_links']['cici:trials']['href'] puts "Need to retrieve all trials for #{details['_links']['cici:trials']['href']}" single_trial = JSON.parse(get(trials_url)) single_trial['_links']['cici:trial'].each do |st| trials << st end end trials end |