Class: CiderClient

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  @host = options.fetch(:host)
  @username = options.fetch(:username)
  @password = options.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_urlObject (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_idObject

Returns the value of attribute execution_id.



8
9
10
# File 'lib/cider_client.rb', line 8

def execution_id
  @execution_id
end

#hostObject

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

Parameters:

  • value

    the value to set the attribute password to.



9
10
11
# File 'lib/cider_client.rb', line 9

def password=(value)
  @password = value
end

#username=(value) ⇒ Object (writeonly)

Sets the attribute username

Parameters:

  • value

    the value to set the attribute username to.



9
10
11
# File 'lib/cider_client.rb', line 9

def username=(value)
  @username = value
end

Instance Method Details

#api_compatible?Boolean

Returns:

  • (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 attachment_data(href)
  attachment_details = JSON.parse(get(href))
  stream_url = attachment_details['_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

#tasksObject



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_groupsObject

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 trial_attachment_groups
  puts 'Retrieving trial details to find all attachments, this may take a long time.'
  trial_attachment_groups = []
  trials.each do |trial|
    trial_url = trial['href']
    puts "Retrieving trial details for #{trial_url}."
    single_trial = JSON.parse(get(trial_url))
    trial_attachment_groups << \
      single_trial['_links']['cici:trial-attachments']
  end
  trial_attachment_groups
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 trial_attachment_hrefs(pattern = /.*/)
  matching_tas = []
  trial_attachment_groups.each do |tag|
    trial_attachment_url = tag['href']
    trial_attachment_details = JSON.parse(get(trial_attachment_url))
    matching_tas << trial_attachment_details['_links']['cici:trial-attachment'].select do |ta|
      ta if ta['href'].match(pattern)
    end
  end
  matching_tas.flatten.map { |ta| ta['href'] }
end

#trialsObject

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