Skip to content

Commit 9880d61

Browse files
committed
+ builds command output
1 parent 3a4a949 commit 9880d61

2 files changed

Lines changed: 64 additions & 30 deletions

File tree

jenkins_cli/cli.py

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,57 @@
66
import socket
77
from xml.etree import ElementTree
88

9-
STATUSES = {'blue': {'symbol': 'S',
10-
'color': '\033[94m',
11-
'descr': 'Stable'},
12-
'red': {'symbol': 'F',
13-
'color': '\033[91m',
14-
'descr': 'Failed'},
15-
'yellow': {'symbol': 'U',
16-
'color': '\033[93m',
17-
'descr': 'Unstable'},
18-
'disabled': {'symbol': 'D',
19-
'color': '\033[97m',
20-
'descr': 'Disabled'},
21-
'aborted': {'symbol': 'A',
22-
'color': '\033[97m',
23-
'descr': 'Aborted'}
24-
}
9+
STATUSES_COLOR = {'blue': {'symbol': 'S',
10+
'color': '\033[94m',
11+
'descr': 'Stable'},
12+
'red': {'symbol': 'F',
13+
'color': '\033[91m',
14+
'descr': 'Failed'},
15+
'yellow': {'symbol': 'U',
16+
'color': '\033[93m',
17+
'descr': 'Unstable'},
18+
'disabled': {'symbol': 'D',
19+
'color': '\033[97m',
20+
'descr': 'Disabled'},
21+
'unknown': {'symbol': '.',
22+
'color': '\033[97m',
23+
'descr': 'Unknown'},
24+
'aborted': {'symbol': 'A',
25+
'color': '\033[97m',
26+
'descr': 'Aborted'}
27+
}
2528

2629
# 'green': '\033[92m',
2730

2831

2932
ENDCOLLOR = '\033[0m'
3033
ANIME_SYMBOL = ['..', '>>']
3134

35+
RESULT_TO_COLOR = {"FAILURE": 'red',
36+
"SUCCESS": 'blue',
37+
"UNSTABLE": 'yellow',
38+
"ABORTED": 'aborted',
39+
"DISABLED": 'aborted'
40+
}
3241

33-
def get_formated_status(job_color, format_pattern="%(color)s%(symbol)s%(run_status)s%(endcollor)s"):
42+
43+
def get_formated_status(job_color, format_pattern="%(color)s%(symbol)s%(run_status)s%(endcollor)s", extra_params={}):
3444
color_status = job_color.split('_')
3545
color = color_status[0]
3646
run_status = color_status[1] if len(color_status) == 2 else None
37-
status = STATUSES[color]
38-
return format_pattern % {'color': status['color'],
39-
'symbol': status['symbol'],
40-
'descr': status['descr'],
41-
'run_status': ANIME_SYMBOL[run_status == 'anime'],
42-
'endcollor': ENDCOLLOR}
47+
status = STATUSES_COLOR[color]
48+
params = {'color': status['color'],
49+
'symbol': status['symbol'],
50+
'descr': status['descr'],
51+
'run_status': ANIME_SYMBOL[run_status == 'anime'],
52+
'endcollor': ENDCOLLOR}
53+
params.update(extra_params)
54+
return format_pattern % params
4355

4456

4557
def get_jobs_legend():
4658
pattern = "%(color)s%(symbol)s..%(endcollor)s -> %(descr)s"
47-
legend = [get_formated_status(job_color, pattern) for job_color in STATUSES.keys()]
59+
legend = [get_formated_status(job_color, pattern) for job_color in STATUSES_COLOR.keys()]
4860
legend.append(".>> -> Build in progress")
4961
return legend
5062

@@ -189,12 +201,34 @@ def start(self, args):
189201
start_status = self.jenkins.build_job(job_name)
190202
print("%s: %s" % (job_name, 'started' if not start_status else start_status))
191203

