Class: ZephyrClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/zephyr_client/base/client.rb

Constant Summary collapse

EXECUTION_CMD =

TODO: Leverage command pattern.

"/jira/rest/zapi/latest/execution"
ZAPI_LIST =
{
  :cycle => "/jira/rest/zapi/latest/cycle"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*p) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zephyr_client/base/client.rb', line 20

def initialize(*p)

  @user=nil
  puts __FILE__ + (__LINE__).to_s + " size : #{p.size}" if @debug

  if p.size==1 && p[0].is_a?(Hash)
    ;
  elsif p.size==1 && p[0].is_a?(String)
    # Load form from a JSON file
    @options = JSON.parse(File.read(p[0].to_s))
    pp @options if @debug
  elsif p.size==2
    @user=p[0]
    server=p[1]
    puts __FILE__ + (__LINE__).to_s + " ZephyrClient::Client.new(#{user}, #{server})" if @debug
    @options = {
      'site' => server,
      'context_path' => "/jira",
      'rest_base_path' => "/jira/rest/api/2",
      'ssl_verify_mode' => 1,
      'use_ssl' => true,
      'auth_type' => :basic,
      'http_debug' => true,
      'read_timeout' => 100
    }
  end

end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



17
18
19
# File 'lib/zephyr_client/base/client.rb', line 17

def debug
  @debug
end

#userObject

Returns the value of attribute user.



18
19
20
# File 'lib/zephyr_client/base/client.rb', line 18

def user
  @user
end

Instance Method Details

#connect(uri = @options['site']) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/zephyr_client/base/client.rb', line 53

def connect(uri=@options['site'])
  if @debug
    puts __FILE__ + (__LINE__).to_s + " [enter]:http_con(#{uri})"
    puts __FILE__ + (__LINE__).to_s + " | options"
    pp @options
  end

  ZephyrClient::Utils::connect(@options['site'], @options)
end

#getCycles(id) ⇒ Object



74
75
76
77
78
# File 'lib/zephyr_client/base/client.rb', line 74

def getCycles(id)
  response = sendRequest("#{ZAPI_LIST[:cycle]}/#{id}")
  puts __FILE__ + (__LINE__).to_s + " ==== Cycles #{id}  ====\n" if @debug
  response.body
end

#getCyclesByProjectId(projectId, versionId) ⇒ Object



81
82
83
84
85
86
# File 'lib/zephyr_client/base/client.rb', line 81

def getCyclesByProjectId(projectId, versionId)
  response = sendRequest("/jira/rest/zapi/latest/cycle?projectId=#{projectId}&versionId=#{versionId}")
  puts __FILE__ + (__LINE__).to_s + " ==== Cycles for Project #{projectId} ====\n"  if @debug

  QResponse.new(response)
end

#getExecutions(opt) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/zephyr_client/base/client.rb', line 189

def getExecutions(opt)
  rc = nil
  if opt[:projectId] && opt[:versionId] && opt[:cycleId]
    response = sendRequest("/jira/rest/zapi/latest/execution?projectId=#{opt[:projectId]}&versionId=#{opt[:versionId]}&cycleId=#{opt[:cycleId]}", {'Accept' => 'application/json'})
    rc = QResponse.new(response).to_json
  end

  rc
end

#getProject(projectId) ⇒ Object



95
96
97
98
99
100
# File 'lib/zephyr_client/base/client.rb', line 95

def getProject(projectId)
  response = sendRequest("/jira/rest/api/2/project/#{projectId}")
  puts __FILE__ + (__LINE__).to_s + " ==== Project #{projectId} ====\n" if @debug

  QResponse.new(response)
end

#getProjectByKey(k) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/zephyr_client/base/client.rb', line 103

def getProjectByKey(k)
  #puts __FILE__ + (__LINE__).to_s + " [getProjectByKey]: #{k}"
  matches=[]
  hits = getProjects().to_json
  hits.each do |p|
    if p.is_a?(Hash)
      if p.has_key?('key') && p['key']==k
        matches << p
      end

    else
      raise "UNEXPECTED::TypeError::#{p.class}"
    end
  end

  matches
