Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --git a/puppet_compiler/prepare.py b/puppet_compiler/prepare.py
index 6a61b21..81f18a9 100644
--- a/puppet_compiler/prepare.py
+++ b/puppet_compiler/prepare.py
@@ -2,6 +2,7 @@ from contextlib import contextmanager
import json
import subprocess
import os
+import re
import shutil
import requests
from puppet_compiler import _log
@@ -116,7 +117,6 @@ class ManageCode(object):
def _fetch_change(self):
"""get changes from the change directly"""
- git = Git()
headers = {'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'}
change = requests.get(
@@ -128,19 +128,57 @@ class ManageCode(object):
json_data = change.text.split("\n")[-2:][0]
res = json.loads(json_data)
revision = res["revisions"].values()[0]["_number"]
+ project = res["project"]
ref = 'refs/changes/%02d/%d/%d' % (
self.change_id % 100,
self.change_id,
revision)
_log.debug(
- 'Downloading patch for change %d, revision %d',
- self.change_id, revision)
- git.fetch('-q', 'https://gerrit.wikimedia.org/r/operations/puppet',
- ref)
- git.checkout('FETCH_HEAD')
- git.pull('--rebase', 'origin', 'production')
+ 'Downloading patch for project %s, change %d, revision %d',
+ project, self.change_id, revision)
+
+ # Assumption:
+ # Gerrit suported repo names and branches:
+ # 1) operations/puppet - origin/production
+ # 2) operations/puppet/submodulename - origin/master
+ if project == 'operations/puppet':
+ self._pull_gerrit_revision(project, ref, 'production')
+ else:
+ self._update_submodule_repo(project, ref, 'master')
+
# Update submodules according to the last hash of the parent repo.
- git.submodule('update', '--init')
+ _log.debug('Running submodule init')
+ self.git.submodule('update', '--init')
+
+ def _update_submodule_repo(self, project, gerrit_rev, gerrit_origin_branch):
+ _log.debug('The change has not been filed for the main puppet repo.\
+ Checking submodule repository matching the regex operations/puppet/(\w*)')
+ submodule_name = self._get_submodule_name(project)
+ if submodule_name:
+ submodule_path = os.getcwd + '/modules/' + submodule_name
+ _log.debug('Submodule repository name: %s path: %s',
+ submodule_name, submodule_path)
+ if os.path.exists(submodule_path):
+ _log.debug('Executing git commands in %s', submodule_path)
+ with pushd(submodule_path):
+ self._pull_gerrit_revision(project, gerrit_rev, gerrit_origin_branch)
+ else:
+ _log.debug('Cannot access the submodule directory: %s', submodule_path)
+ return
+ else:
+ _log.debug('No gerrit repository found!')
+ return
+
+ def _get_submodule_name(self, project):
+ submodule_matcher = re.search('operations/puppet/(\w*)$', project)
+ if submodule_matcher:
+ return submodule_matcher.group(1)
+ return None
+
+ def _pull_gerrit_revision(self, project, revision, origin_branch):
+ self.git.fetch('-q', 'https://gerrit.wikimedia.org/r/' + project, revision)
+ self.git.checkout('FETCH_HEAD')
+ self.git.pull('--rebase', 'origin', origin_branch)
def _sh(command):
try:
diff --git a/puppet_compiler/tests/test_prepare.py b/puppet_compiler/tests/test_prepare.py
index add277b..d87515e 100644
--- a/puppet_compiler/tests/test_prepare.py
+++ b/puppet_compiler/tests/test_prepare.py
@@ -90,3 +90,20 @@ class TestManageCode(unittest.TestCase):
exim_priv = os.path.join(self.m.prod_dir, 'private/modules/privateexim')
exim_pub = os.path.join(self.m.prod_dir, 'src/modules/privateexim')
mock_symlink.assert_any_call(exim_priv, exim_pub)
+
+ def test_get_submodule_name(self):
+ assert None == self.m._get_submodule_name('not_a_valid_project')
+ assert None == self.m._get_submodule_name('/operations/puppet/not_a_valid_project/bla')
+ assert 'varnish' == self.m._get_submodule_name('/operations/puppet/varnish')
+
+
+
+
+
+
+
+
+
+
+
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment