From e081a8d8d4d4b6096f2b64fe450fa0d50de159b2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonas=20H=C3=B6ppner?= <jonas.hoeppner@garz-fricke.com>
Date: Fri, 18 Mar 2022 13:37:34 +0100
Subject: [PATCH] CI: Create integration branch optionally also when no
 submodule change is detected

---
 check_if_integration_branch_is_up_to_date.py |  7 +++
 common.py                                    | 37 +++++++++++++--
 integrate_into_manifest.py                   | 49 ++++++--------------
 update_submodule.py                          |  4 +-
 4 files changed, 57 insertions(+), 40 deletions(-)

diff --git a/check_if_integration_branch_is_up_to_date.py b/check_if_integration_branch_is_up_to_date.py
index 1d5cddff..9a0b0527 100755
--- a/check_if_integration_branch_is_up_to_date.py
+++ b/check_if_integration_branch_is_up_to_date.py
@@ -36,6 +36,13 @@ def check_if_integration_branch_is_up_to_date(
         except GitCommandError as e:
             sys.exit("ERROR: could not clone manifest repository\n" + str(e))
 
+        branch = common.find_gitlab_ci_integration_branch( 
+                manifest_repo, 
+                merge_request.source_branch
+        )
+        if branch is not None:
+            integration_branch = branch
+
         # Get branches
         try:
             integration_branch = manifest_repo.heads[integration_branch]
diff --git a/common.py b/common.py
index 89960dd9..8642798f 100755
--- a/common.py
+++ b/common.py
@@ -21,6 +21,33 @@ def integration_branch_name(project_name, branch_name):
     return "integrate/" + project_name.lower() + "/" + branch_name
 
 
+def find_gitlab_ci_integration_branch(repo: Repo, branch_name):
+    """
+    # Special handling for the gitlab-ci integration
+    # When the branch 'merge_request.source_branch' already starts with
+    # integrate/gitlab-ci we add our new commit to this branch
+    # Otherwise (normal behaviour) a new integration branch is created
+    """
+    if not branch_name.startswith("integrate/gitlab-ci"):
+        return None
+
+    logging.debug("Integration of gitlab-ci: %s", branch_name)
+    for ref in repo.references:
+        refname = ref.name
+        logging.debug("Found ref: %s", refname)
+        if not refname.startswith("origin/"):
+            continue
+        # remove 'origin/' from the ref before compare
+        refname = ref.name.split("/", 1)[1]
+        logging.debug("Splitted refname: %s", refname)
+        if branch_name == refname:
+            integration_branch = refname
+            logging.debug(
+                "Found integration branch for gitlab-ci: %s", integration_branch
+            )
+            return integration_branch
+
+
 def get_project(gitlab, project_name):
     """Get a GitLab project by its name (including or excluding namespace)"""
     project = None
@@ -126,7 +153,7 @@ def commit_and_push(project: Project, repo: Repo, branch, message, name, email):
     # Push commit
     try:
         origin = repo.remote("origin")
-        logging.debug("Push branch %s to %s",branch, origin)
+        logging.debug("Push branch %s to %s", branch, origin)
         origin.push(branch, force=True)
     except GitCommandError as e:
         sys.exit("ERROR: could not commit changes\n" + str(e))
@@ -135,8 +162,7 @@ def commit_and_push(project: Project, repo: Repo, branch, message, name, email):
     revision = repo.head.commit.hexsha
     print("Pushed new commit:")
     print(project.web_url + "/-/commit/" + revision)
-    print(repo.git.log("--oneline", "-n", "5"))
-    #print(repo.git.show("--summary", "--decorate"))
+    print(repo.git.show("--summary", "--decorate"))
 
     return revision
 
@@ -163,8 +189,9 @@ def extract_message_body(msg):
 
     return msg
 
-def get_merge_request( project: Project, merge_request ):
-    """ Return a gitlab mergereqest specified either by id or by link """
+
+def get_merge_request(project: Project, merge_request):
+    """Return a gitlab mergereqest specified either by id or by link"""
 
     # MR may also be specified as
     # SECO-Northern-Europe/yocto/infrastructure/ci-test/minimal-bar!115
diff --git a/integrate_into_manifest.py b/integrate_into_manifest.py
index 2660d578..b4c19315 100755
--- a/integrate_into_manifest.py
+++ b/integrate_into_manifest.py
@@ -9,7 +9,7 @@ import re
 from pathlib import Path
 from furl import furl
 from git import GitCommandError, Repo
-from gitlab import Gitlab, GitlabGetError
+from gitlab import Gitlab
 from lxml import etree
 
 
@@ -47,33 +47,18 @@ def integrate_into_manifest(
         # When the branch 'merge_request.source_branch' already starts with
         # integrate/gitlab-ci we add our new commit to this branch
         # Otherwise (normal behaviour) a new integration branch is created
-        integration_branch = None
-        if merge_request.source_branch.startswith("integrate/gitlab-ci"):
-            logging.debug("Integration of gitlab-ci: %s", merge_request.source_branch)
-            for ref in manifest_repo.references:
-                refname = ref.name
-                logging.debug("Found ref: %s", refname)
-                if not refname.startswith("origin/"):
-                    continue
-                # remove 'origin/' from the ref before compare
-                refname = ref.name.split("/", 1)[1]
-                logging.debug("Splitted refname: %s", refname)
-                if merge_request.source_branch == refname:
-                    logging.debug("Found integration branch for gitlab-ci")
-
-                    integration_branch = refname
-                    logging.debug("Integration branch: %s", integration_branch)
-
-                    manifest_repo.git.checkout('-b', integration_branch, ref.name)
-                    #origin = manifest_repo.remote("origin")
-                    #fetch = origin.fetch(integration_branch)
-                    #logging.debug("Fetch:%s", fetch)
-                    logging.debug("Heads: %s", manifest_repo.heads)
-                    manifest_repo.heads[integration_branch].checkout()
-                    print(manifest_repo.git.log("--oneline", "-n", "5"))
-                    break
+        integration_branch = common.find_gitlab_ci_integration_branch(
+            manifest_repo, merge_request.source_branch
+        )
 
-        if integration_branch is None:
+        if integration_branch is not None:
+            manifest_repo.git.checkout(
+                "-b", integration_branch, "origin/{}".format(integration_branch)
+            )
+            logging.debug("Heads: %s", manifest_repo.heads)
+            manifest_repo.heads[integration_branch].checkout()
+            print(manifest_repo.git.log("--oneline", "-n", "5"))
+        else:
             # Create integration branch (delete former one if already exists)
             integration_branch = common.integration_branch_name(
                 project.name, merge_request.source_branch
@@ -87,7 +72,6 @@ def integrate_into_manifest(
                 manifest_repo.create_head(integration_branch)
             )
 
-
         # Parse manifest file
         try:
             manifest = etree.parse(manifest_filepath.as_posix())
@@ -254,15 +238,12 @@ def main():
 
     manifest_project = common.get_project(gitlab, args.manifest_project)
     project = common.get_project(gitlab, args.project)
-    logging.debug("Project: %s", project.name )
+    logging.debug("Project: %s", project.name)
     logging.debug("Merge Request: %s", args.merge_request)
 
-    merge_request = common.get_merge_request( project, args.merge_request)
+    merge_request = common.get_merge_request(project, args.merge_request)
     if merge_request is None:
-        sys.exit(
-            "ERROR: could not get %s  %s"
-            % (project.name, args.merge_request)
-        )
+        sys.exit("ERROR: could not get %s  %s" % (project.name, args.merge_request))
 
     manifest_revision = integrate_into_manifest(
         manifest_project=manifest_project,
diff --git a/update_submodule.py b/update_submodule.py
index b0d5930e..3caa7dfc 100755
--- a/update_submodule.py
+++ b/update_submodule.py
@@ -63,7 +63,9 @@ def update_submodule(
         # Check if revisions are different
         if submodule.hexsha == submodule_revision:
             print("Submodule is already at %s" % submodule_revision)
-            return (None, None, None)
+            if not replace_existing_branch:
+                # TODO test this
+                return (None, None, None)
 
         # Check for relative path
         if not submodule.url.startswith(".."):
-- 
GitLab