From 98c8fdf5daee69b0ed21c357c9bc5502faa6b7d7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonas=20H=C3=B6ppner?= <jonas.hoeppner@garz-fricke.com>
Date: Tue, 29 Mar 2022 17:51:44 +0200
Subject: [PATCH] CI: merge gitlab-ci: manifest is merged at last

The merge job of a subproject include in the merge request merged by
gitlab ci, should not merge the integration branch in the manifest by
themself. This is now disabled by checking the source branch name.

This allow to merge the manifests commit at last.
---
 check_if_integration_branch_is_up_to_date.py |  7 ++---
 common.py                                    |  7 ++++-
 merge_gitlab_ci.py                           |  9 ++----
 merge_into_manifest.py                       | 30 +++++---------------
 4 files changed, 17 insertions(+), 36 deletions(-)

diff --git a/check_if_integration_branch_is_up_to_date.py b/check_if_integration_branch_is_up_to_date.py
index 0058e9d6..460f3312 100755
--- a/check_if_integration_branch_is_up_to_date.py
+++ b/check_if_integration_branch_is_up_to_date.py
@@ -16,14 +16,13 @@ def check_if_integration_branch_is_up_to_date(
 
     integration_branch = None
     branch_list = []
-    if merge_request.source_branch.startswith("integrate/gitlab-ci"):
+    if common.is_gitlab_ci_integration_branch(merge_request.source_branch):
         try:
             integration_branch = manifest_project.branches.get(
                 merge_request.source_branch,
                 retry_transient_errors=True,
             )
             branch_list.append(merge_request.source_branch)
-
         except GitlabGetError:
             # Branch not found
             pass
@@ -40,9 +39,7 @@ def check_if_integration_branch_is_up_to_date(
         except GitlabGetError:
             sys.exit(
                 "ERROR: could not find integration branch in {},"
-                "branch names checked: {}\n".format(
-                manifest_project.name,
-                branch_list)
+                "branch names checked: {}\n".format(manifest_project.name, branch_list)
             )
 
     try:
diff --git a/common.py b/common.py
index ab6093c4..06325119 100755
--- a/common.py
+++ b/common.py
@@ -22,6 +22,11 @@ def integration_branch_name(project_name, branch_name):
     return "integrate/" + project_name.lower() + "/" + branch_name
 
 
+def is_gitlab_ci_integration_branch(branch_name):
+    gitlab_ci_integration_branch = "integrate/.gitlab_ci"
+    return branch_name.startswith(gitlab_ci_integration_branch)
+
+
 def find_gitlab_ci_integration_branch(repo: Repo, branch_name):
     """
     # Special handling for the gitlab-ci integration
@@ -29,7 +34,7 @@ def find_gitlab_ci_integration_branch(repo: Repo, branch_name):
     # 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"):
+    if not is_gitlab_ci_integration_branch(branch_name):
         return None
 
     logging.debug("Integration of gitlab-ci: %s", branch_name)
diff --git a/merge_gitlab_ci.py b/merge_gitlab_ci.py
index 37a6c72e..dfabc841 100755
--- a/merge_gitlab_ci.py
+++ b/merge_gitlab_ci.py
@@ -134,10 +134,9 @@ def main():
 
     # Start with the manifest here, so the subproject
     # can see that they are already integrated
-    for p in [args.project] + args.projects:
+    for p in args.projects + [args.project]:
         project = common.get_project(gitlab, p)
         branch = args.branch
-        should_remove_source_branch = True
 
         if branch is None:
             branch = project.default_branch
@@ -161,10 +160,6 @@ def main():
         if p == args.project:
             manifest_project = project
             manifest_integration_mr = mr
-            # For the manifest we keep the source branch
-            # otherwise does the check job fail for not yet merged
-            # child projects
-            should_remove_source_branch = False
 
         print("Merge {}!{}: {}".format(project.name, mr.iid, mr.title))
         # Wait until GitLab has checked merge status
@@ -176,8 +171,8 @@ def main():
             mr,
             # Only the file manifest commit may be rebased
             # other rebase would require new integration commits
+            # TODO implement rebase
             rebase=(p == args.project),
-            should_remove_source_branch=should_remove_source_branch,
         )
 
         if not merged:
diff --git a/merge_into_manifest.py b/merge_into_manifest.py
index 7c656096..e3c6c4fc 100755
--- a/merge_into_manifest.py
+++ b/merge_into_manifest.py
@@ -10,7 +10,6 @@ from accept_merge_request import accept_merge_request
 from create_merge_request import create_merge_request
 from get_merge_requests import get_merge_requests
 from integrate_into_manifest import integrate_into_manifest
-from get_current_revision_from_manifest import get_current_revision_from_manifest
 
 
 def merge_into_manifest(
@@ -28,28 +27,6 @@ def merge_into_manifest(
     the current manifest master.
     """
 
-    # Check if the commit is already merged
-    # Currently this is especially true if the gitlab-ci
-    # pipeline already has merged an gitlab-ci update
-    current_revisions = get_current_revision_from_manifest(
-        manifest_project=manifest_project,
-        manifest_branch=master_branch,
-        project=project,
-        recipe_name=recipe_name,
-        srcrev_file=srcrev_file,
-    )
-    logging.debug(current_revisions)
-    # This commit is contained there if current_revisions contains
-    # it directly or any parrent of current_revisions is commit
-    for rev in current_revisions.values():
-        if common.is_commit_parent_of_project_commit(project, rev, commit):
-            print(
-                "Commit {} is already merged into {}".format(
-                    commit, manifest_project.name
-                )
-            )
-            return ""
-
     # Get source merge request
     mrs = get_merge_requests(
         project,
@@ -65,6 +42,13 @@ def merge_into_manifest(
 
     # Get original branch for commit
     original_branch = source_mr.source_branch
+
+    if common.is_gitlab_ci_integration_branch(original_branch):
+        print(
+            "Commit {} is a gitlab-ci integration branch and will be merged by the gitlab-ci pipeline."
+        )
+        return ""
+
     integration_branch = common.integration_branch_name(project.name, original_branch)
     target_branch = master_branch
 
-- 
GitLab