Added build.get_revision_branch method (Git only)
authorAleksey Maksimov <ctpeko3a@gmail.com>
Wed, 20 Nov 2013 02:24:02 +0000 (10:24 +0800)
committerAleksey Maksimov <ctpeko3a@gmail.com>
Wed, 20 Nov 2013 02:24:02 +0000 (10:24 +0800)
jenkinsapi/build.py
jenkinsapi_tests/unittests/test_build_scm_git.py

index c0cfade..acf023c 100644 (file)
@@ -62,6 +62,10 @@ class Build(JenkinsBase):
         vcs = self._data['changeSet']['kind'] or 'git'
         return getattr(self, '_get_%s_rev' % vcs, lambda: None)()
 
+    def get_revision_branch(self):
+        vcs = self._data['changeSet']['kind'] or 'git'
+        return getattr(self, '_get_%s_rev_branch' % vcs, lambda: None)()
+
     def _get_svn_rev(self):
         warnings.warn("This untested function may soon be removed from Jenkinsapi.")
         maxRevision = 0
@@ -74,17 +78,27 @@ class Build(JenkinsBase):
         # which have lastBuiltRevision in them
         _actions = [x for x in self._data['actions']
                     if x and "lastBuiltRevision" in x]
-        # FIXME So this code returns the first item found in the filtered
-        # list. Why not just:
-        #     `return _actions[0]["lastBuiltRevision"]["SHA1"]`
-        for item in _actions:
-            revision = item["lastBuiltRevision"]["SHA1"]
-            return revision
+
+        return _actions[0]["lastBuiltRevision"]["SHA1"]
 
     def _get_hg_rev(self):
         warnings.warn("This untested function may soon be removed from Jenkinsapi.")
         return [x['mercurialNodeName'] for x in self._data['actions'] if 'mercurialNodeName' in x][0]
 
+    def _get_svn_rev_branch(self):
+        raise NotImplementedError('_get_svn_rev_branch is not yet implemented')
+
+    def _get_git_rev_branch(self):
+        # Sometimes we have None as part of actions. Filter those actions
+        # which have lastBuiltRevision in them
+        _actions = [x for x in self._data['actions']
+                    if x and "lastBuiltRevision" in x]
+
+        return _actions[0]["lastBuiltRevision"]["branch"]
+
+    def _get_hg_rev_branch(self):
+        raise NotImplementedError('_get_hg_rev_branch is not yet implemented')
+
     def get_duration(self):
         return datetime.timedelta(milliseconds=self._data["duration"])
 
index 7a339fe..cd31855 100644 (file)
@@ -99,7 +99,20 @@ class test_build(unittest.TestCase):
         Can we extract git build revision data from a build object?
         """
         self.assertIsInstance(self.b.get_revision(), basestring)
-        self.assertEquals(self.b.get_revision(), '7def9ed6e92580f37d00e4980c36c4d36e68f702')
+        self.assertEquals(self.b.get_revision(),
+                          '7def9ed6e92580f37d00e4980c36c4d36e68f702')
+
+    def test_git_revision_branch(self):
+        """
+        Can we extract git build branch from a build object?
+        """
+        self.assertIsInstance(self.b.get_revision_branch(), list)
+        self.assertEquals(len(self.b.get_revision_branch()), 1)
+        self.assertIsInstance(self.b.get_revision_branch()[0], dict)
+        self.assertEquals(self.b.get_revision_branch()[0]['SHA1'],
+                          '7def9ed6e92580f37d00e4980c36c4d36e68f702')
+        self.assertEquals(self.b.get_revision_branch()[0]['name'],
+                          'origin/unstable')
 
 if __name__ == '__main__':
     unittest.main()