1
6
from jenkinsapi.artifact import Artifact
2
7
from jenkinsapi import config
3
8
from jenkinsapi.jenkinsbase import JenkinsBase
4
from jenkinsapi.exceptions import NoResults, FailedNoResults
5
from jenkinsapi.constants import STATUS_FAIL, STATUS_ABORTED, RESULTSTATUS_FAILURE
9
from jenkinsapi.exceptions import NoResults
10
from jenkinsapi.constants import STATUS_SUCCESS
6
11
from jenkinsapi.result_set import ResultSet
8
13
from time import sleep
46
55
def _get_git_rev(self):
47
for item in self._data['actions']:
48
branch = item.get('buildsByBranchName')
49
head = branch and branch.get('origin/HEAD')
51
return head['revision']['SHA1']
56
# Sometimes we have None as part of actions. Filter those actions
57
# which have lastBuiltRevision in them
58
_actions = [x for x in self._data['actions'] if x \
59
and "lastBuiltRevision" in x]
61
revision = item["lastBuiltRevision"]["SHA1"]
53
64
def _get_hg_rev(self):
54
revs = [(item['date'], item['node'])
55
for item in self._data['changeSet']['items']]
56
revs = sorted(revs, key=lambda tup: float(tup[0].split('-')[0]))
57
return revs[-1][1] # get last commit revision
65
return [x['mercurialNodeName'] for x in self._data['actions'] if 'mercurialNodeName' in x][0]
59
67
def get_duration(self):
60
68
return self._data["duration"]
62
70
def get_artifacts( self ):
63
71
for afinfo in self._data["artifacts"]:
64
url = "%sartifact/%s" % ( self.baseurl, afinfo["relativePath"] )
72
url = "%s/artifact/%s" % ( self.baseurl, afinfo["relativePath"] )
65
73
af = Artifact( afinfo["fileName"], url, self )
69
76
def get_artifact_dict(self):
70
return dict( (a.filename, a) for a in self.get_artifacts() )
78
(af.filename, af) for af in self.get_artifacts()
72
81
def get_upstream_job_name(self):
210
219
return self._data["building"]
222
while self.is_running():
212
225
def is_good( self ):
214
227
Return a bool, true if the build was good.
215
228
If the build is still running, return False.
217
return ( not self.is_running() ) and self._data["result"] == 'SUCCESS'
230
return ( not self.is_running() ) and self._data["result"] == STATUS_SUCCESS
219
232
def block_until_complete(self, delay=15):
220
233
assert isinstance( delay, int )
222
235
while self.is_running():
223
236
total_wait = delay * count
224
log.info("Waited %is for %s #%s to complete" % ( total_wait, self.job.id(), self.id() ) )
237
log.info("Waited %is for %s #%s to complete" % ( total_wait, self.job.name, self.name ) )
243
256
if self.STR_TOTALCOUNT not in self.get_actions():
244
257
raise NoResults( "%s does not have any published results" % str(self) )
245
258
buildstatus = self.get_status()
246
if buildstatus in [ STATUS_FAIL, RESULTSTATUS_FAILURE, STATUS_ABORTED ]:
247
raise FailedNoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) )
248
259
if not self.get_actions()[self.STR_TOTALCOUNT]:
249
260
raise NoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) )
250
261
obj_results = ResultSet( result_url, build=self )
259
270
def get_actions(self):
261
272
for dct_action in self._data["actions"]:
273
if dct_action is None: continue
262
274
all_actions.update( dct_action )
263
275
return all_actions
265
277
def get_timestamp(self):
266
return self._data['timestamp']
279
Returns build timestamp in UTC
281
# Java timestamps are given in miliseconds since the epoch start!
282
naive_timestamp = datetime.datetime(*time.gmtime(self._data['timestamp']/1000.0)[:6])
283
return pytz.utc.localize(naive_timestamp)
285
def get_console(self):
287
Return the current state of the text console.
289
url = "%s/consoleText" % self.baseurl
290
return self.job.jenkins.requester.get_url(url).content
294
Stops the build execution if it's running
295
:return boolean True if succeded False otherwise or the build is not running
297
if self.is_running():
298
url = "%s/stop" % self.baseurl
299
self.job.jenkins.requester.post_and_confirm_status(url, data='')