Jenkins.get_running_builds() Not Working -- Fixed it for you guys

Bug #1650669 reported by Samuel
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Python Jenkins
New
Undecided
Unassigned

Bug Description

There was a bug in the get_running_builds(). The regular expression was not functioning properly and you were looking for m.group(1) instead of m.group(0)...

Here is what the final code I wrote looks like.
Now it works perfectly
I put comments beside what I modified

    def get_running_builds(self):
        '''Return list of running builds.

        Each build is a dict with keys 'name', 'number', 'url', 'node',
        and 'executor'.

        :returns: List of builds,
          ``[ { str: str, str: int, str:str, str: str, str: int} ]``

        Example::
            >>> builds = server.get_running_builds()
            >>> print(builds)
            [{'node': 'foo-slave', 'url': 'https://localhost/job/test/15/',
              'executor': 0, 'name': 'test', 'number': 15}]
        '''
        builds = []
        nodes = self.get_nodes()
        for node in nodes:
            # the name returned is not the name to lookup when
            # dealing with master :/
            if node['name'] == 'master':
                node_name = '(master)'
            else:
                node_name = node['name']
            try:
                info = self.get_node_info(node_name, depth=2)
            except JenkinsException as e:
                # Jenkins may 500 on depth >0. If the node info comes back
                # at depth 0 treat it as a node not running any jobs.
                if ('[500]' in str(e) and
                        self.get_node_info(node_name, depth=0)):
                    continue
                else:
                    raise
            for executor in info['executors']:
                executable = executor['currentExecutable']
                if executable:
                    executor_number = executor['number']
                    build_number = executable['number']
                    url = executable['url']
                    m = re.match(r'.*/job/.*', urlparse(url).path) # --> Changed this
                    job_name = m.group(0) # --> Changed this
                    builds.append({'name': job_name,
                                   'number': build_number,
                                   'url': url,
                                   'node': node_name,
                                   'executor': executor_number})
        return builds

Revision history for this message
Khai Do (zaro0508) wrote :

Would it be possible for you to provide info:
1. python-jenkins version
2. description of the use case that this patch attempts to fix.
3. Is this fix related to change https://review.openstack.org/#/c/353339/

if #3 does not fix it would it be possible for you to push this patch to Gerrit, our code review system?

Revision history for this message
Nadav Goldin (ngoldin) wrote :

Same issue -
Since upgrading to Jenkins v2.32.1, get_running_builds() fails:
...
  File "/home/ngoldin/graphite/lib/python2.7/site-packages/jenkins/__init__.py", line 1014, in get_running_builds
    build_number = executable['number']
KeyError: 'number'

I'm using latest master: python-jenkins==0.4.14.dev14 and as noted Python 2.7.

Revision history for this message
Bill Anderson (bill.l.anderson) wrote :

I've fixed this bug in my installation with a simpler code change. Just add .* to the front of the existing pattern:

m = re.match(r'.*/job/([^/]+)/.*', urlparse(url).path)

Line 1153 of Jenkins/__init__.py
Running on RHEL 6.4 (Santiago)
Python 2.6.6 (r266:84292, Nov 21 2013, 10:50:32)
Jenkins 1.625.3

Revision history for this message
marc (myoung34) wrote :

Im running into this and am unable to use our chatbot against our jenkins as a result.

>>> pprint(server.get_running_builds())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/app/venv/lib/python3.4/site-packages/jenkins/__init__.py", line 1151, in get_running_builds
    build_number = executable['number']
KeyError: 'number'

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.