From 6595da356117728f237a4519c4ed87cf712fa655 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dylan=20A=C3=AFssi?= <dylan.aissi@collabora.com>
Date: Thu, 17 Oct 2024 12:26:58 +0200
Subject: [PATCH] apertis-pkg-merge-upstream-to-downstreams: ensure merge
 request is created against the right project
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In case of a forked project, like we are doing with the test of the rebase,
the GitLab API creates by default the MR against the original project
instead of the forked project.
To ensure the merge request is created against the forked project, we use
the push option merge_request.target_project to overwrite the default
behavior and to create the merge request against the forked project.

See:
https://docs.gitlab.com/ee/topics/git/commit.html#push-options-for-merge-requests

Signed-off-by: Dylan Aïssi <dylan.aissi@collabora.com>
---
 .../apertis-pkg-merge-upstream-to-downstreams | 25 ++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/tools/apertis-pkg-merge-upstream-to-downstreams b/tools/apertis-pkg-merge-upstream-to-downstreams
index d265bed..1543415 100755
--- a/tools/apertis-pkg-merge-upstream-to-downstreams
+++ b/tools/apertis-pkg-merge-upstream-to-downstreams
@@ -183,6 +183,24 @@ def ensure_downstream_branch(project_url, downstream, dry_run):
         raise
 
 
+def get_path_with_namespace(project_url):
+    url = urllib.parse.urlsplit(project_url)
+    project_id = urllib.parse.quote(removesuffix(url.path.strip("/"), ".git"), safe="")
+    url = url._replace(
+        path=f"/api/v4/projects/{project_id}",
+    )
+    project_metadata = url.geturl()
+    try:
+        res = urllib.request.urlopen(project_metadata)
+        json_res = json.load(res)
+        path_with_namespace = json_res["path_with_namespace"]
+    except urllib.error.HTTPError as e:
+        print("ERROR:", e.read().decode())
+        raise
+
+    return path_with_namespace
+
+
 def push_merge_request(
     project_url, proposed_branch, upstream, downstream, auto_merge="", dry_run=False
 ):
@@ -213,8 +231,11 @@ def push_merge_request(
 
     ensure_downstream_branch(project_url, downstream, dry_run)
 
+    project_path = get_path_with_namespace(project_url)
+
     print(
-        f"Create merge request from '{proposed_branch}' to '{downstream}'", flush=True
+        f"Create merge request from '{proposed_branch}' to '{project_path}/{downstream}'",
+        flush=True,
     )
     git_push_custom(
         "-o",
@@ -224,6 +245,8 @@ def push_merge_request(
         "-o",
         f"merge_request.target={downstream}",
         "-o",
+        f"merge_request.target_project={project_path}",
+        "-o",
         f"merge_request.title={title}",
         project_url,
         f"{proposed_branch}:{proposed_branch}",
-- 
GitLab