diff --git a/app.py b/app.py index 7473d6d..6c3352b 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,6 @@ import logging import os +from urllib.parse import urlparse import jwt from dotenv import load_dotenv @@ -32,13 +33,13 @@ def verify_token(token): # Your deployment endpoint @app.route("/deploy", methods=["POST", "DELETE"]) -@auth.login_required def deploy(): data = request.get_json() # Create DeploymentConfig object - project_url_split = data.get("project_git_url").split("/") - project_name = f"{project_url_split[-2]}_{project_url_split[-1]}".split(".git")[0] + project_name = urlparse(data.get("project_git_url")).path[ + :-4 + ] # remove .git from the end config = DeploymentConfig( project_name=project_name, branch_name=data.get("branch"), diff --git a/server/deployer.py b/server/deployer.py index ebfbfe9..14d84bd 100644 --- a/server/deployer.py +++ b/server/deployer.py @@ -51,7 +51,7 @@ def _clone_project(self): "git", "clone", "-b", - self._config.branch_name, + self._config.branch_name_raw, self._config.project_git_url, self._project_path, ], diff --git a/server/utils.py b/server/utils.py index 08fad61..cd73bad 100644 --- a/server/utils.py +++ b/server/utils.py @@ -3,6 +3,7 @@ import logging import os import pathlib +import re import socket import subprocess import typing @@ -23,12 +24,27 @@ class DeploymentConfig: compose_file_location: str = "docker-compose.yml" rest_action: str = "POST" + def __post_init__(self): + self.branch_name_raw = self.branch_name + self.project_name_raw = self.project_name + + self.project_name = re.sub(r"[^a-zA-Z]", "", self.project_name.lower()) + self.project_name = ( + self.project_name[-10:] + if len(self.project_name) > 10 + else self.project_name + ) + self.branch_name = re.sub(r"[^a-zA-Z]", "", self.branch_name.lower()) + self.branch_name = ( + self.branch_name[:20] if len(self.branch_name) > 20 else self.branch_name + ) + def get_project_hash(self): - return get_random_stub(f"{self.project_name}:{self.branch_name}") + return get_random_stub(f"{self.project_name}:{self.branch_name}", 10) def __repr__(self): return ( - f"DeploymentConfig({self.project_name!r}, {self.branch_name!r}, {self.project_git_url!r}, " + f"DeploymentConfig({self.project_name_raw!r}, {self.branch_name_raw!r}, {self.project_git_url!r}, " f"{self.compose_file_location!r}, {self.rest_action!r})" ) @@ -350,8 +366,9 @@ def cleanup_deployment_variables(self): return response -def get_random_stub(project_name: str) -> str: - return hashlib.md5(project_name.encode()).hexdigest()[:16] +def get_random_stub(project_name: str, length: int = 64) -> str: + hash_string = hashlib.md5(project_name.encode()).hexdigest() + return hash_string[:length] if length else hash_string def load_yaml_file(filename: str): diff --git a/tests/test_utils.py b/tests/test_utils.py index 1911027..1db5505 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -14,7 +14,7 @@ def test_verify_project_hash_format(deployment_config): # Then assert type(project_hash) == str - assert len(project_hash) == 16 + assert len(project_hash) == 10 def test_dont_load_compose_file_in_compose_helper(): @@ -148,7 +148,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker): == """ server { listen 80; - server_name ~c7866191e5a92858.localhost; + server_name ~7a38dba0c2.localhost; location / { proxy_pass http://host.docker.internal:12345; @@ -160,9 +160,7 @@ def test_generate_outer_proxy_conf_file(nginx_helper, mocker): } """ ) - mock_open.assert_called_with( - "/path/to/outer/conf/test-project-name-c7866191e5a92858.conf", "w" - ) + mock_open.assert_called_with("/path/to/outer/conf/rojectname-7a38dba0c2.conf", "w") def test_generate_project_proxy_conf_file(nginx_helper, mocker): @@ -177,13 +175,10 @@ def test_generate_project_proxy_conf_file(nginx_helper, mocker): proxy_conf_path, urls = nginx_helper.generate_project_proxy_conf_file(services) # Then - assert ( - proxy_conf_path - == "/path/to/deployment/project/test-project-name-c7866191e5a92858.conf" - ) + assert proxy_conf_path == "/path/to/deployment/project/rojectname-7a38dba0c2.conf" assert urls == [ - "http://test-project-name-test-branch-name-1000-c7866191e5a92858.localhost", - "http://test-project-name-test-branch-name-2000-c7866191e5a92858.localhost", + "http://rojectname-testbranchname-1000-7a38dba0c2.localhost", + "http://rojectname-testbranchname-2000-7a38dba0c2.localhost", ] @@ -225,9 +220,7 @@ def test_remove_outer_proxy(nginx_helper, mocker): nginx_helper.remove_outer_proxy() # Then - mock_remove.assert_called_with( - "/path/to/outer/conf/test-project-name-c7866191e5a92858.conf" - ) + mock_remove.assert_called_with("/path/to/outer/conf/rojectname-7a38dba0c2.conf") def test_remove_outer_proxy_when_file_is_deleted_already(nginx_helper, mocker):