From fda1d69e768865d6b952b32036cefd2beb025653 Mon Sep 17 00:00:00 2001
From: Lorenzo Pagliai <lorenzo.pagliai@seco.com>
Date: Fri, 31 Mar 2023 13:08:20 +0200
Subject: [PATCH] [CHANGELOG] Modify script to accept project list

* Instead of hardcoding the gitlab repository ID the script accepts a
list of which the manifest project it the first element
* Introduce CHANGELOG_PROJECTS variable containing the list of projects
---
 manifest-pipeline-yocto.yml    | 17 ++++++++++-
 scripts/changelog_generator.py | 55 +++++++++++++++-------------------
 scripts/common.py              |  2 +-
 3 files changed, 41 insertions(+), 33 deletions(-)

diff --git a/manifest-pipeline-yocto.yml b/manifest-pipeline-yocto.yml
index cd83af4..8c2f69e 100644
--- a/manifest-pipeline-yocto.yml
+++ b/manifest-pipeline-yocto.yml
@@ -29,6 +29,14 @@ variables:
   GIT_SSL_NO_VERIFY: 1
   FS_EXTENSION: rootfs.tar.bz2
   IMAGE_EXTENSION: wic.bz2
+  CHANGELOG_PROJECTS:
+    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/external/poky
+    edgehog/layers/external/linux-imx
 
 
   # This is the jinja2 template file used to generate the build jobs
@@ -335,9 +343,16 @@ changelog:
   allow_failure: true
   rules:
     - *schedule_tag_rule
-  script: .gitlab-ci/scripts/changelog_generator.py
+  script:
+    - PROJECT_ARGS=""
+    - for project in ${CHANGELOG_PROJECTS}; do
+          PROJECT_ARGS="${PROJECT_ARGS} --project=$project";
+      done
+    - .gitlab-ci./scripts/changelog_generator.py
               --token=${GITBOT_TOKEN}
               --branch=${MASTER_BRANCH}
+              --project=${CI_PROJECT_PATH}
+              ${PROJECT_ARGS}
               > changelog.md
   artifacts:
     expire_in: 4 weeks
diff --git a/scripts/changelog_generator.py b/scripts/changelog_generator.py
index 39edfc9..280b262 100755
--- a/scripts/changelog_generator.py
+++ b/scripts/changelog_generator.py
@@ -18,24 +18,15 @@ import argparse
 import datetime
 import logging
 import sys
+
 import gitlab as gl
 
+import common
+
 __author__ = "Jonas Höppner"
 __email__ = "jonas.hoeppner@garz-fricke.com"
 
 GITLAB_SERVER = "https://git.seco.com"
-# ID of the Edgehog yocto group
-GITLAB_GROUP_ID = "479"
-
-# List of main repositories to collect changelog
-THINGS_PROJECT_ID = "1460"
-EMBEDDED_PROJECT_ID = "2449"
-IMX_PROJECT_ID = "1457"
-RK_PROJECT_ID = "1458"
-INTEL_PROJECT_ID = "2109"
-MANIFEST_PROJECT_ID = "1456"
-
-DEFAULTBRANCH = "kirkstone"
 
 GITLAB_TIMEFORMAT = "%Y-%m-%dT%H:%M:%S.%f%z"
 TIMEFORMAT = "%Y-%m-%d %H:%M"
@@ -147,9 +138,10 @@ class Release:
         # Ignore automated merge requests
         if m.mr.author["username"] == "gitbot":
             return False
-        # Exclude also MRs opened by a temporary access token 
-        # from Lorenzo Pagliai
-        if m.mr.author["username"] == "lorenzo.pagliai":
+        # Exclude MRs containing CICD reference in the title
+        if "CICD" in m.mr.title:
+            return False
+        if "CI/CD" in m.mr.title:
             return False
         # With the movement to git.seco.com the MRs owned by
         # the guf-gitbot have been transfered to tobias
@@ -211,8 +203,7 @@ def main(args):
         "--gitlab-url",
         help="""URL to the GitLab instance""",
         dest="gitlab_url",
-        action="store",
-        default=GITLAB_SERVER,
+        default=common.GITLAB_URL,
     )
     parser.add_argument(
         "--token",
@@ -220,13 +211,20 @@ def main(args):
         dest="token",
         required=True,
     )
+    parser.add_argument(
+        "--project",
+        help="""Project to get merge requests from
+                (can be passed multiple times, tags are read from the first one)""",
+        dest="project",
+        action="append",
+        required=True,
+    )
     parser.add_argument(
         "-b",
         "--branch",
-        action="store",
+        help="""Branch to work on""",
         dest="branch",
-        default=DEFAULTBRANCH,
-        help=("Specify the branch to work on, default is dunfell."),
+        required=True,
     )
 
     parser.add_argument(
@@ -245,17 +243,12 @@ def main(args):
     logging.debug(options)
     gitlab = gl.Gitlab(options.gitlab_url, private_token=options.token)
 
-    # Speed up, complete project lookup takes much longer
-    # then specifying the ID directly
-    things = Project(gitlab.projects.get(THINGS_PROJECT_ID)) 
-    embedded = Project(gitlab.projects.get(EMBEDDED_PROJECT_ID)) 
-    imx = Project(gitlab.projects.get(IMX_PROJECT_ID)) 
-    rk = Project(gitlab.projects.get(RK_PROJECT_ID)) 
-    intel = Project(gitlab.projects.get(INTEL_PROJECT_ID)) 
-    manifest = Project(gitlab.projects.get(MANIFEST_PROJECT_ID))
+    projects = []
+    for project in options.project:
+        projects.append(Project(gitlab.projects.get(project)))
 
     releases = []
-    for t in manifest.project.tags.list():
+    for t in projects[0].project.tags.list(): #search=options.branch
         releases.append(Release(Tag(t)))
 
     # Add dummy release with date today for new untaged commits
@@ -263,7 +256,7 @@ def main(args):
         Release(
             DummyTag(
                 "Not yet released",
-                "Merged Request already merged into "
+                "Merge Request already merged into "
                 + options.branch
                 + " but not yet released.",
             )
@@ -273,7 +266,7 @@ def main(args):
     # Sort by date, oldest first
     releases = sorted(releases, key=lambda d: d.tag.timestamp, reverse=False)
 
-    for p in [manifest, things, embedded, imx, rk, intel]:
+    for p in projects:
         for mr in p.project.mergerequests.list(
             scope="all", state="merged", target_branch=options.branch, per_page="10000"
         ):
diff --git a/scripts/common.py b/scripts/common.py
index ff00747..bec4ae7 100755
--- a/scripts/common.py
+++ b/scripts/common.py
@@ -12,7 +12,7 @@ from gitlab import GitlabAuthenticationError, GitlabGetError, GitlabMRRebaseErro
 from gitlab.v4.objects import Project
 from gitlab.v4.objects import MergeRequest
 
-
+GITLAB_URL = "https://git.seco.com"
 manifest_file = "default.xml"
 srcrev_file = "SRCREV.conf"
 pending_states = ["created", "waiting_for_resource", "preparing", "pending", "running"]
-- 
GitLab