cider_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cider_client.rb +131 -0
  3. metadata +73 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7badeb8e7fe4e1f1e5f92fd2d3c77c68dc43298
4
+ data.tar.gz: cbad76cedde3f4e55a34bc990a058036e1864feb
5
+ SHA512:
6
+ metadata.gz: 3992118565cb51083efcc34725ccbaf25caef0c53421d15d16818aee452b66e2738aca2516dfba83fb80b93f88a9a9d29b2e062502b3dca2281edf129c243774
7
+ data.tar.gz: 7dc8046005962131739117bb2217fe0788e954c1613291920061e24bfb625c5a91f14b367f6e00f85841b175acd6c37fa4cd936c5a1cba8402f188cc002a7878
@@ -0,0 +1,131 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'fileutils'
4
+
5
+ # Allows you to query a Cider CI server using Ruby. Wraps the responses
6
+ # in Ruby data structures (usually hashes)
7
+ class CiderClient
8
+ attr_accessor :execution_id, :host
9
+ attr_writer :username, :password
10
+
11
+ def initialize(options = {})
12
+ @host = options.fetch(:host)
13
+ @username = options.fetch(:username)
14
+ @password = options.fetch(:password)
15
+ fail "The server at #{@host} does not provide the\
16
+ correct API version. v2 is required." unless api_compatible?
17
+ end
18
+
19
+ # Returns the base URL including usernames and passwords. Always uses usernames
20
+ # and passwords, because you can't do anything on Cider without basic auth anyhow.
21
+ # Is used in all further url_* methods.
22
+ def base_url
23
+ "http://#{@username}:#{@password}@#{@host}"
24
+ end
25
+
26
+ # URL starting from the base URL root, with the passed path appended
27
+ def url(path)
28
+ "#{base_url}#{path}"
29
+ end
30
+
31
+ def api_url(path = '')
32
+ url("/cider-ci/api/v2/#{path}")
33
+ end
34
+
35
+ # URL starting from the execution, with the passed path appended
36
+ # TODO: Stick these *_url methods into something like url_for(:execution, 'foo')
37
+ def execution_url(path)
38
+ api_url("execution/#{@execution_id}/#{path}")
39
+ end
40
+
41
+ def api_compatible?
42
+ begin
43
+ # Try to get the API root URL. If it 404s out, this server probably
44
+ # doesn't offer that API version.
45
+ RestClient.get(api_url)
46
+ api_version_matches = true
47
+ rescue RestClient::ResourceNotFound
48
+ api_version_matches = false
49
+ end
50
+ api_version_matches
51
+ end
52
+
53
+ def recurse_tasks(tasks, data)
54
+ if data['_links']['cici:task']
55
+ tasks = tasks.concat(data['_links']['cici:task'])
56
+ end
57
+ if data['_links']['next']
58
+ puts "Retrieved #{tasks.count} tasks total so far."
59
+ data = JSON.parse(RestClient.get(url(data['_links']['next']['href'])))
60
+ tasks = recurse_tasks(tasks, data)
61
+ end
62
+ tasks
63
+ end
64
+
65
+ def tasks
66
+ tasks = []
67
+ recurse_tasks(tasks,
68
+ JSON.parse(RestClient.get(execution_url('tasks'))))
69
+ end
70
+
71
+ # I've got a long thing, what can I say...
72
+ # rubocop:disable Metrics/MethodLength
73
+ def trials
74
+ trials = []
75
+ tasks.each do |task|
76
+ task_url = url(task['href'])
77
+ details = JSON.parse(RestClient.get(task_url))
78
+ trials_url = url(details['_links']['cici:trials']['href'])
79
+ puts "Need to retrieve all trials for #{details['_links']['cici:trials']['href']}"
80
+ single_trial = JSON.parse(RestClient.get(trials_url))
81
+ single_trial['_links']['cici:trial'].each do |st|
82
+ trials << st
83
+ end
84
+ end
85
+ trials
86
+ end
87
+
88
+ # Misguided idea: We thought we could retrieve all attachments
89
+ # based on a commit SHA traced to its tree id, but you do need
90
+ # an execution ID
91
+ # def tree_id_from_commit(commit_sha)
92
+ # `git show #{commit_sha} --format=%T | head -1`.chomp
93
+ # end
94
+
95
+ def trial_attachment_groups
96
+ puts 'Retrieving trial details to find all attachments, this may take a long time.'
97
+ trial_attachment_groups = []
98
+ trials.each do |trial|
99
+ trial_url = url(trial['href'])
100
+ puts "Retrieving trial details for #{trial_url}."
101
+ single_trial = JSON.parse(RestClient.get(trial_url))
102
+ trial_attachment_groups << \
103
+ single_trial['_links']['cici:trial-attachments']
104
+ end
105
+ trial_attachment_groups
106
+ end
107
+
108
+ # Takes a regex pattern and returns only hrefs of the attachments
109
+ # that matched the regex.
110
+ def trial_attachment_hrefs(pattern = /.*/)
111
+ matching_tas = []
112
+ trial_attachment_groups.each do |tag|
113
+ trial_attachment_url = url(tag['href'])
114
+ trial_attachment_details = JSON.parse(RestClient.get(trial_attachment_url))
115
+ matching_tas << trial_attachment_details['_links']['cici:trial-attachment'].select do |ta|
116
+ ta if ta['href'].match(pattern)
117
+ end
118
+ end
119
+ matching_tas.flatten.map { |ta| ta['href'] }
120
+ end
121
+
122
+ def attachment_data(href)
123
+ attachment_details = JSON.parse(RestClient.get(url(href)))
124
+ stream_url = attachment_details['_links']['data-stream']['href']
125
+
126
+ # Stupid fix because the CI hosts seem to return their own IP instead of hostname
127
+ # in these responses
128
+ stream_url.gsub!('https://195.176.254.43', base_url)
129
+ RestClient.get(stream_url)
130
+ end
131
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cider_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ramón Cahenzli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ description: Rudimentary library that wraps the Cider CI API in Ruby objects.
42
+ email: rca@psy-q.ch
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/cider_client.rb
48
+ homepage: https://github.com/psy-q/cider_client
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+ post_install_message:
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.2.2
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: A client library for the Cider CI API
72
+ test_files: []
73
+ has_rdoc: