Skip to content

Commit ed15ef0

Browse files
committed
flesh out create_remote and functions from push_branch_to_github _easyconfigs_pr_common in github.py
1 parent 6f88b5e commit ed15ef0

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

easybuild/tools/github.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -808,35 +808,55 @@ def _easyconfigs_pr_common(paths, ecs, start_branch=None, pr_branch=None, start_
808808
# commit
809809
git_repo.index.commit(commit_msg)
810810

811-
# push to GitHub
812-
github_url = '[email protected]:%s/%s.git' % (target_account, pr_target_repo)
811+
push_branch_to_github(git_repo, target_account, pr_target_repo, pr_branch)
812+
813+
return file_info, deleted_paths, git_repo, pr_branch, diff_stat
814+
815+
816+
def create_remote(git_repo, account, repo):
817+
"""Create remote in specified git working directory for specified account & repository."""
818+
819+
github_url = '[email protected]:%s/%s.git' % (account, repo)
813820
salt = ''.join(random.choice(ascii_letters) for _ in range(5))
814-
remote_name = 'github_%s_%s' % (target_account, salt)
821+
remote_name = 'github_%s_%s' % (account, salt)
822+
823+
try:
824+
remote = git_repo.create_remote(remote_name, github_url)
825+
except GitCommandError as err:
826+
raise EasyBuildError("Failed to create remote %s for %s: %s", remote_name, github_url, err)
827+
828+
return remote
829+
830+
831+
def push_branch_to_github(git_repo, target_account, target_repo, branch):
832+
"""Push specified branch to GitHub from specified git repository."""
833+
834+
# push to GitHub
835+
remote = create_remote(git_repo, target_account, target_repo)
815836

816837
dry_run = build_option('dry_run') or build_option('extended_dry_run')
817838

818-
push_branch_msg = "pushing branch '%s' to remote '%s' (%s)" % (pr_branch, remote_name, github_url)
839+
github_url = '[email protected]:%s/%s.git' % (target_account, target_repo)
840+
841+
push_branch_msg = "pushing branch '%s' to remote '%s' (%s)" % (branch, remote.name, github_url)
819842
if dry_run:
820843
print_msg(push_branch_msg + ' [DRY RUN]', log=_log)
821844
else:
822845
print_msg(push_branch_msg, log=_log)
823846
try:
824-
my_remote = git_repo.create_remote(remote_name, github_url)
825-
res = my_remote.push(pr_branch)
847+
res = remote.push(branch)
826848
except GitCommandError as err:
827-
raise EasyBuildError("Failed to push branch '%s' to GitHub (%s): %s", pr_branch, github_url, err)
849+
raise EasyBuildError("Failed to push branch '%s' to GitHub (%s): %s", branch, github_url, err)
828850

829851
if res:
830852
if res[0].ERROR & res[0].flags:
831853
raise EasyBuildError("Pushing branch '%s' to remote %s (%s) failed: %s",
832-
pr_branch, my_remote, github_url, res[0].summary)
854+
branch, remote, github_url, res[0].summary)
833855
else:
834-
_log.debug("Pushed branch %s to remote %s (%s): %s", pr_branch, my_remote, github_url, res[0].summary)
856+
_log.debug("Pushed branch %s to remote %s (%s): %s", branch, remote, github_url, res[0].summary)
835857
else:
836858
raise EasyBuildError("Pushing branch '%s' to remote %s (%s) failed: empty result",
837-
pr_branch, my_remote, github_url)
838-
839-
return file_info, deleted_paths, git_repo, pr_branch, diff_stat
859+
branch, remote, github_url)
840860

841861

842862
def is_patch_for(patch_name, ec):

test/framework/github.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,35 @@ def test_det_account_branch_for_pr(self):
663663
self.assertEqual(account, 'migueldiascosta')
664664
self.assertEqual(branch, 'fix_inject_checksums')
665665

666+
def test_push_branch_to_github(self):
667+
"""Test push_branch_to_github."""
668+
669+
build_options = {'dry_run': True}
670+
init_config(build_options=build_options)
671+
672+
git_repo = gh.init_repo(self.test_prefix, GITHUB_REPO)
673+
branch = 'test123'
674+
675+
self.mock_stderr(True)
676+
self.mock_stdout(True)
677+
gh.setup_repo(git_repo, GITHUB_USER, GITHUB_REPO, 'master')
678+
git_repo.create_head(branch, force=True)
679+
gh.push_branch_to_github(git_repo, GITHUB_USER, GITHUB_REPO, branch)
680+
stderr = self.get_stderr()
681+
stdout = self.get_stdout()
682+
self.mock_stderr(True)
683+
self.mock_stdout(True)
684+
685+
self.assertEqual(stderr, '')
686+
687+
github_path = '%s/%s.git' % (GITHUB_USER, GITHUB_REPO)
688+
pattern = r'^' + '\n'.join([
689+
r"== fetching branch 'master' from https://github.com/%s\.\.\." % github_path,
690+
r"== pushing branch 'test123' to remote 'github_.*' \([email protected]:%s\) \[DRY RUN\]" % github_path,
691+
]) + r'$'
692+
regex = re.compile(pattern)
693+
self.assertTrue(regex.match(stdout.strip()), "Pattern '%s' doesn't match: %s" % (regex.pattern, stdout))
694+
666695

667696
def suite():
668697
""" returns all the testcases in this module """

0 commit comments

Comments
 (0)