Skip to content

Commit

Permalink
Add update_submodule()
Browse files Browse the repository at this point in the history
  • Loading branch information
iamsergio committed Jan 5, 2025
1 parent e3a3cb4 commit 50566aa
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[MESSAGES CONTROL]
disable=C0114,C0116,C0301,R0911,W0719,R0913,R0917,C0103,W0718,W0702,R1714
disable=C0114,C0116,C0301,R0911,W0719,R0913,R0917,C0103,W0718,W0702,R1714,R0801,R0912
79 changes: 78 additions & 1 deletion src/gh_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import json
import argparse
import uuid
from utils import get_projects, repo_exists, run_command, run_command_with_output, run_command_silent, tag_for_version, get_project, get_submodule_builtin_dependencies
from version_utils import is_numeric, previous_version, get_current_version_in_cmake
from changelog_utils import get_changelog
Expand Down Expand Up @@ -267,6 +268,13 @@ def get_submodule_dependency_version(repo_path):
return run_command_with_output(f"git -C {repo_path} describe --abbrev=0 --tags HEAD").strip()


def checkout_randomly_named_branch(repo_path, prefix):
branch = f"{prefix}/{str(uuid.uuid4())}"
if run_command(f"git -C {repo_path} checkout -B {branch}"):
return branch
return None


def get_submodule_versions(master_repo_path, proj_name, submodule_name=None):
'''
returns the list of submodule current and latest versions for give project
Expand All @@ -287,7 +295,7 @@ def get_submodule_versions(master_repo_path, proj_name, submodule_name=None):

result = []
for key, dep in deps.items():
if key == submodule_name:
if submodule_name and key != submodule_name:
continue

repo_path = master_repo_path + '/' + dep['submodule_path']
Expand Down Expand Up @@ -329,6 +337,75 @@ def print_submodule_versions(repo_paths):
f" {submodule_path}: {current_version} -> ????")


def update_submodule(proj_name, submodule_name, sha1, repo_path, remote, branch):
proj = get_project(proj_name)

if 'dependencies' not in proj:
print(
f"Project {proj} does not have any dependencies, check releasing.toml")
return False

deps = proj['dependencies']

if submodule_name not in deps:
print(
f"No submodule {submodule_name} in dependencies. dependencies={deps}")
return False

if not branch:
branch = proj.get('main_branch', 'main')

submodule = deps[submodule_name]
if 'submodule_path' not in submodule:
print(
f"Expected 'submodule_path' key with the submodule path. Got={submodule}")
return False

submodule_path = repo_path + '/' + submodule['submodule_path']
versions = get_submodule_versions(repo_path, proj_name, submodule_name)
if len(versions) != 1:
print("Could not get submodule versions")
return False

versions = versions[0]

if not sha1:
if versions['current_version'] == versions['latest_version'] or versions['current_version'] == 'latest':
# already latest
return True

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, "bump")
if not tmp_branch:
return False

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

if not run_command(f"git -C {repo_path} add {submodule['submodule_path']}"):
return False

commit_msg = f"\"Bump {submodule_name} from {versions['current_version']} to {sha1}\""

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}"):
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 True


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--get-latest-release', metavar='REPO',
Expand Down
15 changes: 15 additions & 0 deletions src/update_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,22 @@ def print_dependencies(proj_name, repo_path):
parser.add_argument('--repo-path', metavar='<path>',
help="Path to repository", required=True)

parser.add_argument('--update-dependency', metavar='<dependency name>',
help="Dependency name", required=False)

parser.add_argument('--remote', metavar='<remote>',
help="Remote, defaults to origin", required=False, default='origin')

parser.add_argument('--branch', metavar='<branch>',
help="Remote branch, defaults to the main branch", required=False)

parser.add_argument('--sha1', metavar='<sha1, tag or branch>',
help="Sha tag or branch, defaults to latest", required=False, default='latest')

args = parser.parse_args()

if args.print_dependency_versions:
print_dependencies(args.proj_name, args.repo_path)
elif args.update_dependency:
gh_utils.update_submodule(args.proj_name, args.update_dependency,
args.sha1, args.repo_path, args.remote, args.branch)

0 comments on commit 50566aa

Please sign in to comment.