204+
def _get_build_changesets(self, build):
205+
if 'changeSet' in build and 'items' in build['changeSet']:
206+
return build['changeSet']['items']
207+
else:
208+
return []
209+
210+
def _get_build_duration(self, build):
211+
return datetime.timedelta(milliseconds=build["duration"])
212+
192213
def builds(self, args):
193214
job_name = self._check_job(args.job_name)
194215
job_info = self.jenkins.get_job_info(job_name, 1)
195-
print(job_info)
196-
# start_status = self.jenkins.build_job(job_name)
197-
# print("%s: %s" % (job_name, 'started' if not start_status else start_status))
216+
for build in job_info['builds'][:10]:
217+
color = RESULT_TO_COLOR.get(build['result'], 'unknown')
218+
if build['building']:
219+
color = color + "_anime"
220+
pattern = "%(color)s%(symbol)s%(run_status)s #%(number)s%(endcollor)s %(duration)s (%(changeset_count)s commits)"
221+
changeset_count = len(self._get_build_changesets(build))
222+
status = get_formated_status(color,
223+
format_pattern=pattern,
224+
extra_params={'number': build['number'],
225+
'duration': str(self._get_build_duration(build)).split('.')[0],
226+
'changeset_count': changeset_count})
227+
print(status)
228+
# "actions": [
229+
#{
230+
#"causes": [
231+
# "number": 17191,
198232

199233
def stop(self, args):
200234
job_name = self._check_job(args.job_name)

tests/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import jenkins
1313

14-
from jenkins_cli.cli import JenkinsCli, CliException, STATUSES, ENDCOLLOR
14+
from jenkins_cli.cli import JenkinsCli, CliException, STATUSES_COLOR, ENDCOLLOR
1515

1616
GIT_SCM_XML = """<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n<project>\n <actions/>\n <description></description>\n <keepDependencies>false</keepDependencies>\n <properties/>\n <scm class="hudson.plugins.git.GitSCM" plugin="git@2.4.2">\n <configVersion>2</configVersion>\n <userRemoteConfigs>\n <hudson.plugins.git.UserRemoteConfig>\n <url>https://github.com/LD250/jenkins-cli-python/</url>\n </hudson.plugins.git.UserRemoteConfig>\n </userRemoteConfigs>\n <branches>\n <hudson.plugins.git.BranchSpec>\n <name>cli-tests</name>\n </hudson.plugins.git.BranchSpec>\n </branches>\n <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>\n <submoduleCfg class="list"/>\n <extensions/>\n </scm>\n <canRoam>true</canRoam>\n <disabled>false</disabled>\n <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>\n <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>\n <triggers/>\n <concurrentBuild>false</concurrentBuild>\n <builders>\n <hudson.tasks.Shell>\n <command></command>\n </hudson.tasks.Shell>\n <jenkins.plugins.shiningpanda.builders.VirtualenvBuilder plugin="shiningpanda@0.22">\n <pythonName>System-CPython-2.7</pythonName>\n <home></home>\n <clear>true</clear>\n <systemSitePackages>false</systemSitePackages>\n <nature>shell</nature>\n <command>pip install -U pip\npip install -U setuptools\npip install -U wheel\npip install -r requirements.txt\npip list -o\n\nflake8 jenkins_cli\npython setup.py test</command>\n <ignoreExitCode>false</ignoreExitCode>\n </jenkins.plugins.shiningpanda.builders.VirtualenvBuilder>\n </builders>\n <publishers/>\n <buildWrappers/>\n</project>
1717
"""
@@ -135,8 +135,8 @@ def test_jobs(self, patched_get_jobs):
135135
patched_get_jobs.return_value = jobs
136136
self.args.a = False
137137
JenkinsCli(self.args).jobs(self.args)
138-
arg1 = "%sS..%s Job1" % (STATUSES[jobs[0]['color']]['color'], ENDCOLLOR)
139-
arg2 = "%sD..%s Job2" % (STATUSES[jobs[1]['color']]['color'], ENDCOLLOR)
138+
arg1 = "%sS..%s Job1" % (STATUSES_COLOR[jobs[0]['color']]['color'], ENDCOLLOR)
139+
arg2 = "%sD..%s Job2" % (STATUSES_COLOR[jobs[1]['color']]['color'], ENDCOLLOR)
140140
self.patched_print.assert_has_calls([mock.call(arg1)], [mock.call(arg2)])
141141
self.patched_print.reset_mock()
142142
self.args.a = True

0 commit comments

Comments
 (0)