Skip to content

Commit

Permalink
job.py: manage scm data for multibranch pipelines (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismaes87 authored and lechat committed Oct 31, 2019
1 parent dce4073 commit 8e93675
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 3 deletions.
16 changes: 13 additions & 3 deletions jenkinsapi/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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: []
}
Expand Down Expand Up @@ -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(
Expand Down
86 changes: 86 additions & 0 deletions jenkinsapi_tests/unittests/test_job_scm_hg.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,85 @@ def configtree_with_default_branch(self):
'''
return config_node

def configtree_multibranch_git(self):
config_node = '''
<flow-definition plugin="[email protected]">
<keepDependencies>false</keepDependencies>
<properties>
<org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
<triggers>
<hudson.triggers.TimerTrigger>
<spec>H H * * H(6-7)</spec>
</hudson.triggers.TimerTrigger>
<jenkins.triggers.ReverseBuildTrigger>
<spec></spec>
<upstreamProjects></upstreamProjects>
<threshold>
<name>SUCCESS</name>
<ordinal>0</ordinal>
<color>BLUE</color>
<completeBuild>true</completeBuild>
</threshold>
</jenkins.triggers.ReverseBuildTrigger>
</triggers>
</org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
<jenkins.model.BuildDiscarderProperty>
<strategy class="hudson.tasks.LogRotator">
<daysToKeep>-1</daysToKeep>
<numToKeep>5</numToKeep>
<artifactDaysToKeep>-1</artifactDaysToKeep>
<artifactNumToKeep>5</artifactNumToKeep>
</strategy>
</jenkins.model.BuildDiscarderProperty>
<org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty
plugin="[email protected]">
<branch plugin="[email protected]">
<sourceId>a2d4bcda-6141-4af2-8088-39139a147902</sourceId>
<head class="com.cloudbees.jenkins.plugins.bitbucket.BranchSCMHead"
plugin="[email protected]">
<name>master</name>
<repositoryType>GIT</repositoryType>
</head>
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<name>origin</name>
<refspec>+refs/heads/master:refs/remotes/origin/master</refspec>
<url>ssh:https://[email protected]/project-name/reponame.git</url>
<credentialsId>jenkins-stash</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches class="singleton-list">
<hudson.plugins.git.BranchSpec>
<name>master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<browser class="hudson.plugins.git.browser.BitbucketWeb">
<url>https://bitbucket.site/projects/project-name/repos/reponame</url>
</browser>
<submoduleCfg class="empty-list"/>
<extensions>
<jenkins.plugins.git.GitSCMSourceDefaults>
<includeTags>false</includeTags>
</jenkins.plugins.git.GitSCMSourceDefaults>
</extensions>
</scm>
<properties/>
</branch>
</org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty>
</properties>
<definition class="org.jenkinsci.plugins.workflow.multibranch.SCMBinder"
plugin="[email protected]">
<scriptPath>Jenkinsfile</scriptPath>
</definition>
<triggers/>
<disabled>false</disabled>
</flow-definition>
'''
return config_node

@mock.patch.object(Job, 'get_config', configtree_with_branch)
def test_hg_attributes(self):
expected_url = ['https://cm5/hg/sandbox/v01.0/int']
Expand All @@ -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:https://[email protected]/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()

0 comments on commit 8e93675

Please sign in to comment.