From 8a956bcc74eed3d5386dda9cb98ee92b62b6523c Mon Sep 17 00:00:00 2001 From: Martin Konrad Date: Wed, 12 Mar 2014 16:00:46 -0400 Subject: [PATCH] SCM module: Add support for multiple Git remotes. Configuring multiple Git remotes is necessary to allow Jenkins to merge commits from a remote branch into a local branch. Note that this is not the same as specifying multiple SCMs which are cloned into separate directories. Change-Id: Ifef2da85fa22a979570d4ea2b6f30d4bd37da116 --- jenkins_jobs/modules/scm.py | 38 ++++++++++++----- tests/scm/fixtures/git-multiple-remotes.xml | 43 ++++++++++++++++++++ tests/scm/fixtures/git-multiple-remotes.yaml | 10 +++++ 3 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 tests/scm/fixtures/git-multiple-remotes.xml create mode 100644 tests/scm/fixtures/git-multiple-remotes.yaml diff --git a/jenkins_jobs/modules/scm.py b/jenkins_jobs/modules/scm.py index bbfc806e1..bce1fd758 100644 --- a/jenkins_jobs/modules/scm.py +++ b/jenkins_jobs/modules/scm.py @@ -44,6 +44,13 @@ def git(self, xml_parent, data): :arg str credentials-id: ID of credentials to use to connect (optional) :arg str refspec: refspec to fetch :arg str name: name to fetch + :arg list(str) remotes: list of remotes to set up (optional, only needed if + multiple remotes need to be set up) + + :Remote: * **url** (`string`) - url of remote repo + * **refspec** (`string`) - refspec to fetch (optional) + * **credentials-id** - ID of credentials to use to connect + (optional) :arg list(str) branches: list of branch specifiers to build :arg list(str) excluded-users: list of users to ignore revisions from when polling for changes. (if polling is enabled) @@ -133,16 +140,27 @@ def git(self, xml_parent, data): 'scm', {'class': 'hudson.plugins.git.GitSCM'}) XML.SubElement(scm, 'configVersion').text = '2' user = XML.SubElement(scm, 'userRemoteConfigs') - huser = XML.SubElement(user, 'hudson.plugins.git.UserRemoteConfig') - XML.SubElement(huser, 'name').text = data.get('name', 'origin') - if 'refspec' in data: - refspec = data['refspec'] - else: - refspec = '+refs/heads/*:refs/remotes/origin/*' - XML.SubElement(huser, 'refspec').text = refspec - XML.SubElement(huser, 'url').text = data['url'] - if 'credentials-id' in data: - XML.SubElement(huser, 'credentialsId').text = data['credentials-id'] + if 'remotes' not in data: + data['remotes'] = [{data.get('name', 'origin'): data}] + for remoteData in data['remotes']: + huser = XML.SubElement(user, 'hudson.plugins.git.UserRemoteConfig') + remoteName = remoteData.keys()[0] + XML.SubElement(huser, 'name').text = remoteName + remoteParams = remoteData.values()[0] + if 'refspec' in remoteParams: + refspec = remoteParams['refspec'] + else: + refspec = '+refs/heads/*:refs/remotes/' + remoteName + '/*' + XML.SubElement(huser, 'refspec').text = refspec + if 'url' in remoteParams: + remoteURL = remoteParams['url'] + else: + raise JenkinsJobsException('Must specify a url for git remote \"' + + remoteName + '"') + XML.SubElement(huser, 'url').text = remoteURL + if 'credentials-id' in remoteParams: + credentialsId = remoteParams['credentials-id'] + XML.SubElement(huser, 'credentialsId').text = credentialsId xml_branches = XML.SubElement(scm, 'branches') branches = data.get('branches', ['**']) for branch in branches: diff --git a/tests/scm/fixtures/git-multiple-remotes.xml b/tests/scm/fixtures/git-multiple-remotes.xml new file mode 100644 index 000000000..c657d5fd5 --- /dev/null +++ b/tests/scm/fixtures/git-multiple-remotes.xml @@ -0,0 +1,43 @@ + + + + 2 + + + internal + +refs/heads/*:refs/remotes/internal/* + ssh://git@internal.example.com/foobar.git + 01234567-89ab-cdef-0123-456789abcdef + + + github + +refs/heads/*:refs/remotes/github/* + https://github.com/exampleuser/foobar.git + + + + + github/master + + + + + false + false + false + false + false + true + false + false + Default + + + + + + false + + false + + \ No newline at end of file diff --git a/tests/scm/fixtures/git-multiple-remotes.yaml b/tests/scm/fixtures/git-multiple-remotes.yaml new file mode 100644 index 000000000..8011ce56f --- /dev/null +++ b/tests/scm/fixtures/git-multiple-remotes.yaml @@ -0,0 +1,10 @@ +scm: + - git: + remotes: + - internal: + url: ssh://git@internal.example.com/foobar.git + credentials-id: 01234567-89ab-cdef-0123-456789abcdef + - github: + url: https://github.com/exampleuser/foobar.git + branches: + - github/master \ No newline at end of file