end

#getProjectCycles(projectId) ⇒ Object



121
122
123
124
125
126
# File 'lib/zephyr_client/base/client.rb', line 121

def getProjectCycles(projectId)
  response = sendRequest("/jira/rest/zapi/latest/cycle?projectId=#{projectId}")
  puts __FILE__ + (__LINE__).to_s + " ==== Cycles for Project #{projectId} ====\n"

  response.body
end

#getProjectPerRelease(projectKey, expected_release, cycle, debug = @debug) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/zephyr_client/base/client.rb', line 129

def getProjectPerRelease(projectKey, expected_release, cycle, debug = @debug)
  projectId = nil
  cycleId = nil
  versionId = nil

  hits = getProjectByKey(projectKey)


  if hits.size==1

    projectId = hits[0]['id']

    puts "\n\n=========== getProject(#{projectId} ============"  if @debug
    project = getProject(projectId).to_json

    if debug
      puts __FILE__ + (__LINE__).to_s + " hits => #{project.keys.sort.to_s}"
      pp project
      puts '*' * 72
    end

    project['versions'].each do |v|
      puts __FILE__ + (__LINE__).to_s +  " KEY #{v} => " if @debug

      if v['name'].match(/#{expected_release}/)
        puts "RELEASE: #{v['id']} : #{v['name']}" if @debug
        versionId = v['id']
      end
    end

    if versionId

      ## Find the Cycle (e.g. Sprint)
      ## We have the projectID, versionId, now let's get the CycleID
      rc = getCyclesByProjectId(projectId, versionId)
      pp rc.to_json if @debug

      rc.to_json.each_pair do |k, v|

        if v.is_a?(Hash) && v.has_key?('name') && v['name'].match(/#{cycle}/)
          cycleId = k.to_s

          if @debug
            puts __FILE__ + (__LINE__).to_s + " cycleId #{k.to_s}"
            pp k
          end

        elsif !v.is_a?(Hash)
          puts "#{k} => #{v}" if @debug
        end
      end

    end

  end

  { :projectId => projectId, :cycleId => cycleId, :versionId => versionId }
end

#getProjectsObject



89
90
91
92
93
# File 'lib/zephyr_client/base/client.rb', line 89

def getProjects()
  response = sendRequest("/jira/rest/api/2/project/")
  puts __FILE__ + (__LINE__).to_s + " ==== All Projects ====\n" if @debug
  QResponse.new(response)
end

#sendRequest(path, headers = {'Accept' => 'application/json'}) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/zephyr_client/base/client.rb', line 64

def sendRequest(path, headers = {'Accept' => 'application/json'})
  request = ZephyrClient::Utils::createRequest(@user, 'get', path, nil, headers)
  connection = connect(@options['site'])

  puts __FILE__ + (__LINE__).to_s + " connection => #{connection.class}" if @debug
  response = connection.request(request)
  response
end

#setUser(u) ⇒ Object



49
50
51
# File 'lib/zephyr_client/base/client.rb', line 49

def setUser(u)
  @user = u
end

#updateTestStatus(id, status) ⇒ Object

body = { “status” => “3”}



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/zephyr_client/base/client.rb', line 201

def updateTestStatus(id, status)

  unless ZephyrClient::ZSTATUS.has_key?(status.downcase)
    raise "UNKNOWN::STATUS::#{status.to_s}"
  end

  body = "{ \"status\" : \"#{ZephyrClient::ZSTATUS[status.downcase]}\"  }"

  request = ZephyrClient::Utils::createRequest(@user, 'PUT', "#{EXECUTION_CMD}/#{id}/execute", body, {'Content-Type' => 'application/json'})
  httpReq = connect(@options['site'])
  response = httpReq.request(request)

  if ZephyrClient::Utils::isVerbose()
    puts ">>>>>  TEST STATUS <<<<<<"
    puts response
  end

  response
end