From ac01de8d46270d31ff334ccb9cdcc6d6343ba54a Mon Sep 17 00:00:00 2001
From: Lorenzo Pagliai <lorenzo.pagliai@seco.com>
Date: Thu, 15 Dec 2022 14:58:46 +0100
Subject: [PATCH] Include 'check_if_layer_branch_is_up_to_date.py'

* This commit adds the script to check if the integration branch is
up-to-date on the layer side
* Very few modification with respect to the original script
* The script can be merged with the original one in the near future
---
 .../check_if_layer_branch_is_up_to_date.py    | 140 ++++++++++++++++++
 1 file changed, 140 insertions(+)
 create mode 100755 scripts/check_if_layer_branch_is_up_to_date.py

diff --git a/scripts/check_if_layer_branch_is_up_to_date.py b/scripts/check_if_layer_branch_is_up_to_date.py
new file mode 100755
index 0000000..e3c3024
--- /dev/null
+++ b/scripts/check_if_layer_branch_is_up_to_date.py
@@ -0,0 +1,140 @@
+#!/usr/bin/env python3
+import common
+
+import argparse
+import sys
+import logging
+from gitlab import Gitlab, GitlabGetError
+from gitlab.v4.objects import Project
+
+
+def check_if_layer_branch_is_up_to_date(
+    target_project: Project,
+    target_branch_name: str,
+    integration_branch_name: str,
+):
+    try:
+        integration_branch = target_project.branches.get(
+            integration_branch_name, retry_transient_errors=True
+        )
+    except GitlabGetError:
+        sys.exit(
+            "ERROR: could not find integration branch {} in {}.".format(
+                integration_branch_name, target_project.name
+            )
+        )
+
+    try:
+        target_branch = target_project.branches.get(
+            target_branch_name, retry_transient_errors=True
+        )
+    except GitlabGetError:
+        sys.exit(
+            "ERROR: could not find target branch {} in {}.".format(
+                target_branch_name, target_project.name
+            )
+        )
+
+    # Loop over the commits until the integration_branch head id is found
+    return common.is_commit_parent_of_project_commit(
+        target_project,
+        integration_branch.commit["id"],
+        target_branch.commit["id"],
+        limit=10,
+    )
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "--gitlab-url",
+        help="""URL to the GitLab instance""",
+        dest="gitlab_url",
+        required=True,
+    )
+    parser.add_argument(
+        "--token",
+        help="""GitLab REST API private access token""",
+        dest="token",
+        required=True,
+    )
+    parser.add_argument(
+        "--target-project",
+        help="""name of the target project""",
+        dest="target_project",
+        required=True,
+    )
+    parser.add_argument(
+        "--target-branch",
+        help="""target branch to integrate into""",
+        dest="target_branch",
+        required=True,
+    )
+    parser.add_argument(
+        "--source-project",
+        help="""name of the source project""",
+        dest="source_project",
+        required=True,
+    )
+    parser.add_argument(
+        "--recipe-name",
+        help="""name of the BB recipe (for integration branch name)""",
+        dest="recipe_name",
+        required=True,
+    )
+    parser.add_argument(
+        "--merge-request",
+        help="""source project merge request IID containing the changes to be integrated""",
+        dest="merge_request",
+        required=True,
+    )
+    parser.add_argument(
+        "-v",
+        "--verbose",
+        action="store_true",
+        help="""Increase verbosity.""",
+    )
+
+    args, _ = parser.parse_known_args()
+    if args.verbose:
+        logging.basicConfig(level=logging.DEBUG)
+
+    gitlab = Gitlab(args.gitlab_url, private_token=args.token)
+
+    logging.debug(args)
+    target_project = common.get_project(gitlab, args.target_project)
+    source_project = common.get_project(gitlab, args.source_project)
+    merge_request = common.get_merge_request(source_project, args.merge_request)
+    if merge_request is None:
+        sys.exit(
+            "ERROR: could not get %s  %s" % (source_project.name, args.merge_request)
+        )
+
+    integration_branch_name = args.recipe_name + "/" + merge_request.source_branch
+
+    if check_if_layer_branch_is_up_to_date(
+        target_project=target_project,
+        target_branch_name=args.target_branch,
+        integration_branch_name=integration_branch_name,
+    ):
+        print(
+            "Integration branch {} in {} is up to date.".format(
+                integration_branch_name, target_project.name
+            )
+        )
+    else:
+        sys.exit(
+            "Integration branch {} in {} is not up to date.\n"
+            "Please re-run the MR pipeline:\n"
+            "  1. Open the MR pipelines page:\n"
+            "     {}\n"
+            "  2. Click 'Run Pipeline'".format(
+                integration_branch_name,
+                target_project.name,
+                merge_request.web_url + "/pipelines",
+            )
+        )
+
+
+if __name__ == "__main__":
+    main()
-- 
GitLab