Skip to content

Commit

Permalink
refactor to share code
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Jan 22, 2025
1 parent 6a3e19c commit e5143c6
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions src/gh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ def get_head_version(repo_path, sha1='HEAD'):
return run_command_with_output(f"git -C {repo_path} describe --abbrev=0 --tags {sha1}").strip()


def checkout_randomly_named_branch(repo_path, prefix):
def checkout_randomly_named_branch(repo_path, prefix, base_branch=None):
if base_branch and not run_command(f"git -C {repo_path} checkout {base_branch}"):
return False

branch = f"{prefix}-{str(uuid.uuid4())}"
if run_command(f"git -C {repo_path} checkout -B {branch}"):
return branch
Expand Down Expand Up @@ -432,9 +435,6 @@ def update_fetchcontent(proj_name, dep_name, sha1, repo_path, remote, branch):
Like update_submodule() but bumps a FetchContent dependency.
'''

if not run_command(f"git -C {repo_path} checkout {branch}"):
return False

tmp_branch = checkout_randomly_named_branch(repo_path, "gh-actions")
if not tmp_branch:
return False
Expand Down Expand Up @@ -462,19 +462,10 @@ def update_fetchcontent(proj_name, dep_name, sha1, repo_path, remote, branch):

commit_msg = f"\"Bump {dep_name} from {versions['current_version']} to {tag_name}\""

if not run_command(f"git -C {repo_path} commit --author \"KDAB GitHub Actions <gh@kdab>\" -m {commit_msg}"):
return False

if not run_command(f"git -C {repo_path} push {remote} {tmp_branch}"):
return False

if not run_command(f"git -C {repo_path} push --set-upstream {remote} {tmp_branch}"):
if not commit_and_push_pr(commit_msg, f"KDAB/{proj_name}", repo_path, remote, branch, tmp_branch):
return False

if not run_command(f"gh pr create -R KDAB/{proj_name} --base {branch} -H {tmp_branch} --title {commit_msg} --body \"Automatically created via GH action.\""):
return False

return False
return True


def update_submodule(proj_name, submodule_name, sha1, repo_path, remote, branch):
Expand All @@ -498,9 +489,6 @@ def update_submodule(proj_name, submodule_name, sha1, repo_path, remote, branch)

sha1 = versions['latest_version']

if not run_command(f"git -C {repo_path} checkout {branch}"):
return False

tmp_branch = checkout_randomly_named_branch(repo_path, "gh-actions")
if not tmp_branch:
return False
Expand All @@ -512,7 +500,13 @@ def update_submodule(proj_name, submodule_name, sha1, repo_path, remote, branch)
return False

commit_msg = f"\"Bump {submodule_name} from {versions['current_version']} to {sha1}\""
if not commit_and_push_pr(commit_msg, f"KDAB/{proj_name}", repo_path, remote, branch, tmp_branch):
return False

return True


def commit_and_push_pr(commit_msg, gh_repo, repo_path, remote, branch, tmp_branch):
if not run_command(f"git -C {repo_path} commit --author \"KDAB GitHub Actions <gh@kdab>\" -m {commit_msg}"):
return False

Expand All @@ -522,7 +516,7 @@ def update_submodule(proj_name, submodule_name, sha1, repo_path, remote, branch)
if not run_command(f"git -C {repo_path} push --set-upstream {remote} {tmp_branch}"):
return False

if not run_command(f"gh pr create -R KDAB/{proj_name} --base {branch} -H {tmp_branch} --title {commit_msg} --body \"Automatically created via GH action.\""):
if not run_command(f"gh pr create -R {gh_repo} --base {branch} -H {tmp_branch} --title {commit_msg} --body \"Automatically created via GH action.\""):
return False

return True
Expand Down

0 comments on commit e5143c6

Please sign in to comment.