From 0d7b59f5d24ece9d697ddb47d2785c432b94e9c3 Mon Sep 17 00:00:00 2001
From: Ardar Martian <ardar@126.com>
Date: Wed, 10 Aug 2016 09:26:47 +0000
Subject: [PATCH] get_running_builds failed when server has a path

When the server has an URL path such as http://example.org/ci/, the
get_running_builds() would fail to extract the job name from the
executor informations.  It most probably fails when using CloudBees
folder as well.

Relax the regex used to detect the job name by changing match() with
search().

Update JenkinsListRunningBuildsTest so it craft the URLs based on the
test scenario (make_url) instead of hardcoding them.  That would have
caught the bug. See 2aa1a5f1 which introduced the scenarios.

Signed-off-by: Antoine Musso <hashar@free.fr>
Change-Id: I1b69adf121ee74e3336ac9bd1d4d55797fb72de5
---
 jenkins/__init__.py | 2 +-
 tests/test_build.py | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/jenkins/__init__.py b/jenkins/__init__.py
index 2cabd3c..34a91d8 100755
--- a/jenkins/__init__.py
+++ b/jenkins/__init__.py
@@ -1149,7 +1149,7 @@ class Jenkins(object):
                     executor_number = executor['number']
                     build_number = executable['number']
                     url = executable['url']
-                    m = re.match(r'/job/([^/]+)/.*', urlparse(url).path)
+                    m = re.search(r'/job/([^/]+)/.*', urlparse(url).path)
                     job_name = m.group(1)
                     builds.append({'name': job_name,
                                    'number': build_number,
diff --git a/tests/test_build.py b/tests/test_build.py
index c5ce49f..497ae30 100644
--- a/tests/test_build.py
+++ b/tests/test_build.py
@@ -259,7 +259,7 @@ class JenkinsListRunningBuildsTest(JenkinsTestBase):
             "number": 1,
             "result": None,
             "timestamp": 1442262342729,
-            "url": "https://localhost/job/test/1/",
+            "url": self.make_url('job/test/1/'),
             "builtOn": "",
             "changeSet": {
                 "items": [],
@@ -293,7 +293,7 @@ class JenkinsListRunningBuildsTest(JenkinsTestBase):
                            'number': 1,
                            'node': '(master)',
                            'executor': 1,
-                           'url': 'https://localhost/job/test/1/'}], builds)
+                           'url': self.make_url('job/test/1/')}], builds)
 
     @patch.object(jenkins.Jenkins, 'get_node_info')
     @patch.object(jenkins.Jenkins, 'get_nodes')
@@ -338,7 +338,7 @@ class JenkinsListRunningBuildsTest(JenkinsTestBase):
             "number": 15,
             "result": None,
             "timestamp": 1442262342729,
-            "url": "https://localhost/job/test/15/",
+            "url": self.make_url("job/test/15/"),
             "builtOn": "",
             "changeSet": {
                 "items": [],
@@ -372,7 +372,7 @@ class JenkinsListRunningBuildsTest(JenkinsTestBase):
                            'number': 15,
                            'node': 'foo-slave',
                            'executor': 0,
-                           'url': 'https://localhost/job/test/15/'}], builds)
+                           'url': self.make_url('job/test/15/')}], builds)
 
     @patch.object(jenkins.Jenkins, 'get_node_info')
     @patch.object(jenkins.Jenkins, 'get_nodes')