Verified Commit 48f3063d authored by Jordan Petridіs's avatar Jordan Petridіs 🌱
Browse files 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.
parent e3d2c345
......@@ -45,7 +45,7 @@ def preserve_ci_vars(func):
def wrapper():
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):
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,
def test_find_repository_sha():
os.environ["CI_PROJECT_URL"] = ""
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment