def init_environment(): # Create the agent first globals()['puppet_agent'] = PuppetAgent(workspace = WORKSPACE, stub_branch = identity) puppet_agent.reset_branch(AGENT_BRANCH) puppet_agent[AGENT_BRANCH]( rewrite_file("VERSION", AGENT_BRANCH.replace("x", "30")), commit("Rewrite the version file for the agent repo!") ) # Now create each of the components for component, (constructor, branch) in COMPONENT_BRANCHES.items(): edited_name = component.replace("-", "_") globals()[edited_name] = constructor(workspace = WORKSPACE, puppet_agent = puppet_agent, stub_branch = identity, update_ref = False) globals()[edited_name].reset_branch(branch) globals()[edited_name].update_url(branch) prev_version = branch.replace("x", "30") next_version = branch.replace("x", "31") globals()[edited_name].in_repo(sequence( lambda : git('push --delete origin %s' % prev_version), lambda : git('tag --delete origin %s' % prev_version), )) globals()[edited_name][branch]( bump_version(prev_version), bump_version(next_version) ) tag_sha = globals()[edited_name].in_branch(branch, exec_stdout('git', 'rev-parse', 'HEAD^')).decode("utf-8") globals()[edited_name].in_repo(sequence( lambda : git('tag -a %s %s -m "Tagging to %s!"' % (prev_version, tag_sha, prev_version)), lambda : git('push origin --tags') )) puppet_agent[AGENT_BRANCH]( bump_component(component, "refs/tags/%s" % prev_version), commit("Pinning '%s' back to previous tag!" % component) )
def update_file_action(file_path): temp_file = file_path + ".tmp" with open(file_path, 'r') as f: with open(temp_file, 'w') as ftemp: modify(f, ftemp) os.rename(temp_file, file_path) git('add %s' % file_path)
def setup_happy_cases(): init_environment() # facter, pxp-agent, and puppet will be the ones that will be bumped for (component, branch) in [(facter, "3.6.x"), (puppet, "4.10.x"), (pxp_agent, "1.5.x")]: next_version = branch.replace("x", "31") component[branch](bump_version(next_version), update_ref=True) component.in_repo( sequence( lambda: git('push --delete origin %s' % next_version), lambda: git('tag --delete origin %s' % next_version), ))
def __prepare_stubs(self): # Ensure the branches are checked out branch_exists = lambda git_branch : self.in_repo(lambda : git('show-branch %s' % git_branch)) == 0 for branch in self.branches: stub = self.branches[branch] if branch_exists(stub): continue if branch_exists("remotes/origin/%s" % stub): self.in_repo(lambda : git("checkout -b %s origin/%s" % (stub, stub))) continue self.in_repo(lambda : git("checkout -b %s" % stub))
def simulate_workflow(repo, last_passing_ref, project): ticket_summaries = [ "Add component A", "Add component B", "Add component C" ] issues = [ create_jira_ticket(project, summary).key for summary in ticket_summaries ] repo.in_repo(lambda: git("reset --hard %s" % last_passing_ref)) if repo != puppet_agent: repo.update_url(COMPONENT_BRANCHES[repo.name]) repo["master"]( add_feature("maint_one", "(maint) Added maint_one"), add_feature("one_one", "(%s) Added one_one feature" % issues[0]), add_feature("two_one", "(%s) Added two_one feature" % issues[1]), add_feature("one_two", "(%s) Added one_two feature" % issues[0]), add_feature("one_three", "(%s) Added one_three feature" % issues[0]), add_feature("two_two", "(%s) Added two_two feature" % issues[1]), add_feature("packaging", "(packaging) Committed some packaging work"), add_feature("no_label", "Committed some non-label work"), add_feature("three_one", "(%s) Added three_one feature" % issues[2]), push=True, push_agent=True, )
def __init__(self, repo_name, branches, github_user = GITHUB_USERNAME, **kwargs): if not github_user: raise Exception("github_user cannot be empty! Use the environment variable GITHUB_USERNAME instead!") self.name = repo_name self.workspace = kwargs.get('workspace') if not self.workspace: self.workspace = mkdtemp(prefix = 'tmp-workspace') self.root = os.path.join(self.workspace, self.name) # Map of <base-branch> -> <stubbed-branch>. This is to avoid messing with special # stuff that people might have on their forks when simulating the workflow. self.stub_branch = kwargs.get('stub_branch', lambda branch: kwargs.get('stub_prefix', BRANCH_PREFIX) + "-" + branch) self.branches = dict([[branch, self.stub_branch(branch)] for branch in branches]) metadata = kwargs.get('metadata', {}) for key in metadata: self.__class__._add_repo_metadata(self.name, key, metadata[key]) self.prompt_push = kwargs.get('prompt_push', False) self.remotes = kwargs.get('remotes', { 'upstream' : 'puppetlabs' }) self.github_user = github_user if os.path.exists(self.root): self.__prepare_stubs() return None git('clone %s %s' % (self._git_url(self.github_user, self.name), self.root)) with in_directory(self.root): for (remote_name, remote_user) in self.remotes.items(): git('remote add %s %s' % (remote_name, self._git_url(remote_user, self.name))) git('fetch %s' % remote_name) # NOTE: Bit hacky, but it's a way to run some initialization. This should be cleaned # up later initialize_repo = kwargs.get("initialize_with", None) if initialize_repo: initialize_repo() self.__prepare_stubs()
def in_repo_action(): print("\n\nSTARTING CONTEXT ...") git('checkout %s' % stub) self.__print_context()(self.name, branch) return do_action(self.name, branch)
git, git_head, sequence, exec_stdout) from workflow.actions.repo_actions import (bump_version, bump_component) from workflow.actions.file_actions import (new_file, modify_line, rewrite_file) import os # The agent branch itself AGENT_BRANCH = "5.3.x" RELEASE_BRANCH = AGENT_BRANCH.replace("x", "50") + "-release" GITHUB_DIR = "%s/%s" % (os.environ['HOME'], "GitHub") WORKSPACE = "%s/%s" % (GITHUB_DIR, "puppet-agent-workflow/workspaces/PA-1762") # Create the agent first globals()['puppet_agent'] = PuppetAgent(workspace=WORKSPACE, stub_branch=identity, use_private_fork=True) puppet_agent.reset_branch(AGENT_BRANCH) puppet_agent.in_repo( sequence( lambda: git('branch -D %s' % RELEASE_BRANCH), lambda: git('checkout -b %s' % RELEASE_BRANCH), lambda: git('push --set-upstream origin %s --force' % RELEASE_BRANCH))) puppet_agent.in_repo( sequence( lambda: new_file("feature_one", "first feature")("", ""), lambda: new_file("feature_two", "second feature")("", ""), lambda: commit("Added some features to set-up stuff to merge-up") ("", ""), lambda: git("push")))
globals()[edited_name].in_repo( sequence( lambda: git('tag -a %s %s -m "Tagging to %s!"' % (prev_version, tag_sha, prev_version)), lambda: git('push origin --tags'))) puppet_agent[AGENT_BRANCH]( bump_component(component, "refs/tags/%s" % prev_version), commit("Pinning '%s' back to previous tag!" % component)) def setup_happy_cases(): init_environment() # facter, pxp-agent, and puppet will be the ones that will be bumped for (component, branch) in [(facter, "3.6.x"), (puppet, "4.10.x"), (pxp_agent, "1.5.x")]: next_version = branch.replace("x", "31") component[branch](bump_version(next_version), update_ref=True) component.in_repo( sequence( lambda: git('push --delete origin %s' % next_version), lambda: git('tag --delete origin %s' % next_version), )) setup_happy_cases() puppet_agent.in_repo( sequence( lambda: git('branch -D %s' % AGENT_VERSION), lambda: git('checkout -b %s' % AGENT_VERSION), lambda: git('push --set-upstream origin %s --force' % AGENT_VERSION)))
def remove_file(file_path): removal_action = lambda _file_path: os.remove(_file_path) or git( 'rm %s' % _file_path) return __crud_action(file_path, removal_action, __check_file_exists)
def rename_action(file_path): os.rename(file_path, new_path) git('add %s' % file_path) git('add %s' % new_path)
def create_file_action(file_path): with open(file_path, 'w') as f: write_to(f) git('add %s' % file_path)