diff --git a/jenkinsapi/job.py b/jenkinsapi/job.py index c1e86dcc..1a343f44 100644 --- a/jenkinsapi/job.py +++ b/jenkinsapi/job.py @@ -46,6 +46,7 @@ def __init__(self, url, name, jenkins_obj): self._revmap = None self._config = None self._element_tree = None + self._scm_prefix = "" self._scm_map = { 'hudson.scm.SubversionSCM': 'svn', 'hudson.plugins.git.GitSCM': 'git', @@ -54,13 +55,13 @@ def __init__(self, url, name, jenkins_obj): } self._scmurlmap = { 'svn': lambda element_tree: list(element_tree.findall(SVN_URL)), - 'git': lambda element_tree: list(element_tree.findall(GIT_URL)), + 'git': lambda element_tree: list(element_tree.findall(self._scm_prefix + GIT_URL)), 'hg': lambda element_tree: list(element_tree.findall(HG_URL)), None: lambda element_tree: [] } self._scmbranchmap = { 'svn': lambda element_tree: [], - 'git': lambda element_tree: list(element_tree.findall(GIT_BRANCH)), + 'git': lambda element_tree: list(element_tree.findall(self._scm_prefix + GIT_BRANCH)), 'hg': self._get_hg_branch, None: lambda element_tree: [] } @@ -500,7 +501,16 @@ def load_config(self): def get_scm_type(self): element_tree = self._get_config_element_tree() - scm_class = element_tree.find('scm').get('class') + scm_element = element_tree.find('scm') + if not scm_element: + multibranch_scm_prefix = \ + "properties/org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty/branch/" + multibranch_path = multibranch_scm_prefix + "scm" + scm_element = element_tree.find(multibranch_path) + if scm_element: + # multibranch pipeline. + self._scm_prefix = multibranch_scm_prefix + scm_class = scm_element.get('class') if scm_element else None scm = self._scm_map.get(scm_class) if not scm: raise NotSupportSCM( diff --git a/jenkinsapi_tests/unittests/test_job_scm_hg.py b/jenkinsapi_tests/unittests/test_job_scm_hg.py index 3b94fbfa..bd35422d 100644 --- a/jenkinsapi_tests/unittests/test_job_scm_hg.py +++ b/jenkinsapi_tests/unittests/test_job_scm_hg.py @@ -95,6 +95,85 @@ def configtree_with_default_branch(self): ''' return config_node + def configtree_multibranch_git(self): + config_node = ''' + + false + + + + + H H * * H(6-7) + + + + + + SUCCESS + 0 + BLUE + true + + + + + + + -1 + 5 + -1 + 5 + + + + + a2d4bcda-6141-4af2-8088-39139a147902 + + master + GIT + + + 2 + + + origin + +refs/heads/master:refs/remotes/origin/master + ssh://git@bitbucket.site/project-name/reponame.git + jenkins-stash + + + + + master + + + false + + https://bitbucket.site/projects/project-name/repos/reponame + + + + + false + + + + + + + + + Jenkinsfile + + + false + + ''' + return config_node + @mock.patch.object(Job, 'get_config', configtree_with_branch) def test_hg_attributes(self): expected_url = ['http://cm5/hg/sandbox/v01.0/int'] @@ -106,6 +185,13 @@ def test_hg_attributes(self): def test_hg_attributes_default_branch(self): self.assertEqual(self.j.get_scm_branch(), ['default']) + @mock.patch.object(Job, 'get_config', configtree_multibranch_git) + def test_git_attributes_multibranch(self): + expected_url = ['ssh://git@bitbucket.site/project-name/reponame.git'] + self.assertEqual(self.j.get_scm_type(), 'git') + self.assertEqual(self.j.get_scm_url(), expected_url) + self.assertEqual(self.j.get_scm_branch(), ['master']) + if __name__ == '__main__': unittest.main()