Skip to content

Commit 82b4893

Browse files
committed
Merge pull request #20 from LD250/build-statuses
Build statuses for solving #3
2 parents fe597de + 8ffce6f commit 82b4893

4 files changed

Lines changed: 58 additions & 23 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ Server URL, Username and password may be specified either by the command line ar
3232
# Available commands:
3333
```bash
3434
{jobs,queue,building,start,info,set_branch,stop,console}
35-
jobs Show all jobs and their status
35+
jobs Show all jobs and their statuses
3636
queue Shows builds queue
3737
building Build executor status
3838
start Start job
3939
info Job info
40-
set_branch Job info
40+
set_branch Set SCM branch
4141
stop Stop job
42-
console Show job history
42+
console Show console for last build
4343
```

jenkins_cli/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
from jenkins import JenkinsException
33

4-
from jenkins_cli.cli import JenkinsCli, CliException
4+
from jenkins_cli.cli import JenkinsCli, CliException, get_jobs_legend
55

66

77
def main():
@@ -17,7 +17,10 @@ def main():
1717

1818
subparsers = parser.add_subparsers(title='Available commands', dest='jenkins_command')
1919

20-
jobs_parser = subparsers.add_parser('jobs', help='Show all jobs and their status')
20+
jobs_parser = subparsers.add_parser('jobs',
21+
help='Show all jobs and their statuses',
22+
formatter_class=argparse.RawTextHelpFormatter,
23+
description="Status description:\n\n" + "\n".join(get_jobs_legend()))
2124
jobs_parser.add_argument('-a', help='Show only active jobs', default=False, action='store_true')
2225

2326
subparsers.add_parser('queue', help='Shows builds queue')
@@ -30,15 +33,15 @@ def main():
3033
start_parser = subparsers.add_parser('info', help='Job info')
3134
start_parser.add_argument('job_name', help='Job to to get info for')
3235

33-
set_branch = subparsers.add_parser('set_branch', help='Job info')
36+
set_branch = subparsers.add_parser('setbranch', help='Set SCM branch')
3437
set_branch.add_argument('job_name', help='Job to to set branch')
35-
set_branch.add_argument('branch_name', help='Name of the branch')
38+
set_branch.add_argument('branch_name', help='Name of the SCM branch')
3639

3740
stop_parser = subparsers.add_parser('stop', help='Stop job')
3841
stop_parser.add_argument('job_name', help='Job to stop')
3942

40-
console_parser = subparsers.add_parser('console', help='Show job history')
41-
console_parser.add_argument('job_name', help='Job to show history for')
43+
console_parser = subparsers.add_parser('console', help='Show console for last build')
44+
console_parser.add_argument('job_name', help='Job to show console for')
4245
console_parser.add_argument('-n', help='Show first n num of the lines only(if n is negative, shows last n lines)', type=int)
4346
console_parser.add_argument('-i', help='Interactive console', default=False, action='store_true')
4447

jenkins_cli/cli.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,47 @@
66
import socket
77
from xml.etree import ElementTree
88

9-
COLORS = {'blue': '\033[94m',
10-
'green': '\033[92m',
11-
'red': '\033[91m',
12-
'yellow': '\033[93m',
13-
'disabled': '\033[97m',
14-
'endcollor': '\033[0m',
15-
'aborted': '\033[97m(aborted)',
16-
'yellow_anime': '\033[93m(running)',
17-
'blue_anime': '\033[94m(running)',
18-
'red_anime': '\033[91m(running)'}
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+
}
25+
26+
# 'green': '\033[92m',
27+
28+
29+
ENDCOLLOR = '\033[0m'
30+
ANIME_SYMBOL = ['..', '>>']
31+
32+
33+
def get_formated_status(job_color, format_pattern="%(color)s%(symbol)s%(run_status)s%(endcollor)s"):
34+
color_status = job_color.split('_')
35+
color = color_status[0]
36+
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}
43+
44+
45+
def get_jobs_legend():
46+
pattern = "%(color)s%(symbol)s..%(endcollor)s -> %(descr)s"
47+
legend = [get_formated_status(job_color, pattern) for job_color in STATUSES.keys()]
48+
legend.append(".>> -> Build in progress")
49+
return legend
1950

2051

2152
class CliException(Exception):
@@ -77,7 +108,8 @@ def run_command(self, args):
77108
def jobs(self, args):
78109
jobs = self._get_jobs(args)
79110
for job in jobs:
80-
print("%s***%s %s" % (COLORS.get(job['color'], job['color']), COLORS['endcollor'], job['name']))
111+
formated_status = get_formated_status(job['color'])
112+
print(formated_status + " " + job['name'])
81113

82114
def _get_jobs(self, args):
83115
jobs = self.jenkins.get_jobs()

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, COLORS
14+
from jenkins_cli.cli import JenkinsCli, CliException, STATUSES, 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 = "%s***%s Job1" % (COLORS.get(jobs[0]['color'], jobs[0]['color']), COLORS['endcollor'])
139-
arg2 = "%s***%s Job2" % (COLORS.get(jobs[1]['color'], jobs[1]['color']), COLORS['endcollor'])
138+
arg1 = "%sS..%s Job1" % (STATUSES[jobs[0]['color']]['color'], ENDCOLLOR)
139+
arg2 = "%sD..%s Job2" % (STATUSES[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)