def library_merge_submodules(pull_request_id, repo_name, access_token): print() print() git_root = get_git_root() git_fetch(git_root) versions = get_lib_versions(git_root) for i, v in enumerate(versions): pv = previous_v(v, versions) ov = out_v(v, versions) v_branch = "branch-{}.{}.{}".format(*ov) v_tag = "v{}.{}.{}".format(*ov) git_sequence = int(get_sequence_number(pull_request_id)) n_branch = 'pullrequest/temp/{0}/{1}/{2}'\ .format(pull_request_id, str(git_sequence), v_branch) print() print("Was:", pv, "Now updating", (v_branch, v_tag), "with", n_branch) print('-'*20, flush=True) # Get us back to a very clean tree. # git('reset --hard HEAD', git_root) git_clean(git_root) # Checkout the right branch git('checkout {0}'.format(v_branch), git_root) print("Now reseting ", v_branch, " to ", n_branch) git('reset --hard origin/{0}'.format(n_branch), git_root) print("Now Pushing", v_branch) git('push -f origin {0}:{0}'.format(v_branch), git_root) for i in range(git_sequence + 1): git('push origin --delete pullrequest/temp/{0}/{1}/{2}' .format(pull_request_id, str(i), v_branch), git_root) git_clean(git_root) n_branch = 'pullrequest/temp/{0}/{1}/master'.format(pull_request_id, str(git_sequence)) git('checkout master', git_root) print("Now reseting master to ", n_branch) git('reset --hard origin/{0}'.format(n_branch), git_root) print("Now Pushing", v_branch) git('push -f origin master:master', git_root) for i in range(git_sequence + 1): git('push origin --delete pullrequest/temp/{0}/{1}/master' .format(pull_request_id, str(i)), git_root) git_issue_close(repo_name, pull_request_id, access_token) comment_body = """\ Thank you for your pull request. This pull request will be closed, because \ the Pull-Request Merger has successfully applied it internally to all \ branches. """ git_issue_comment(repo_name, pull_request_id, comment_body, access_token)
def library_clean_submodules(all_open_pull_requests): print() print() print("Cleaning up pull request branches for closed pull requests.") git_root = get_git_root() git_fetch(git_root) all_branches = subprocess.check_output('git branch -r', shell=True).decode('utf-8').split() print("All branchs:", all_branches) for br in all_branches: if "origin/pullrequest/temp/" in br \ and br.split('/')[3] not in all_open_pull_requests: print('Deleting ', br) git('push origin --delete {0}'.format(br.split('origin/', 1)[1]), git_root)
def library_rebase_submodules(pull_request_id): print() print() git_root = get_git_root() git_fetch(git_root) versions = get_lib_versions(git_root) for i, v in enumerate(versions): pv = previous_v(v, versions) ov = out_v(v, versions) v_branch = "branch-{}.{}.{}".format(*ov) v_tag = "v{}.{}.{}".format(*ov) git_sequence = int(get_sequence_number(pull_request_id)) n_branch = 'pullrequest/temp/{0}/{1}/{2}'.format( pull_request_id, str(git_sequence), v_branch) print() print("Was:", pv, "Now rebasing ", n_branch, " with ", (v_branch, v_tag)) print('-'*20, flush=True) # Get us back to a very clean tree. # git('reset --hard HEAD', git_root) git_clean(git_root) # Checkout the right branch git('checkout {0}'.format(n_branch), git_root) git('rebase origin/{0}'.format(v_branch), git_root) print("Now Pushing", n_branch) git('push -f origin {0}:{0}'.format(n_branch), git_root) git_clean(git_root) n_branch = 'pullrequest/temp/{0}/{1}/master'.format( pull_request_id, str(git_sequence)) git('checkout {0}'.format(n_branch), git_root) git('rebase origin/master', git_root) print("Now Pushing", n_branch) git('push -f origin {0}:{0}'.format(n_branch), git_root)
def handle_pull_requests(args): print(args) assert len(args) == 6 dmp = args.pop(0) repo_name = args.pop(0) dmp = args.pop(0) access_token = args.pop(0) dmp = args.pop(0) external_path = args.pop(0) print(dmp) print() print() git_root = get_git_root() git_fetch(git_root) r = requests.get( 'https://api.github.com/repos/{0}/pulls?state=open'.format(repo_name)) all_open_pull_requests = \ sorted(list(set([str(item['number']) for item in r.json()]))) pr_hash_list = subprocess.check_output( "git ls-remote origin 'pull/*/head'", shell=True).decode('utf-8').split('\n') print("All Open Pull Requests: ", all_open_pull_requests) library_clean_submodules(all_open_pull_requests) for pull_request_id in all_open_pull_requests: print() print("Processing:", str(pull_request_id)) print('-' * 20, flush=True) commit_hash = '' for pr_hash in pr_hash_list: if 'pull/{0}/head'.format(pull_request_id) in pr_hash: commit_hash = pr_hash.split()[0] break print("head commit hash: ", commit_hash) print() print("Getting Patch") print() http = urllib3.PoolManager() patch_request = \ http.request('GET', 'https://github.com/{0}/pull/{1}.patch' .format(repo_name, pull_request_id)) if patch_request.status != 200: print('Unable to get patch. Skipping...') continue patchfile = '{0}/{1}.patch'.format(external_path, pull_request_id) with open(patchfile, 'w') as f: f.write(patch_request.data.decode('utf-8')) print("Will try to apply: ", patchfile) if library_patch_submodules(patchfile, pull_request_id, repo_name, access_token, commit_hash): print() print("Pull Request Handled: ", str(pull_request_id)) print('-' * 20, flush=True) if label_exists(repo_name, pull_request_id, 'ready-to-merge'): print("PR {0} is now ready to be merged..".format( pull_request_id)) library_merge_submodules(pull_request_id, repo_name, access_token) print("Resetting Branches") reset_branches(git_root) print("Reset Branches Done!") print('-' * 20, flush=True) print("Done Creating PR branches!") print('-' * 20, flush=True)
def library_patch_submodules( patchfile, pull_request_id, repo_name, access_token, commit_hash): assert os.path.exists(patchfile), patchfile assert os.path.isfile(patchfile), patchfile assert pull_request_id.isdigit(), pull_request_id print() print() git_root = get_git_root() git_fetch(git_root) versions = get_lib_versions(git_root) failed = True apply_idx = 0 for i, v in enumerate(versions): pv = previous_v(v, versions) ov = out_v(v, versions) v_branch = "branch-{}.{}.{}".format(*ov) v_tag = "v{}.{}.{}".format(*ov) print() print("Was:", pv, "Now patching", (v_branch, v_tag), "with", patchfile) print('-'*20, flush=True) # Get us back to a very clean tree. # git('reset --hard HEAD', git_root) git_clean(git_root) # Checkout the right branch git('checkout {0}'.format(v_branch), git_root) diff_pos = 'branch-{}.{}.{}'.format(*pv) # Update the contents if v == versions[apply_idx]: if git('am {}'.format(patchfile), git_root, can_fail=True) is False: apply_idx += 1 git('am --abort', git_root) failed = False continue # Create the merge commit git('merge {} --no-ff --no-commit --strategy=recursive' .format(diff_pos), git_root) git('commit -C HEAD@{1}', git_root) if failed: return False git('branch -D master', git_root, can_fail=True) git('branch master', git_root) print('='*75, flush=True) old_git_sequence = int(get_sequence_number(pull_request_id)) sequence_increment = 1 if old_git_sequence != -1: old_pr_branch = \ 'pullrequest/temp/{0}/{1}/master'.format( pull_request_id, str(old_git_sequence)) git('checkout {0}'.format(old_pr_branch), git_root) internal_patch = subprocess.check_output( 'git diff {0}..master'.format(old_pr_branch), shell=True).decode('utf-8').strip() print(internal_patch) print('**********************') if not len(internal_patch): sequence_increment = 0 print(sequence_increment) git_sequence = old_git_sequence + sequence_increment n_branch_links = "" for i, v in enumerate(versions): ov = out_v(v, versions) v_branch = "branch-{}.{}.{}".format(*ov) v_tag = "v{}.{}.{}".format(*ov) print() print("Now Pushing", (v_branch, v_tag)) print('-'*20, flush=True) n_branch = 'pullrequest/temp/{0}/{1}/{2}'.format( pull_request_id, str(git_sequence), v_branch) branch_link = "https://github.com/{0}/tree/{1}".format( repo_name, n_branch) n_branch_links += "\n- {0}".format(branch_link) print("Now Pushing", n_branch) if git('push -f origin {0}:{1}'.format(v_branch, n_branch), git_root, can_fail=True) is False: print("""\ Pull Request {0} is coming from a fork and trying to update the workflow. \ We will skip it!!! \ """) return False print() n_branch = 'pullrequest/temp/{0}/{1}/master'.format( pull_request_id, str(git_sequence)) branch_link = "https://github.com/{0}/tree/{1}".format(repo_name, n_branch) n_branch_links += "\n- {0}".format(branch_link) print("Now Pushing", n_branch) print('-'*20, flush=True) if git('push -f origin master:{0}'.format(n_branch), git_root, can_fail=True) is False: print("""\ Pull Request {0} is coming from a fork and trying to update the workflow. \ We will skip it!!! \ """) return False if sequence_increment: comment_body = """\ The latest commit of this PR, commit {0} has been applied to the branches, \ please check the links here: {1} """.format(commit_hash, n_branch_links) git_issue_comment(repo_name, pull_request_id, comment_body, access_token) return True
def handle_pull_requests(args): print(args) assert len(args) == 6 dmp = args.pop(0) repo_name = args.pop(0) dmp = args.pop(0) access_token = args.pop(0) dmp = args.pop(0) external_path = args.pop(0) print(dmp) print() print() git_root = get_git_root() git_fetch(git_root) prs_open_url = \ 'https://api.github.com/repos/{0}/pulls?state=open'.format(repo_name) curl_grep = \ "curl -sS {0} | grep -o -E 'pull/[[:digit:]]+'".format(prs_open_url) comp_cmd = "{0} | sed 's/pull\\///g' | sort | uniq".format(curl_grep) all_open_pull_requests = subprocess.check_output( comp_cmd, shell=True).decode('utf-8').split() print("All Open Pull Requests: ", all_open_pull_requests) library_clean_submodules(all_open_pull_requests) for pull_request_id in all_open_pull_requests: print() print("Processing:", str(pull_request_id)) print('-'*20, flush=True) commit_hash = subprocess.check_output( "git ls-remote origin 'pull/*/head'| grep 'pull/{0}/head'".format( pull_request_id) + " | tail -1 | awk '{ print $1F }'", shell=True).decode('utf-8') print() print("Getting Patch") print() run('wget https://github.com/{0}/pull/{1}.patch' .format(repo_name, pull_request_id)) run('mv {0}.patch {1}/'.format(pull_request_id, external_path)) patchfile = '{0}/{1}.patch'.format(external_path, pull_request_id) print("Will try to apply: ", patchfile) if library_patch_submodules( patchfile, pull_request_id, repo_name, access_token, commit_hash): print() print("Pull Request Handled: ", str(pull_request_id)) print('-'*20, flush=True) if label_exists(repo_name, pull_request_id, 'ready-to-merge'): print("PR {0} is now ready to be merged.." .format(pull_request_id)) library_merge_submodules( pull_request_id, repo_name, access_token) print("Resetting Branches") reset_branches(git_root) print("Reset Branches Done!") print('-'*20, flush=True) print("Done Creating PR branches!") print('-'*20, flush=True)