add job.get_build_metadata
authorJingjing Duan <jduan@twitter.com>
Wed, 4 Jun 2014 21:59:38 +0000 (14:59 -0700)
committerJingjing Duan <jduan@twitter.com>
Wed, 4 Jun 2014 21:59:40 +0000 (14:59 -0700)
For large builds, this method can be significantly faster!

jenkinsapi/build.py
jenkinsapi/job.py
jenkinsapi_tests/unittests/test_build.py
jenkinsapi_tests/unittests/test_job.py

index 556929ab1c9b0c37153c4ff35e0884f931c897f8..781370fb572f870a6f9b95ee3e8762033c1ecbce 100644 (file)
@@ -34,16 +34,20 @@ class Build(JenkinsBase):
     STR_TOTALCOUNT = "totalCount"
     STR_TPL_NOTESTS_ERR = "%s has status %s, and does not have any test results"
 
-    def __init__(self, url, buildno, job):
+    def __init__(self, url, buildno, job, depth=1):
+        """
+        depth=1 is for backward compatibility consideration
+        """
         assert type(buildno) == int
         self.buildno = buildno
         self.job = job
+        self.depth = depth
         JenkinsBase.__init__(self, url)
 
     def _poll(self):
         # For build's we need more information for downstream and upstream builds
         # so we override the poll to get at the extra data for build objects
-        url = self.python_api_url(self.baseurl) + '?depth=1'
+        url = '%s?depth=%s' % (self.python_api_url(self.baseurl), self.depth)
         return self.get_data(url)
 
     def __str__(self):
index 697a525321b2018d4a36c34f1d7e002e8f7206bb..e7e7891a6be90c9065f1408edb8fc3249b3b3cc8 100644 (file)
@@ -389,6 +389,16 @@ class Job(JenkinsBase, MutableJenkinsThing):
         url = self.get_build_dict()[buildnumber]
         return Build(url, buildnumber, job=self)
 
+    def get_build_metadata(self, buildnumber):
+        """
+        Get the build metadata for a given build number. For large builds with
+        tons of tests, this method is faster than get_build by returning less
+        data.
+        """
+        assert type(buildnumber) == int
+        url = self.get_build_dict()[buildnumber]
+        return Build(url, buildnumber, job=self, depth=0)
+
     def __getitem__(self, buildnumber):
         return self.get_build(buildnumber)
 
index c3529ad2087dcf9e735fc6a986950316b7374b98..0e67858f7f0ce9643ad2035f203aa493c1a5c03d 100644 (file)
@@ -73,6 +73,11 @@ class test_build(unittest.TestCase):
         self.assertEquals(self.b.get_duration().microseconds, 782000)
         self.assertEquals(str(self.b.get_duration()), '0:00:05.782000')
 
+    @mock.patch.object(Build, 'get_data')
+    def test_build_depth(self, get_data_mock):
+        build = Build('http://halob:8080/job/foo/98', 98, self.j, depth=0)
+        get_data_mock.assert_called_with('http://halob:8080/job/foo/98/api/python?depth=0')
+
     ## TEST DISABLED - DOES NOT WORK
     # def test_downstream(self):
     #     expected = ['SingleJob','MultipleJobs']
index 84d2e2e7220b330b30ecf1a05f91209c960417e3..3e427c66adeb2e62cee71e02f4c68920aa05f123 100644 (file)
@@ -5,6 +5,7 @@ try:
 except ImportError:
     import unittest
 
+import jenkinsapi
 from jenkinsapi import config
 from jenkinsapi.job import Job
 from jenkinsapi.jenkinsbase import JenkinsBase
@@ -279,5 +280,23 @@ class TestJob(unittest.TestCase):
         self.assertEquals(len(params), 2)
         self.assertEquals(params, ['param1', 'param2'])
 
+    def test_get_build(self):
+        buildnumber = 1
+        with mock.patch('jenkinsapi.job.Build') as build_mock:
+            instance = build_mock.return_value
+            build = self.j.get_build(buildnumber)
+            self.assertEquals(build, instance)
+            build_mock.assert_called_with('http://halob:8080/job/foo/1/',
+                                          buildnumber, job=self.j)
+
+    def test_get_build_metadata(self):
+        buildnumber = 1
+        with mock.patch('jenkinsapi.job.Build') as build_mock:
+            instance = build_mock.return_value
+            build = self.j.get_build_metadata(buildnumber)
+            self.assertEquals(build, instance)
+            build_mock.assert_called_with('http://halob:8080/job/foo/1/',
+                                          buildnumber, job=self.j, depth=0)
+
 if __name__ == '__main__':
     unittest.main()