@@ -216,18 +216,21 @@ def _get_build_duration(self, build):
216216 def builds (self , args ):
217217 job_name = self ._check_job (args .job_name )
218218 job_info = self .jenkins .get_job_info (job_name , 1 )
219- for build in job_info ['builds' ][:10 ]:
220- color = RESULT_TO_COLOR .get (build ['result' ], 'unknown' )
221- if build ['building' ]:
222- color = color + "_anime"
223- pattern = "%(color)s%(symbol)s%(run_status)s #%(number)s%(endcollor)s %(duration)s (%(changeset_count)s commits)"
224- changeset_count = len (self ._get_build_changesets (build ))
225- status = get_formated_status (color ,
226- format_pattern = pattern ,
227- extra_params = {'number' : build ['number' ],
228- 'duration' : str (self ._get_build_duration (build )).split ('.' )[0 ],
229- 'changeset_count' : changeset_count })
230- print (status )
219+ if not job_info ['builds' ]:
220+ print ("%(job_name)s has no builds" % {'job_name' : job_name })
221+ else :
222+ for build in job_info ['builds' ][:10 ]:
223+ color = RESULT_TO_COLOR .get (build ['result' ], 'unknown' )
224+ if build ['building' ]:
225+ color = color + "_anime"
226+ pattern = "%(color)s%(symbol)s%(run_status)s #%(number)s%(endcollor)s %(duration)s (%(changeset_count)s commits)"
227+ changeset_count = len (self ._get_build_changesets (build ))
228+ status = get_formated_status (color ,
229+ format_pattern = pattern ,
230+ extra_params = {'number' : build ['number' ],
231+ 'duration' : str (self ._get_build_duration (build )).split ('.' )[0 ],
232+ 'changeset_count' : changeset_count })
233+ print (status )
231234
232235 def stop (self , args ):
233236 job_name = self ._check_job (args .job_name )
@@ -240,6 +243,9 @@ def stop(self, args):
240243 print ("%s job is not running" % job_name )
241244
242245 def _get_build_number (self , job_name , build_number ):
246+ info = self .jenkins .get_job_info (job_name )
247+ if not info ['lastBuild' ]:
248+ return None
243249 if build_number :
244250 if build_number [0 ] == "#" :
245251 build_number = build_number [1 :]
@@ -248,53 +254,57 @@ def _get_build_number(self, job_name, build_number):
248254 else :
249255 raise CliException ('Build number must be in format 123' )
250256 else :
251- info = self .jenkins .get_job_info (job_name )
252257 build_number = info ['lastBuild' ].get ('number' )
253258 return build_number
254259
255260 def changes (self , args ):
256261 job_name = self ._check_job (args .job_name )
257262 build_number = self ._get_build_number (job_name , args .build )
258- build = self .jenkins .get_build_info (job_name , build_number )
259- if 'changeSet' in build :
260- changesets = build ['changeSet' ].get ('items' )
261- if changesets :
262- for change in changesets :
263- params = {'rev' : change ['rev' ],
264- 'msg' : change ['msg' ],
265- 'author' : change ['author' ].get ('fullName' , 'Unknown' ),
266- 'is_merge' : "MERGE" if change .get ('merge' ) else '' ,
267- 'affected_files' : len (change ['affectedPaths' ]),
268- 'endcollor' : ENDCOLLOR ,
269- 'author_collor' : AUTHOR_COLLOR ,
270- 'msg_collor' : MSG_COLLOR }
271- print ("%(rev)s %(msg_collor)s%(msg)s%(endcollor)s by %(author_collor)s%(author)s%(endcollor)s affected %(affected_files)s files %(is_merge)s" % params )
272- else :
273- print ("%(job_name)s %(build_number)s has no changes" % {'job_name' : job_name , 'build_number' : build_number })
263+ if build_number is None :
264+ print ("Can't show changes. %(job_name)s has no builds" % {'job_name' : job_name })
274265 else :
275- raise CliException ('Changesets not found for %s' % job_name )
276-
266+ build = self .jenkins .get_build_info (job_name , build_number )
267+ if 'changeSet' in build :
268+ changesets = build ['changeSet' ].get ('items' )
269+ if changesets :
270+ for index , change in enumerate (changesets ):
271+ params = {'num' : index + 1 ,
272+ 'msg' : change ['msg' ],
273+ 'author' : change ['author' ].get ('fullName' , 'Unknown' ),
274+ 'is_merge' : "MERGE" if change .get ('merge' ) else '' ,
275+ 'affected_files' : len (change ['affectedPaths' ]),
276+ 'endcollor' : ENDCOLLOR ,
277+ 'author_collor' : AUTHOR_COLLOR ,
278+ 'msg_collor' : MSG_COLLOR }
279+ print ("%(num)s. %(msg_collor)s%(msg)s%(endcollor)s by %(author_collor)s%(author)s%(endcollor)s affected %(affected_files)s files %(is_merge)s" % params )
280+ else :
281+ print ("%(job_name)s %(build_number)s has no changes" % {'job_name' : job_name , 'build_number' : build_number })
282+ else :
283+ raise CliException ('Changesets not found for %s' % job_name )
277284
278285 def console (self , args ):
279286 job_name = self ._check_job (args .job_name )
280287 build_number = self ._get_build_number (job_name , args .build )
281- console_out = self .jenkins .get_build_console_output (job_name , build_number )
282- console_out = console_out .split ('\n ' )
283- last_line_num = len (console_out )
284- if args .n :
285- console_out = console_out [args .n :] if args .n < 0 else console_out [:args .n ]
286- print ("\n " .join (console_out ))
287- if args .i :
288- build_info = self .jenkins .get_build_info (job_name , build_number )
289- while build_info ['building' ]:
290- console_out = self .jenkins .get_build_console_output (job_name , build_number )
291- console_out = console_out .split ('\n ' )
292- new_line_num = len (console_out )
293- if new_line_num > last_line_num :
294- print ("\n " .join (console_out [last_line_num :]))
295- last_line_num = new_line_num
296- time .sleep (3 )
288+ if build_number is None :
289+ print ("Can't show console output. %(job_name)s has no builds" % {'job_name' : job_name })
290+ else :
291+ console_out = self .jenkins .get_build_console_output (job_name , build_number )
292+ console_out = console_out .split ('\n ' )
293+ last_line_num = len (console_out )
294+ if args .n :
295+ console_out = console_out [args .n :] if args .n < 0 else console_out [:args .n ]
296+ print ("\n " .join (console_out ))
297+ if args .i :
297298 build_info = self .jenkins .get_build_info (job_name , build_number )
299+ while build_info ['building' ]:
300+ console_out = self .jenkins .get_build_console_output (job_name , build_number )
301+ console_out = console_out .split ('\n ' )
302+ new_line_num = len (console_out )
303+ if new_line_num > last_line_num :
304+ print ("\n " .join (console_out [last_line_num :]))
305+ last_line_num = new_line_num
306+ time .sleep (3 )
307+ build_info = self .jenkins .get_build_info (job_name , build_number )
298308
299309 def building (self , args ):
300310 args .a = True
0 commit comments