From 9cfa350f7729972dd759803a46d91046795d06db Mon Sep 17 00:00:00 2001
From: Lorenzo Pagliai <lorenzo.pagliai@seco.com>
Date: Mon, 29 Apr 2024 13:43:12 +0200
Subject: [PATCH] [CHANGELOG] Improve search for MRs to be collected

* Since now, for each project, the script was searching only for MRs
that were merged into branches having the same name of the manifest main
branch.
* In order to correct this approach, we define a function that searches
  for the INTEGRATION variable in each project and get the name of
the branch whose changes are integrated into the manifest main branch. If
more than one branch integrates into the manifest one, all of them are
considered in the search for MRs to be inserted in the changelog.
* Include BSP repositories and new meta-layers in CHANGELOG_PROJECTS
  variable
---
 manifest-pipeline-yocto.yml    | 15 ++++++++
 scripts/changelog_generator.py | 67 +++++++++++++++++++++++++---------
 2 files changed, 65 insertions(+), 17 deletions(-)

diff --git a/manifest-pipeline-yocto.yml b/manifest-pipeline-yocto.yml
index e09a2c6..c147f41 100644
--- a/manifest-pipeline-yocto.yml
+++ b/manifest-pipeline-yocto.yml
@@ -18,11 +18,26 @@ variables:
 
   # Projects to include in the changelog in addition to the manifest project
   CHANGELOG_PROJECTS:
+    edgehog/bsp/nxp/linux-seco-imx
+    edgehog/bsp/nxp/u-boot-seco-imx
+    edgehog/bsp/nxp/tools/imx-mkimage
+    edgehog/bsp/rockchip/linux-seco-rk
+    edgehog/bsp/rockchip/u-boot-seco-rk
+    edgehog/bsp/rockchip/tools/rk-tools
+    edgehog/bsp/rockchip/tools/rkbin
+    edgehog/tools/menuconfig
+    edgehog/tools/seco-eeprom-manager
+    edgehog/seco-base
+    edgehog/firmware/tools/STM_D23_Tester
     edgehog/layers/seco/meta-seco-imx
     edgehog/layers/seco/meta-seco-rk
     edgehog/layers/seco/meta-seco-intel
     edgehog/layers/seco/meta-seco-edgehog-things
     edgehog/layers/seco/meta-seco-edgehog-embedded
+    edgehog/layers/seco/meta-seco-rpi
+    edgehog/layers/seco/meta-seco-bsp
+    edgehog/layers/seco/meta-seco-core
+    edgehog/layers/seco/meta-seco-edgehog
     edgehog/layers/external/poky
   IMAGES_PATH: "tmp/deploy/images"
   TAG_NAME: weekly
diff --git a/scripts/changelog_generator.py b/scripts/changelog_generator.py
index 89a7745..904bced 100755
--- a/scripts/changelog_generator.py
+++ b/scripts/changelog_generator.py
@@ -38,6 +38,35 @@ def decode_timestamp(t):
     timestamp = datetime.datetime.strptime(t, GITLAB_TIMEFORMAT)
     return timestamp
 
+def get_project_branch_from_integration(gitlab, project_path, manifest_branch):
+
+    # Get project ID from project path
+    project_id = gitlab.projects.get(project_path).id
+
+    # Get CICD variables for the project
+    variables = gitlab.projects.get(project_id).variables.list()
+
+    # Initialize dictionary to store project branches corresponding to manifest branches
+    branch_project_dict = {}
+
+    # Parse INTEGRATION variable and extract 
+    for variable in variables:
+        if variable.key == 'INTEGRATION':
+            integration_value = variable.value.split('\n')
+            for line in integration_value:
+                parts = line.split(':')
+                branch_project, manifest, branch_manifest = parts
+                if branch_manifest == manifest_branch:
+                    if branch_manifest not in branch_project_dict:
+                        branch_project_dict[branch_manifest] = []
+                    branch_project_dict[branch_manifest].append(branch_project)
+    
+    # If INTEGRATION variable is not defined or no matching branch_project found, use manifest_branch as branch_project
+    if not branch_project_dict:
+        branch_project_dict = {manifest_branch: [manifest_branch]}
+
+    # Return branch_projects corresponding to branch_manifest
+    return branch_project_dict.get(manifest_branch, [])
 
 class Project:
     def __init__(self, project):
@@ -272,23 +301,27 @@ def main(args):
     releases = sorted(releases, key=lambda d: d.tag.timestamp, reverse=False)
 
     for p in projects:
-        for mr in p.project.mergerequests.list(
-            scope="all", state="merged", target_branch=options.branch, per_page="10000"
-        ):
-            m = MergeRequest(mr, p)
-            for r in releases:
-                if r.add_mergerequest(m):
-                    break
-
-        #Iterate again over MR because of the kirkstone/develop -> kirkstone changement
-        old_branch="kirkstone/develop"
-        for mr in p.project.mergerequests.list(
-            scope="all", state="merged", target_branch=old_branch, per_page="10000"
-        ):
-            m = MergeRequest(mr, p)
-            for r in releases:
-                if r.add_mergerequest(m):
-                    break
+        project_namespace=p.project.path_with_namespace
+        branch_list=get_project_branch_from_integration(gitlab, project_namespace, options.branch)
+        for branch in branch_list:
+            #print(f"For this project {project_namespace} we search for the MRs on this branch {branch}")
+            for mr in p.project.mergerequests.list(
+                scope="all", state="merged", target_branch=branch, per_page="10000"
+            ):
+                m = MergeRequest(mr, p)
+                for r in releases:
+                    if r.add_mergerequest(m):
+                        break
+    
+            #Iterate again over MR because of the kirkstone/develop -> kirkstone changement
+            old_branch="kirkstone/develop"
+            for mr in p.project.mergerequests.list(
+                scope="all", state="merged", target_branch=old_branch, per_page="10000"
+            ):
+                m = MergeRequest(mr, p)
+                for r in releases:
+                    if r.add_mergerequest(m):
+                        break
 
     # Sort by date, newest first
     releases = sorted(releases, key=lambda d: d.tag.timestamp, reverse=True)
-- 
GitLab