Skip to content

Commit bde07ad

Browse files
committed
add git branch detection
1 parent a42e3d3 commit bde07ad

3 files changed

Lines changed: 25 additions & 10 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ python:
66
install:
77
- pip install flake8 mock==1.3.0
88
script:
9-
- flake8 .
9+
- flake8 jenkins_cli
1010
- python setup.py test

jenkins_cli/cli.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ def _check_job(self, job_name):
9494
raise CliException('Job name does not esist')
9595
return job_name
9696

97+
def _get_scm_name_and_node(self, xml_root):
98+
scm_name = 'UnknownSCM'
99+
branch_node = None
100+
try:
101+
scm = xml_root.find('scm')
102+
if scm.attrib['class'] == 'hudson.plugins.mercurial.MercurialSCM':
103+
scm_name = 'Mercurial'
104+
branch_node = scm.find('revision')
105+
elif scm.attrib['class'] == 'hudson.plugins.git.GitSCM':
106+
scm_name = 'Git'
107+
branch_node = scm.find('branches').find('hudson.plugins.git.BranchSpec').find('name')
108+
except AttributeError:
109+
pass
110+
return (scm_name, branch_node)
111+
97112
def info(self, args):
98113
job_name = self._check_job(args.job_name)
99114
job_info = self.jenkins.get_job_info(job_name, 1)
@@ -105,21 +120,21 @@ def info(self, args):
105120
"Last success build name: %s\n"
106121
"Build started: %s\n"
107122
"Building now: %s\n"
108-
"Mercurial branch set: %s")
123+
"%s branch set to: %s")
109124
xml = self.jenkins.get_job_config(job_name)
110125
root = ElementTree.fromstring(xml.encode('utf-8'))
111-
rev = 'Not Known'
112-
scm = root.find('scm')
113-
if scm is not None:
114-
revision = scm.find('revision')
115-
if revision is not None:
116-
rev = revision.text
126+
scm_name, branch_node = self._get_scm_name_and_node(root)
127+
if branch_node is not None:
128+
branch_name = branch_node.text
129+
else:
130+
branch_name = 'Unknown branch'
117131
print(info % (last_build.get('fullDisplayName', 'Not Built'),
118132
last_build.get('result', 'Not Built'),
119133
last_success_build.get('fullDisplayName', 'Not Built'),
120134
datetime.datetime.fromtimestamp(last_build['timestamp'] / 1000) if last_build else 'Not built',
121135
'Yes' if last_build.get('building') else 'No',
122-
rev))
136+
scm_name,
137+
branch_name))
123138

124139
def set_branch(self, args):
125140
job_name = self._check_job(args.job_name)

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ envlist = py27, py34
88

99
[testenv]
1010
commands =
11-
flake8 .
11+
flake8 jenkins_cli
1212
{envpython} setup.py test
1313
deps =
1414
flake8

0 commit comments

Comments
 (0)