From 892c12fe363d66e63757ec4214d27c87dd82fde9 Mon Sep 17 00:00:00 2001
From: Tim Jaacks <tim.jaacks@garz-fricke.com>
Date: Tue, 9 Aug 2022 18:18:06 +0200
Subject: [PATCH] Create separate integration branches for each target branch

This makes it possible to integrate a project branch into different
branches of the same manifest project.
---
 manifest-integration-jobs.yml.jinja2                |  2 +-
 .../check_if_integration_branch_is_up_to_date.py    |  2 +-
 scripts/common.py                                   | 13 ++++++++++---
 scripts/integrate_into_manifest.py                  |  2 +-
 scripts/merge_into_manifest.py                      |  5 ++++-
 scripts/update_submodule.py                         |  2 +-
 6 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/manifest-integration-jobs.yml.jinja2 b/manifest-integration-jobs.yml.jinja2
index 19cdc67d..74d39724 100644
--- a/manifest-integration-jobs.yml.jinja2
+++ b/manifest-integration-jobs.yml.jinja2
@@ -102,7 +102,7 @@ build:
     # FIXME: see FIXME comments in manifest-integration-pipelines.yml.jinja2
     # project: $MANIFEST_PROJECT
     project: {{ TARGET_PROJECT }}
-    branch: "integrate/${CI_PROJECT_NAME}/${CI_COMMIT_REF_NAME}"
+    branch: "integrate/${CI_PROJECT_NAME}/${CI_COMMIT_REF_NAME}/into/${MASTER_BRANCH_MANIFEST}"
     strategy: depend
 
 # --------------------------------------------------------------------------------------
diff --git a/scripts/check_if_integration_branch_is_up_to_date.py b/scripts/check_if_integration_branch_is_up_to_date.py
index 42c26051..a9f45ab5 100755
--- a/scripts/check_if_integration_branch_is_up_to_date.py
+++ b/scripts/check_if_integration_branch_is_up_to_date.py
@@ -28,7 +28,7 @@ def check_if_integration_branch_is_up_to_date(
             pass
     if integration_branch is None:
         integration_branch_name = common.integration_branch_name(
-            project.name, merge_request.source_branch
+            project.name, merge_request.source_branch, integration_base
         )
         branch_list.append(integration_branch)
         try:
diff --git a/scripts/common.py b/scripts/common.py
index 6e45da0e..8154b2bc 100755
--- a/scripts/common.py
+++ b/scripts/common.py
@@ -17,9 +17,16 @@ srcrev_file = "SRCREV.conf"
 pending_states = ["created", "waiting_for_resource", "preparing", "pending", "running"]
 
 
-def integration_branch_name(project_name, branch_name):
+def integration_branch_name(project_name, source_branch_name, target_branch_name):
     """Get integration branch name"""
-    return "integrate/" + project_name.lower() + "/" + branch_name
+    return (
+        "integrate/"
+        + project_name.lower()
+        + "/"
+        + source_branch_name
+        + "/into/"
+        + target_branch_name
+    )
 
 
 def is_gitlab_ci_integration_branch(branch_name):
@@ -231,7 +238,7 @@ def clone_project(project: Project, into, branch=None):
         repo = Repo.clone_from(clone_url.url, into, branch=branch, depth=1)
     except GitCommandError as e:
         raise Exception("could not clone repository\n" + str(e)) from e
-    except IndexError:
+    except IndexError as e:
         raise Exception("branch '%s' not found" % branch) from e
     return repo
 
diff --git a/scripts/integrate_into_manifest.py b/scripts/integrate_into_manifest.py
index 223c5ffa..8c0fd7d2 100755
--- a/scripts/integrate_into_manifest.py
+++ b/scripts/integrate_into_manifest.py
@@ -119,7 +119,7 @@ def integrate_into_manifest(
         else:
             # Create integration branch (delete former one if already exists)
             integration_branch = common.integration_branch_name(
-                project.name, merge_request.source_branch
+                project.name, merge_request.source_branch, integration_base
             )
             for ref in manifest_repo.references:
                 if integration_branch == ref.name:
diff --git a/scripts/merge_into_manifest.py b/scripts/merge_into_manifest.py
index f29cd76e..f57e66c9 100755
--- a/scripts/merge_into_manifest.py
+++ b/scripts/merge_into_manifest.py
@@ -52,9 +52,12 @@ def merge_into_manifest(
         )
         return ""
 
-    integration_branch = common.integration_branch_name(project.name, original_branch)
     target_branch = master_branch
 
+    integration_branch = common.integration_branch_name(
+        project.name, original_branch, target_branch
+    )
+
     # Create merge request. If there already is a merge request, we assume that it has
     # been opened manually and thus will be merged manually as well, so we fail here.
     mr, created = create_merge_request(
diff --git a/scripts/update_submodule.py b/scripts/update_submodule.py
index d22b47df..32bfe97d 100755
--- a/scripts/update_submodule.py
+++ b/scripts/update_submodule.py
@@ -248,7 +248,7 @@ def update_submodule_and_include_ref(
 
     # Construct integration branch name
     integration_branch_name = common.integration_branch_name(
-        submodule_project.name, integration_branch_suffix
+        submodule_project.name, integration_branch_suffix, branch
     )
 
     # Construct commit message
-- 
GitLab