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..7548c78 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 @@ -128,20 +129,63 @@ 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': + _pull_gerrit_revision(project, ref, 'production') + else: + _log.debug('The change has not been filed for the main puppet repo.\ + Checking submodule repository matching the regex operations/puppet/(\w*)') + submodule_name = _get_submodule_name(project) + if submodule_name: + submodule_path = 'modules/' + submodule_name + _log.debug('Submodule repository name: %s path: %s', + submodule_name, submodule_path) + base_repo_dir = os.getcwd + if os.path.exists(base_repo_dir + '/' + submodule_path): + _log.debug('Change directory to: %s', + base_repo_dir + '/' + submodule_path_) + os.chdir(base_repo_dir + '/' + submodule_path) + _pull_gerrit_revision(project, ref, 'master') + _log.debug('Change directory to: %s', base_repo_dir) + os.chdir(base_repo_dir) + else: + _log.debug('Cannot access the submodule directory: %s', + base_repo_dir + '/' + submodule_path) + return + else: + _log.debug('No gerrit repository found!') + return + # Update submodules according to the last hash of the parent repo. + _log.debug('Running submodule init') git.submodule('update', '--init') + + def _get_submodule_name(project): + submodule_matcher = re.search('operations/puppet/(\w*)', project) + if submodule_matcher: + return submodule_matcher.group(1) + return None + + + def _pull_gerrit_revision(project, revision, origin_branch): + git.fetch('-q', 'https://gerrit.wikimedia.org/r/' + project, revision) + git.checkout('FETCH_HEAD') + git.pull('--rebase', 'origin', origin_branch) + + def _sh(command): try: subprocess.check_call(command, shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment