From 48f3063dafb299066351eec118f2e7887d909364 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 13 Nov 2018 12:52:17 +0200 Subject: [PATCH] build_manifest.py: Use CI_PROJECT_NAMESPACE instead of GITLAB_USER GITLAB_USER_* variables represent the user *that triggered* the job, which might not match the user that owns the namespace of the fork. This happens frequently with rebase, where the one who triggers the rebase is the one that appears in the GITLAB_USER_* vars. Instead use the CI_PROJECT_NAMESPACE variable to search the user's projects. If CI_PROJECT_NAMESPACE has the value of a Group namespace search_user_namespace returns 404. --- gitlab/build_manifest.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/gitlab/build_manifest.py b/gitlab/build_manifest.py index 14884fca74..669c4ddee8 100755 --- a/gitlab/build_manifest.py +++ b/gitlab/build_manifest.py @@ -45,7 +45,7 @@ def preserve_ci_vars(func): def wrapper(): try: url = os.environ["CI_PROJECT_URL"] - user = os.environ["GITLAB_USER_LOGIN"] + user = os.environ["CI_PROJECT_NAMESPACE"] except KeyError: url = "invalid" user = "" @@ -57,7 +57,7 @@ def preserve_ci_vars(func): func() os.environ["CI_PROJECT_URL"] = url - os.environ["GITLAB_USER_LOGIN"] = user + os.environ["CI_PROJECT_NAMESPACE"] = user if private: os.environ["READ_PROJECTS_TOKEN"] = private @@ -177,10 +177,14 @@ def test_search_user_namespace(): res = search_user_namespace("alatiera", "404-project-not-found") assert res is None + # Passing a group namespace instead of user should return None + res = search_user_namespace("gstreamer", "gst-plugins-good") + assert res is None + def find_repository_sha(module: Tuple[str, int], branchname: str) -> Tuple[str, str]: - user_login: str = os.environ["GITLAB_USER_LOGIN"] - project = search_user_namespace(user_login, module[0]) + namespace: str = os.environ["CI_PROJECT_NAMESPACE"] + project = search_user_namespace(namespace, module[0]) # Find a fork in the User's namespace if project: @@ -219,7 +223,7 @@ def find_repository_sha(module: Tuple[str, int], branchname: str) -> Tuple[str, @preserve_ci_vars def test_find_repository_sha(): os.environ["CI_PROJECT_URL"] = "https://gitlab.freedesktop.org/gstreamer/gst-plugins-good" - os.environ["GITLAB_USER_LOGIN"] = "alatiera" + os.environ["CI_PROJECT_NAMESPACE"] = "alatiera" del os.environ["READ_PROJECTS_TOKEN"] # This should find the repository in the user namespace