Skip to content
Snippets Groups Projects
Commit 02d634c3 authored by Tim Jaacks's avatar Tim Jaacks
Browse files

Improve naming of functions and variables

parent 051871d9
No related branches found
No related tags found
No related merge requests found
Pipeline #27999 passed with stages
in 30 seconds
...@@ -9,7 +9,7 @@ from gitlab import Gitlab ...@@ -9,7 +9,7 @@ from gitlab import Gitlab
from accept_merge_request import accept_merge_request from accept_merge_request import accept_merge_request
from create_merge_request import create_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 get_merge_requests import get_merge_requests
from update_submodule import ( from update_submodule import (
update_submodule_and_include_ref, update_submodule_and_include_ref,
...@@ -187,39 +187,37 @@ def main(): ...@@ -187,39 +187,37 @@ def main():
# Create integration branches and commits with updates # Create integration branches and commits with updates
# submodule in all projects # submodule in all projects
# ======================================================= # =======================================================
projects = [] integration_sources = []
for manifest_branch in manifest_branches: for manifest_branch in manifest_branches:
print( print(
"Searching for projects in %s that are configured for automatic integration into %s:%s" "Searching for projects in %s that are configured for automatic integration into %s:%s"
% (args.group, args.manifest_project, manifest_branch) % (args.group, args.manifest_project, manifest_branch)
) )
for p in get_integrating_projects( for s in get_integration_sources(args.manifest_project, manifest_branch, group):
args.manifest_project, manifest_branch, group if s not in integration_sources:
): integration_sources.append(s)
if p not in projects:
projects.append(p)
# Update submodule in all integrating projects # Update submodule in all integration sources
project_integrations = [] project_integrations = []
for p in projects: for s in integration_sources:
print("Create integration commit in %s:%s" % (p["project"], p["branch"])) print("Create integration commit in %s:%s" % (s["project"], s["branch"]))
integration = integrate_submodule_into( 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 # Store in the list if commit is set (meaning there was an update or
# an exising integration branch) # an exising integration branch)
if integration["commit"] is not None: if integration["commit"] is not None:
project_integrations.append(integration) project_integrations.append(integration)
manifest_projects = [] # Update submodule in all manifest branches
manifest_integrations = []
for manifest_branch in manifest_branches: for manifest_branch in manifest_branches:
print( print(
"Create integration commit in %s:%s" "Create integration commit in %s:%s"
% (args.manifest_project, manifest_branch), % (args.manifest_project, manifest_branch),
) )
# Update submodule in manifest project manifest_integrations.append(
manifest_projects.append(
integrate_submodule_into( integrate_submodule_into(
gitlab, gitlab,
args.manifest_project, args.manifest_project,
...@@ -236,11 +234,11 @@ def main(): ...@@ -236,11 +234,11 @@ def main():
# ======================================================= # =======================================================
if args.merge: if args.merge:
# Get source merge request (the one in the gitlab-ci repo) # 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: 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( 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) submodule_project = common.get_project(gitlab, submodule_project_path)
mrs = get_merge_requests( mrs = get_merge_requests(
...@@ -256,27 +254,28 @@ def main(): ...@@ -256,27 +254,28 @@ def main():
) )
source_mr = mrs[0] source_mr = mrs[0]
for integration in project_integrations: for project_integration in project_integrations:
logging.debug("Create MR in %s", integration["project"].name) logging.debug("Create MR in %s", project_integration["project"].name)
mr = create_integration_merge_request( 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 # 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 # 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 # Attempt to merge
merged, integration_commit = accept_merge_request( 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: # 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 # Save the target branch here, as the source branch gets deleted
# during merge # during merge
integration["integration_branch"] = mr.target_branch project_integration["integration_branch"] = mr.target_branch
if not merged: if not merged:
sys.exit( sys.exit(
...@@ -287,35 +286,35 @@ def main(): ...@@ -287,35 +286,35 @@ def main():
"In either case restart this job afterwards in order to get it merged." "In either case restart this job afterwards in order to get it merged."
) )
print("Successfully merged")
# ======================================================= # =======================================================
# Now create the integration commit in the manifest # Now create the integration commit in the manifest
# for all subprojects at once # for all subprojects at once
# ======================================================= # =======================================================
for manifest_project in manifest_projects: for manifest_integration in manifest_integrations:
manifest_file_abs = os.path.join( 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) logging.debug("Read manifest from: %s", manifest_file_abs)
with open(manifest_file_abs, "r", encoding="utf8") as fp: with open(manifest_file_abs, "r", encoding="utf8") as fp:
manifest = fp.read() manifest = fp.read()
logging.debug(manifest) logging.debug(manifest)
srcrev_file_abs = os.path.join( 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) logging.debug("Read manifest from: %s", srcrev_file_abs)
with open(srcrev_file_abs, "r", encoding="utf8") as fp: with open(srcrev_file_abs, "r", encoding="utf8") as fp:
srcrev = fp.read() srcrev = fp.read()
logging.debug(srcrev) logging.debug(srcrev)
for integration in project_integrations: for project_integration in project_integrations:
logging.debug( 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( new_manifest = update_manifest(
manifest, integration["project"], integration["commit"] manifest, project_integration["project"], project_integration["commit"]
) )
if new_manifest is not None: if new_manifest is not None:
manifest = new_manifest manifest = new_manifest
...@@ -327,50 +326,55 @@ def main(): ...@@ -327,50 +326,55 @@ def main():
# the repo if the branch is already up to date # the repo if the branch is already up to date
gitlab_ci_yml = common.get_repository_file_raw( gitlab_ci_yml = common.get_repository_file_raw(
integration["project"], project_integration["project"],
".gitlab-ci.yml", ".gitlab-ci.yml",
ref=integration["integration_branch"], ref=project_integration["integration_branch"],
) )
project_keys = read_keys_from_gitlab_ci_yml(gitlab_ci_yml) project_keys = read_keys_from_gitlab_ci_yml(gitlab_ci_yml)
new_srcrev = update_srcrev( new_srcrev = update_srcrev(
srcrev, project_keys["recipe"], integration["commit"] srcrev, project_keys["recipe"], project_integration["commit"]
) )
if new_srcrev is not None: if new_srcrev is not None:
srcrev = new_srcrev srcrev = new_srcrev
logging.debug(srcrev) logging.debug(srcrev)
else: 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 # Write manifest
with open(manifest_file_abs, "w", encoding="utf8") as fp: with open(manifest_file_abs, "w", encoding="utf8") as fp:
fp.write(manifest) fp.write(manifest)
manifest_project["repo"].git.add(args.manifest_file) manifest_integration["repo"].git.add(args.manifest_file)
logging.debug(manifest) logging.debug(manifest)
with open(srcrev_file_abs, "w", encoding="utf8") as fp: with open(srcrev_file_abs, "w", encoding="utf8") as fp:
fp.write(srcrev) fp.write(srcrev)
manifest_project["repo"].git.add(args.srcrev_file) manifest_integration["repo"].git.add(args.srcrev_file)
logging.debug(srcrev) logging.debug(srcrev)
# ======================================================== # ========================================================
# Squash all commits on the integration branch to one # Squash all commits on the integration branch to one
# ======================================================== # ========================================================
manifest_project["repo"].remotes.origin.fetch(manifest_project["master_branch"]) manifest_integration["repo"].remotes.origin.fetch(
manifest_master = manifest_project["project"].branches.get( manifest_integration["master_branch"]
manifest_project["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 # Now commit and push the changes to the manifest repo
# ======================================================== # ========================================================
# Make an API request to create the gitlab.user object # Make an API request to create the gitlab.user object
gitlab = manifest_project["project"].manager.gitlab gitlab = integration["project"].manager.gitlab
gitlab.auth() gitlab.auth()
integration_commit = common.commit_and_push( integration_commit = common.commit_and_push(
manifest_project["project"], manifest_integration["project"],
manifest_project["repo"], manifest_integration["repo"],
manifest_project["message"], manifest_integration["message"],
gitlab.user.username, gitlab.user.username,
gitlab.user.email, gitlab.user.email,
) )
...@@ -381,25 +385,24 @@ def main(): ...@@ -381,25 +385,24 @@ def main():
# ============================================ # ============================================
# Create merge requests for the manifest # Create merge requests for the manifest
# ============================================ # ============================================
for manifest_project in manifest_projects: for integration in manifest_integrations:
logging.debug("Create MR in %s", manifest_project["project"].name) logging.debug("Create MR in %s", integration["project"].name)
manifest_project["mr"] = create_integration_merge_request( mr = create_integration_merge_request(
manifest_project["project"], integration["project"],
manifest_project["integration_branch"], integration["integration_branch"],
source_mr, source_mr,
) )
# ================================================= # =================================================
# Now merge it # Now merge it
# ================================================= # =================================================
# The manifest needs to be merged at last # The manifest needs to be merged at last
mr = manifest_project["mr"]
logging.debug("Merge %s!%s", args.manifest_project, mr.iid) logging.debug("Merge %s!%s", args.manifest_project, mr.iid)
# Wait until GitLab has checked merge status # 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 # 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: if not merged:
sys.exit( sys.exit(
......
...@@ -6,18 +6,18 @@ from gitlab import Gitlab, GitlabGetError ...@@ -6,18 +6,18 @@ from gitlab import Gitlab, GitlabGetError
from gitlab.v4.objects import Group 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 Get a list of projects and branches in the given group which are configured for
the given branch of the given manifest project. automatic integration into the given branch of the given manifest project.
""" """
projects = [] integration_sources = []
gitlab = group.manager.gitlab gitlab = group.manager.gitlab
# Recurse into subgroups # Recurse into subgroups
for g in group.subgroups.list(): for g in group.subgroups.list():
subgroup = gitlab.groups.get(g.id) subgroup = gitlab.groups.get(g.id)
projects += get_integrating_projects( integration_sources += get_integration_sources(
manifest_project, manifest_branch, subgroup manifest_project, manifest_branch, subgroup
) )
...@@ -32,7 +32,7 @@ def get_integrating_projects(manifest_project: str, manifest_branch: str, group: ...@@ -32,7 +32,7 @@ def get_integrating_projects(manifest_project: str, manifest_branch: str, group:
for integration in integrations.splitlines(): for integration in integrations.splitlines():
if re.search(regex, integration): if re.search(regex, integration):
source_branch = integration.split(":")[0] source_branch = integration.split(":")[0]
projects.append( integration_sources.append(
{ {
"project": project.path_with_namespace, "project": project.path_with_namespace,
"branch": source_branch, "branch": source_branch,
...@@ -52,7 +52,7 @@ def get_integrating_projects(manifest_project: str, manifest_branch: str, group: ...@@ -52,7 +52,7 @@ def get_integrating_projects(manifest_project: str, manifest_branch: str, group:
else: else:
raise raise
return projects return integration_sources
def main(): def main():
...@@ -93,14 +93,14 @@ def main(): ...@@ -93,14 +93,14 @@ def main():
gitlab = Gitlab(args.gitlab_url, private_token=args.token) gitlab = Gitlab(args.gitlab_url, private_token=args.token)
group = gitlab.groups.get(args.group) group = gitlab.groups.get(args.group)
projects = get_integrating_projects( integration_sources = get_integration_sources(
args.manifest_project, args.manifest_project,
args.manifest_branch, args.manifest_branch,
group, group,
) )
for project in projects: for source in integration_sources:
print(project) print("%s:%s" % (source["project"], source["branch"]))
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -3,7 +3,7 @@ import argparse ...@@ -3,7 +3,7 @@ import argparse
import sys import sys
from gitlab import Gitlab, GitlabJobRetryError 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 get_merge_requests import get_merge_requests
from retrigger_pipeline_jobs import retrigger_pipeline_jobs from retrigger_pipeline_jobs import retrigger_pipeline_jobs
...@@ -58,20 +58,20 @@ def main(): ...@@ -58,20 +58,20 @@ def main():
gitlab = Gitlab(args.gitlab_url, private_token=args.token) gitlab = Gitlab(args.gitlab_url, private_token=args.token)
group = gitlab.groups.get(args.group, retry_transient_errors=True) 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 args.manifest_project, args.manifest_branch, group
) )
failed = 0 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( mrs = get_merge_requests(
project, project,
state="opened", state="opened",
target_branch=p["branch"], target_branch=source["branch"],
) )
for mr in mrs: for mr in mrs:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment