Skip to content

Commit e9c524b

Browse files
committed
add changes command
1 parent 2c90f35 commit e9c524b

3 files changed

Lines changed: 40 additions & 11 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[![Requirements Status](https://requires.io/github/LD250/jenkins-cli-python/requirements.svg?branch=master)](https://requires.io/github/LD250/jenkins-cli-python/requirements/?branch=master)
66

77
**Based on**
8-
[python-jenkins](https://git.openstack.org/cgit/openstack/python-jenkins)
8+
[python-jenkins](https://github.com/openstack/python-jenkins)
99

1010
# Install:
1111
```bash
@@ -34,6 +34,7 @@ Server URL, Username and password may be specified either by the command line ar
3434
info Job info
3535
setbranch Set SCM branch
3636
stop Stop job
37-
console Show console for last build
37+
console Show console for the build
3838
builds Show builds for job
39+
changes Show build's changes
3940
```

jenkins_cli/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@ def main():
4646
stop_parser = subparsers.add_parser('stop', help='Stop job')
4747
stop_parser.add_argument('job_name', help='Job to stop')
4848

49-
console_parser = subparsers.add_parser('console', help='Show console for last build')
49+
console_parser = subparsers.add_parser('console', help='Show console for the build')
5050
console_parser.add_argument('job_name', help='Job to show console for')
5151
console_parser.add_argument('-b', '--build', help='Job build number to show console for (if omitted, last build number used)', default='')
5252
console_parser.add_argument('-n', help='Show first n num of the lines only(if n is negative, shows last n lines)', type=int)
5353
console_parser.add_argument('-i', help='Interactive console', default=False, action='store_true')
5454

55+
console_parser = subparsers.add_parser('changes', help="Show build's changes")
56+
console_parser.add_argument('job_name', help='Job to show changes for')
57+
console_parser.add_argument('-b', '--build', help='Job build number to show changes for (if omitted, last build number used)', default='')
58+
5559
args = parser.parse_args()
5660
try:
5761
if args.jenkins_command is None:

jenkins_cli/cli.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
ENDCOLLOR = '\033[0m'
3131
ANIME_SYMBOL = ['..', '>>']
32+
AUTHOR_COLLOR = '\033[94m'
33+
MSG_COLLOR = '\033[93m'
3234

3335
RESULT_TO_COLOR = {"FAILURE": 'red',
3436
"SUCCESS": 'blue',
@@ -234,23 +236,45 @@ def stop(self, args):
234236
else:
235237
print("%s job is not running" % job_name)
236238

237-
def _get_build_number(self, build_number):
238-
parsed_build_number = None
239+
def _get_build_number(self, job_name, build_number):
239240
if build_number:
240241
if build_number[0] == "#":
241242
build_number = build_number[1:]
242243
if build_number.isdigit():
243-
parsed_build_number = int(build_number)
244+
build_number = int(build_number)
244245
else:
245246
raise CliException('Build number must be in format 123')
246-
return parsed_build_number
247+
else:
248+
info = self.jenkins.get_job_info(job_name)
249+
build_number = info['lastBuild'].get('number')
250+
return build_number
251+
252+
def changes(self, args):
253+
job_name = self._check_job(args.job_name)
254+
build_number = self._get_build_number(job_name, args.build)
255+
build = self.jenkins.get_build_info(job_name, build_number)
256+
if 'changeSet' in build:
257+
changesets = build['changeSet'].get('items')
258+
if changesets:
259+
for change in changesets:
260+
params = {'rev': change['rev'],
261+
'msg': change['msg'],
262+
'author': change['author'].get('fullName', 'Unknown'),
263+
'is_merge': "MERGE" if change.get('merge') else '',
264+
'affected_files': len(change['affectedPaths']),
265+
'endcollor': ENDCOLLOR,
266+
'author_collor': AUTHOR_COLLOR,
267+
'msg_collor': MSG_COLLOR}
268+
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)
269+
else:
270+
print("%(job_name)s %(build_number)s has no changes" % {'job_name': job_name, 'build_number': build_number})
271+
else:
272+
raise CliException('Changesets not found for %s' % job_name)
273+
247274

248275
def console(self, args):
249276
job_name = self._check_job(args.job_name)
250-
info = self.jenkins.get_job_info(job_name)
251-
build_number = self._get_build_number(args.build)
252-
if not build_number:
253-
build_number = info['lastBuild'].get('number')
277+
build_number = self._get_build_number(job_name, args.build)
254278
console_out = self.jenkins.get_build_console_output(job_name, build_number)
255279
console_out = console_out.split('\n')
256280
last_line_num = len(console_out)

0 commit comments

Comments
 (0)