From 2aeb899ae3a3ad32ccac3b15f7025d42de5d5dac 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 09:57:49 +0200
Subject: [PATCH] CI: merge_gitlab_ci: Fix some post merge issues

---
 accept_merge_request.py                      |  4 ++--
 check_if_integration_branch_is_up_to_date.py | 10 +++++++++-
 common.py                                    |  2 +-
 merge_gitlab_ci.py                           | 20 +++++++++++++++++++-
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/accept_merge_request.py b/accept_merge_request.py
index a193e7a1..779d6117 100755
--- a/accept_merge_request.py
+++ b/accept_merge_request.py
@@ -19,7 +19,7 @@ critical_error = (
 )
 
 
-def accept_merge_request(project, mr, rebase=False):
+def accept_merge_request(project, mr, rebase=False, should_remove_source_branch=True):
     """Attempt to merge a merge request, rebase if necessary"""
     merged = False
     pipeline_pending = False
@@ -37,7 +37,7 @@ def accept_merge_request(project, mr, rebase=False):
 
         # Try to merge the merge request
         try:
-            mr.merge(should_remove_source_branch=True)
+            mr.merge(should_remove_source_branch=should_remove_source_branch)
             if pipeline_pending:
                 print("")
             if mr.state == "merged":
diff --git a/check_if_integration_branch_is_up_to_date.py b/check_if_integration_branch_is_up_to_date.py
index a465ae5d..9e6de6ee 100755
--- a/check_if_integration_branch_is_up_to_date.py
+++ b/check_if_integration_branch_is_up_to_date.py
@@ -15,12 +15,14 @@ def check_if_integration_branch_is_up_to_date(
 ):
 
     integration_branch = None
+    branch_list = []
     if merge_request.source_branch.startswith("integrate/gitlab-ci"):
         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
@@ -29,13 +31,19 @@ def check_if_integration_branch_is_up_to_date(
         integration_branch_name = common.integration_branch_name(
             project.name, merge_request.source_branch
         )
+        branch_list.append(integration_branch)
         try:
             integration_branch = manifest_project.branches.get(
                 integration_branch_name,
                 retry_transient_errors=True,
             )
         except GitlabGetError:
-            sys.exit("ERROR: could not find integration branch\n")
+            sys.exit(
+                "ERROR: could not find integration branch in %s,"
+                "branch names checked: %s\n",
+                manifest_project.name,
+                branch_list,
+            )
 
     try:
         integration_base_branch = manifest_project.branches.get(
diff --git a/common.py b/common.py
index b147aae8..ab6093c4 100755
--- a/common.py
+++ b/common.py
@@ -280,7 +280,7 @@ def is_commit_parent_of_project_commit(project: Project, project_commit, commit)
             ) from e
 
         # The integration branch is up to date if its parent is the integration base
-        logging.debug("Compare '%s' and '%s'",parent.id, commit)
+        logging.debug("Compare '%s' and '%s'", parent.id, commit)
         if parent.id == commit:
             return True
         if len(parent.parent_ids) == 0:
diff --git a/merge_gitlab_ci.py b/merge_gitlab_ci.py
index eeaa3a7c..26da06a0 100755
--- a/merge_gitlab_ci.py
+++ b/merge_gitlab_ci.py
@@ -156,6 +156,11 @@ def main():
             # deploy_gitlab_ci again to create a new integration
             # commit or make sure the change is already integrated.
 
+        # Store for later usage:
+        if p == args.project:
+            manifest_project = project
+            manifest_integration_mr = mr
+
         print("Merge {}!{}: {}".format(project.name, mr.iid, mr.title))
         # Wait until GitLab has checked merge status
         common.wait_until_merge_status_is_set(project, mr)
@@ -167,6 +172,10 @@ def main():
             # Only the file manifest commit may be rebased
             # other rebase would require new integration commits
             rebase=(p == args.project),
+            # For the manifest we keep the source branch
+            # otherwise does the check job fail for not yet merged
+            # child projects
+            should_remove_source_branch=(p == args.project),
         )
 
         if not merged:
@@ -185,9 +194,18 @@ def main():
                     continue
                 if not found:
                     continue
-                print("     {}".format(p2.name))
+                print("     {}".format(p2))
             sys.exit()
 
+    # Delete args.project (manifests) integration branch
+    # (skipped the deletion during the merge request merge before)
+    integration_branch = manifest_project.branches.get(
+        manifest_integration_mr.source_branch
+    )
+    manifest_project.branches.delete(
+        integration_branch.get_id(), retry_transient_errors=True
+    )
+
     print("Successfully merged")
 
     exit()
-- 
GitLab