From 02d634c3684ccd402615d5ca462974c767d230a1 Mon Sep 17 00:00:00 2001 From: Tim Jaacks <tim.jaacks@garz-fricke.com> Date: Mon, 29 Aug 2022 11:08:14 +0200 Subject: [PATCH] Improve naming of functions and variables --- scripts/deploy_gitlab_ci.py | 115 +++++++++--------- ...projects.py => get_integration_sources.py} | 20 +-- scripts/retrigger_integrating_projects.py | 10 +- 3 files changed, 74 insertions(+), 71 deletions(-) rename scripts/{get_integrating_projects.py => get_integration_sources.py} (82%) diff --git a/scripts/deploy_gitlab_ci.py b/scripts/deploy_gitlab_ci.py index 5f1f0d42..a5555e83 100755 --- a/scripts/deploy_gitlab_ci.py +++ b/scripts/deploy_gitlab_ci.py @@ -9,7 +9,7 @@ from gitlab import Gitlab from accept_merge_request import accept_merge_request from create_merge_request import create_merge_request -from get_integrating_projects import get_integrating_projects +from get_integration_sources import get_integration_sources from get_merge_requests import get_merge_requests from update_submodule import ( update_submodule_and_include_ref, @@ -187,39 +187,37 @@ def main(): # Create integration branches and commits with updates # submodule in all projects # ======================================================= - projects = [] + integration_sources = [] for manifest_branch in manifest_branches: print( "Searching for projects in %s that are configured for automatic integration into %s:%s" % (args.group, args.manifest_project, manifest_branch) ) - for p in get_integrating_projects( - args.manifest_project, manifest_branch, group - ): - if p not in projects: - projects.append(p) + for s in get_integration_sources(args.manifest_project, manifest_branch, group): + if s not in integration_sources: + integration_sources.append(s) - # Update submodule in all integrating projects + # Update submodule in all integration sources project_integrations = [] - for p in projects: - print("Create integration commit in %s:%s" % (p["project"], p["branch"])) + for s in integration_sources: + print("Create integration commit in %s:%s" % (s["project"], s["branch"])) integration = integrate_submodule_into( - gitlab, p["project"], args.submodule, args.revision, p["branch"] + gitlab, s["project"], args.submodule, args.revision, s["branch"] ) # Store in the list if commit is set (meaning there was an update or # an exising integration branch) if integration["commit"] is not None: project_integrations.append(integration) - manifest_projects = [] + # Update submodule in all manifest branches + manifest_integrations = [] for manifest_branch in manifest_branches: print( "Create integration commit in %s:%s" % (args.manifest_project, manifest_branch), ) - # Update submodule in manifest project - manifest_projects.append( + manifest_integrations.append( integrate_submodule_into( gitlab, args.manifest_project, @@ -236,11 +234,11 @@ def main(): # ======================================================= if args.merge: # Get source merge request (the one in the gitlab-ci repo) - branch = manifest_projects[0]["master_branch"] + branch = manifest_integrations[0]["master_branch"] if branch is None: - branch = manifest_projects[0]["project"].default_branch + branch = manifest_integrations[0]["project"].default_branch submodule_project_path, _ = get_submodule_project_path_and_revision( - manifest_projects[0]["project"], args.submodule, branch + manifest_integrations[0]["project"], args.submodule, branch ) submodule_project = common.get_project(gitlab, submodule_project_path) mrs = get_merge_requests( @@ -256,27 +254,28 @@ def main(): ) source_mr = mrs[0] - for integration in project_integrations: - logging.debug("Create MR in %s", integration["project"].name) + for project_integration in project_integrations: + logging.debug("Create MR in %s", project_integration["project"].name) mr = create_integration_merge_request( - integration["project"], integration["integration_branch"], source_mr + project_integration["project"], + project_integration["integration_branch"], + source_mr, ) - integration["mr"] = mr # Now merge - logging.debug("Merge %s!%s", p, mr.iid) + logging.debug("Merge %s!%s", project_integration["project"], mr.iid) # Wait until GitLab has checked merge status - common.wait_until_merge_status_is_set(integration["project"], mr) + common.wait_until_merge_status_is_set(project_integration["project"], mr) # Attempt to merge merged, integration_commit = accept_merge_request( - integration["project"], mr, rebase=True + project_integration["project"], mr, rebase=True ) # if this has rebased the integration commit needs to be adapted: - integration["commit"] = integration_commit + project_integration["commit"] = integration_commit # Save the target branch here, as the source branch gets deleted # during merge - integration["integration_branch"] = mr.target_branch + project_integration["integration_branch"] = mr.target_branch if not merged: sys.exit( @@ -287,35 +286,35 @@ def main(): "In either case restart this job afterwards in order to get it merged." ) - print("Successfully merged") - # ======================================================= # Now create the integration commit in the manifest # for all subprojects at once # ======================================================= - for manifest_project in manifest_projects: + for manifest_integration in manifest_integrations: manifest_file_abs = os.path.join( - manifest_project["repo"].working_tree_dir, args.manifest_file + manifest_integration["repo"].working_tree_dir, args.manifest_file ) logging.debug("Read manifest from: %s", manifest_file_abs) with open(manifest_file_abs, "r", encoding="utf8") as fp: manifest = fp.read() logging.debug(manifest) srcrev_file_abs = os.path.join( - manifest_project["repo"].working_tree_dir, args.srcrev_file + manifest_integration["repo"].working_tree_dir, args.srcrev_file ) logging.debug("Read manifest from: %s", srcrev_file_abs) with open(srcrev_file_abs, "r", encoding="utf8") as fp: srcrev = fp.read() logging.debug(srcrev) - for integration in project_integrations: + for project_integration in project_integrations: logging.debug( - "Update %s to %s", integration["project"].name, integration["commit"] + "Update %s to %s", + project_integration["project"].name, + project_integration["commit"], ) new_manifest = update_manifest( - manifest, integration["project"], integration["commit"] + manifest, project_integration["project"], project_integration["commit"] ) if new_manifest is not None: manifest = new_manifest @@ -327,50 +326,55 @@ def main(): # the repo if the branch is already up to date gitlab_ci_yml = common.get_repository_file_raw( - integration["project"], + project_integration["project"], ".gitlab-ci.yml", - ref=integration["integration_branch"], + ref=project_integration["integration_branch"], ) project_keys = read_keys_from_gitlab_ci_yml(gitlab_ci_yml) new_srcrev = update_srcrev( - srcrev, project_keys["recipe"], integration["commit"] + srcrev, project_keys["recipe"], project_integration["commit"] ) if new_srcrev is not None: srcrev = new_srcrev logging.debug(srcrev) else: - logging.debug("Project %s not found in xml or srcrev file", p) + logging.debug( + "Project %s not found in xml or srcrev file", + project_integration["project"], + ) # Write manifest with open(manifest_file_abs, "w", encoding="utf8") as fp: fp.write(manifest) - manifest_project["repo"].git.add(args.manifest_file) + manifest_integration["repo"].git.add(args.manifest_file) logging.debug(manifest) with open(srcrev_file_abs, "w", encoding="utf8") as fp: fp.write(srcrev) - manifest_project["repo"].git.add(args.srcrev_file) + manifest_integration["repo"].git.add(args.srcrev_file) logging.debug(srcrev) # ======================================================== # Squash all commits on the integration branch to one # ======================================================== - manifest_project["repo"].remotes.origin.fetch(manifest_project["master_branch"]) - manifest_master = manifest_project["project"].branches.get( - manifest_project["master_branch"] + manifest_integration["repo"].remotes.origin.fetch( + manifest_integration["master_branch"] + ) + manifest_master = manifest_integration["project"].branches.get( + manifest_integration["master_branch"] ) - manifest_project["repo"].git.reset("--soft", manifest_master.commit["id"]) + manifest_integration["repo"].git.reset("--soft", manifest_master.commit["id"]) # ======================================================== # Now commit and push the changes to the manifest repo # ======================================================== # Make an API request to create the gitlab.user object - gitlab = manifest_project["project"].manager.gitlab + gitlab = integration["project"].manager.gitlab gitlab.auth() integration_commit = common.commit_and_push( - manifest_project["project"], - manifest_project["repo"], - manifest_project["message"], + manifest_integration["project"], + manifest_integration["repo"], + manifest_integration["message"], gitlab.user.username, gitlab.user.email, ) @@ -381,25 +385,24 @@ def main(): # ============================================ # Create merge requests for the manifest # ============================================ - for manifest_project in manifest_projects: - logging.debug("Create MR in %s", manifest_project["project"].name) - manifest_project["mr"] = create_integration_merge_request( - manifest_project["project"], - manifest_project["integration_branch"], + for integration in manifest_integrations: + logging.debug("Create MR in %s", integration["project"].name) + mr = create_integration_merge_request( + integration["project"], + integration["integration_branch"], source_mr, ) # ================================================= # Now merge it # ================================================= # The manifest needs to be merged at last - mr = manifest_project["mr"] logging.debug("Merge %s!%s", args.manifest_project, mr.iid) # Wait until GitLab has checked merge status - common.wait_until_merge_status_is_set(manifest_project["project"], mr) + common.wait_until_merge_status_is_set(integration["project"], mr) # Attempt to merge - merged = accept_merge_request(manifest_project["project"], mr, rebase=True) + merged = accept_merge_request(integration["project"], mr, rebase=True) if not merged: sys.exit( diff --git a/scripts/get_integrating_projects.py b/scripts/get_integration_sources.py similarity index 82% rename from scripts/get_integrating_projects.py rename to scripts/get_integration_sources.py index 45513c3f..4e069aea 100755 --- a/scripts/get_integrating_projects.py +++ b/scripts/get_integration_sources.py @@ -6,18 +6,18 @@ from gitlab import Gitlab, GitlabGetError from gitlab.v4.objects import Group -def get_integrating_projects(manifest_project: str, manifest_branch: str, group: Group): +def get_integration_sources(manifest_project: str, manifest_branch: str, group: Group): """ - Get a list of projects in the given group which are automatically integrated into - the given branch of the given manifest project. + Get a list of projects and branches in the given group which are configured for + automatic integration into the given branch of the given manifest project. """ - projects = [] + integration_sources = [] gitlab = group.manager.gitlab # Recurse into subgroups for g in group.subgroups.list(): subgroup = gitlab.groups.get(g.id) - projects += get_integrating_projects( + integration_sources += get_integration_sources( manifest_project, manifest_branch, subgroup ) @@ -32,7 +32,7 @@ def get_integrating_projects(manifest_project: str, manifest_branch: str, group: for integration in integrations.splitlines(): if re.search(regex, integration): source_branch = integration.split(":")[0] - projects.append( + integration_sources.append( { "project": project.path_with_namespace, "branch": source_branch, @@ -52,7 +52,7 @@ def get_integrating_projects(manifest_project: str, manifest_branch: str, group: else: raise - return projects + return integration_sources def main(): @@ -93,14 +93,14 @@ def main(): gitlab = Gitlab(args.gitlab_url, private_token=args.token) group = gitlab.groups.get(args.group) - projects = get_integrating_projects( + integration_sources = get_integration_sources( args.manifest_project, args.manifest_branch, group, ) - for project in projects: - print(project) + for source in integration_sources: + print("%s:%s" % (source["project"], source["branch"])) if __name__ == "__main__": diff --git a/scripts/retrigger_integrating_projects.py b/scripts/retrigger_integrating_projects.py index 512cb67d..45f64efa 100755 --- a/scripts/retrigger_integrating_projects.py +++ b/scripts/retrigger_integrating_projects.py @@ -3,7 +3,7 @@ import argparse import sys from gitlab import Gitlab, GitlabJobRetryError -from get_integrating_projects import get_integrating_projects +from get_integration_sources import get_integration_sources from get_merge_requests import get_merge_requests from retrigger_pipeline_jobs import retrigger_pipeline_jobs @@ -58,20 +58,20 @@ def main(): gitlab = Gitlab(args.gitlab_url, private_token=args.token) group = gitlab.groups.get(args.group, retry_transient_errors=True) - projects = get_integrating_projects( + integration_sources = get_integration_sources( args.manifest_project, args.manifest_branch, group ) failed = 0 - for p in projects: + for source in integration_sources: - project = gitlab.projects.get(p["project"], retry_transient_errors=True) + project = gitlab.projects.get(source["project"], retry_transient_errors=True) mrs = get_merge_requests( project, state="opened", - target_branch=p["branch"], + target_branch=source["branch"], ) for mr in mrs: -- GitLab