Commit 794738ab authored by Aziz Berkay Yesilyurt's avatar Aziz Berkay Yesilyurt
Browse files

fix: gitlab sluggify logic

Showing with 84 additions and 27 deletions
+84 -27
......@@ -12,7 +12,7 @@ from loguru import logger
from pymemri.pod.client import PodClient
from .template.formatter import _plugin_from_template, str_to_gitlab_identifier
from .template.formatter import _plugin_from_template, gitlab_slugify
MEMRI_PATH = Path.home() / ".memri"
MEMRI_GITLAB_BASE_URL = "https://gitlab.memri.io"
......@@ -36,8 +36,8 @@ class GitlabAPI:
def init_auth_params(self):
access_token = self.client.get_oauth2_access_token("gitlab")
if access_token is None:
raise ValueError(
"No gitlab access token found in the pod."
raise RuntimeError(
"No gitlab access token found in the pod. "
"Please authenticate with gitlab in the frontend."
)
self.auth_params = {"access_token": access_token}
......@@ -73,6 +73,14 @@ class GitlabAPI:
self.auth_headers = {"PRIVATE-TOKEN": access_token}
self.auth_initialized = True
# test if we are authenticated
res = self.get(
f"{GITLAB_API_BASE_URL}/projects", headers=self.auth_headers, params=self.auth_params
)
if res.status_code not in [200, 201]:
logger.error(res.content)
raise RuntimeError(res.content)
def write_file_to_package_registry(
self,
project_id,
......@@ -132,23 +140,25 @@ class GitlabAPI:
logger.error("Failed to trigger pipeline")
def project_id_from_name(self, project_name):
iden = str_to_gitlab_identifier(project_name)
iden = gitlab_slugify(project_name)
username = self.get_current_username()
uri_encoded_name = urllib.parse.quote_plus(f"{username}/{iden}")
res = self.get(
f"{GITLAB_API_BASE_URL}/projects",
f"{GITLAB_API_BASE_URL}/projects/{uri_encoded_name}",
headers=self.auth_headers,
params={**self.auth_params, **{"owned": True, "search": project_name}},
params=self.auth_params,
)
if res.status_code not in [200, 201]:
logger.error(res.content)
raise RuntimeError(f"Failed to get project id for {project_name}")
# we need this extra filter (search is not exact match)
res = [x.get("id") for x in res.json() if x.get("path", None) == iden]
if len(res) == 0:
project_id = res.json().get("id", None)
if project_id:
return project_id
else:
raise ValueError(
f"No plugin found with name {project_name}, make sure to enter the name as specified in the url of the repo"
)
else:
return res[0]
def get_project_id_from_project_path_unsafe(self, project_path):
try:
......@@ -214,7 +224,7 @@ class GitlabAPI:
# NEVER EXPORT THIS
def delete_project(self, path_or_id, client=None):
url_escape_id = urllib.parse.quote(path_or_id, safe="")
url_escape_id = urllib.parse.quote(str(path_or_id), safe="")
url = f"{GITLAB_API_BASE_URL}/projects/{url_escape_id}"
res = self.delete(url=url, headers=self.auth_headers, params=self.auth_params)
if res.status_code not in [200, 201, 202]:
......
......@@ -67,7 +67,9 @@ class PodClient:
return "".join([str(random.randint(0, 9)) for i in range(64)])
def register_base_schemas(self):
assert self.add_to_schema(PluginRun, Account)
result = self.add_to_schema(PluginRun, Account)
if not result:
raise ValueError("Could not register base schemas")
def add_to_store(self, item: Item, priority: Priority = None) -> Item:
item.create_id_if_not_exists()
......@@ -109,15 +111,11 @@ class PodClient:
raise ValueError(f"{item} is not an instance or subclass of Item")
create_items.extend(item.pod_schema())
try:
self.api.bulk(create_items=create_items)
for item in items:
item_cls = type(item) if isinstance(item, Item) else item
self.registered_classes[item.__name__] = item_cls
return True
except Exception as e:
logger.error(e)
return False
self.api.bulk(create_items=create_items)
for item in items:
item_cls = type(item) if isinstance(item, Item) else item
self.registered_classes[item.__name__] = item_cls
return True
def _upload_image(self, img, asyncFlag=True, callback=None):
if isinstance(img, np.ndarray):
......
......@@ -58,11 +58,12 @@ def str_to_identifier(s, lower=True):
return result
def str_to_gitlab_identifier(s, lower=True):
result = re.sub("\W|^(?=\d)", "-", s)
if lower:
result = result.lower()
return result
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/utils.rb#L92
def gitlab_slugify(s):
s = s.lower()
s = re.sub(r"[^a-z0-9]", "-", s)[:63]
s = re.sub(r"(^-+|-+$)", "", s)
return s
def reponame_to_displayname(reponame: str) -> str:
......
import pytest
from pymemri.gitlab_api import GitlabAPI
from pymemri.pod.client import PodClient
from pymemri.template.formatter import gitlab_slugify
def test_gitlab_slugify():
# Lowercased
string = "SAMPLE Project"
assert gitlab_slugify(string) == "sample-project"
# First/Last Character is not a hyphen
string = "--sample space project--"
assert gitlab_slugify(string) == "sample-space-project"
# Maximum length is 63 bytes
string = "s" * 64
assert gitlab_slugify(string) == "s" * 63
# Anything not matching [a-z0-9-] is replaced with a -
string = "sample project"
assert gitlab_slugify(string) == "sample-project"
string = "sample_project"
assert gitlab_slugify(string) == "sample-project"
string = "01-09-test"
assert gitlab_slugify(string) == "01-09-test"
def test_gitlab_missing_oauth():
# Create a random owner/database key pair
client = PodClient()
with pytest.raises(RuntimeError):
gitlab = GitlabAPI(client)
@pytest.mark.skip(
reason="This test requires a valid Gitlab API token, which is not available in CI"
)
def test_gitlab_project_id():
owner_key = ""
database_key = ""
client = PodClient(owner_key=owner_key, database_key=database_key)
gitlab = GitlabAPI(client)
project_id = gitlab.project_id_from_name("test-proj")
Supports Markdown
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