Skip to content

Commit

Permalink
feat: Apply PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik003 authored and MoritzWeber0 committed Sep 20, 2024
1 parent 60b4c20 commit ab5061c
Show file tree
Hide file tree
Showing 16 changed files with 195 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DiagramMetadata(core_pydantic.BaseModel):
class DiagramCacheMetadata(core_pydantic.BaseModel):
diagrams: list[DiagramMetadata]
last_updated: datetime.datetime
job_id: str | int | None = None
job_id: str | None = None

_validate_last_updated = pydantic.field_serializer("last_updated")(
core_pydantic.datetime_serializer
Expand Down
59 changes: 15 additions & 44 deletions backend/capellacollab/projects/toolmodels/diagrams/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import fastapi
import requests
from aiohttp import web

import capellacollab.projects.toolmodels.modelsources.git.injectables as git_injectables
from capellacollab.core import logging as log
Expand Down Expand Up @@ -42,34 +41,21 @@ async def get_diagram_metadata(
),
logger: logging.LoggerAdapter = fastapi.Depends(log.get_request_logger),
):
job_id = None
try:
last_updated, diagram_metadata_entries = await handler.get_file(
trusted_file_path="diagram_cache/index.json",
revision=f"diagram-cache/{handler.revision}",
job_id, last_updated, diagram_metadata_entries = (
await handler.get_file_or_artifact(
trusted_file_path="diagram_cache/index.json",
job_name="update_capella_diagram_cache",
file_revision=f"diagram-cache/{handler.revision}",
)
)
except Exception:
except requests.HTTPError:
logger.info(

Check warning on line 53 in backend/capellacollab/projects/toolmodels/diagrams/routes.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/diagrams/routes.py#L53

Added line #L53 was not covered by tests
"Failed fetching diagram metadata file for %s on revision %s.",
"Failed fetching diagram metadata file or artifact for %s",
handler.path,
f"diagram-cache/{handler.revision}",
exc_info=True,
)
try:
job_id, last_updated, diagram_metadata_entries = (
await handler.get_artifact(
trusted_file_path="diagram_cache/index.json",
job_name="update_capella_diagram_cache",
)
)
except (web.HTTPError, requests.HTTPError):
logger.info(
"Failed fetching diagram metadata artifact for %s on revision %s",
handler.path,
handler.revision,
exc_info=True,
)
raise exceptions.DiagramCacheNotConfiguredProperlyError()
raise exceptions.DiagramCacheNotConfiguredProperlyError()

diagram_metadata_entries = json.loads(diagram_metadata_entries)
return models.DiagramCacheMetadata(
Expand All @@ -89,7 +75,7 @@ async def get_diagram_metadata(
)
async def get_diagram(
diagram_uuid_or_filename: str,
job_id: str | int | None = None,
job_id: str | None = None,
handler: git_handler.GitHandler = fastapi.Depends(
git_injectables.get_git_handler
),
Expand All @@ -102,32 +88,17 @@ async def get_diagram(
diagram_uuid = pathlib.PurePosixPath(diagram_uuid_or_filename).stem
file_path = f"diagram_cache/{parse.quote(diagram_uuid, safe='')}.svg"

if not job_id:
try:
file = await handler.get_file(
trusted_file_path=file_path,
revision=f"diagram-cache/{handler.revision}",
)
return responses.SVGResponse(content=file[1])
except Exception:
logger.info(
"Failed fetching diagram file %s for %s on revision %s.",
diagram_uuid,
handler.path,
f"diagram-cache/{handler.revision}",
exc_info=True,
)

try:
artifact = await handler.get_artifact(
file_or_artifact = await handler.get_file_or_artifact(
trusted_file_path=file_path,
job_name="update_capella_diagram_cache",
job_id=job_id,
file_revision=f"diagram-cache/{handler.revision}",
)
return responses.SVGResponse(content=artifact[2])
except (web.HTTPError, requests.HTTPError):
return responses.SVGResponse(content=file_or_artifact[2])
except requests.HTTPError:
logger.info(

Check warning on line 100 in backend/capellacollab/projects/toolmodels/diagrams/routes.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/diagrams/routes.py#L99-L100

Added lines #L99 - L100 were not covered by tests
"Failed fetching diagram artifact %s for %s on revision %s.",
"Failed fetching diagram file or artifact %s for %s.",
diagram_uuid,
handler.path,
f"diagram-cache/{handler.revision}",
Expand Down
23 changes: 4 additions & 19 deletions backend/capellacollab/projects/toolmodels/modelbadge/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import logging

import fastapi
import requests
from aiohttp import web

import capellacollab.projects.toolmodels.modelsources.git.injectables as git_injectables
from capellacollab.core import logging as log
Expand Down Expand Up @@ -41,26 +39,13 @@ async def get_model_complexity_badge(
logger: logging.LoggerAdapter = fastapi.Depends(log.get_request_logger),
):
try:
file = await git_handler.get_file(
"model-complexity-badge.svg", git_handler.revision
)
return responses.SVGResponse(content=file[1])
except Exception:
logger.debug(
"Failed fetching model badge file for %s on revision %s.",
git_handler.path,
git_handler.revision,
exc_info=True,
)

try:
artifact = await git_handler.get_artifact(
file_or_artifact = await git_handler.get_file_or_artifact(
"model-complexity-badge.svg", "generate-model-badge"
)
return responses.SVGResponse(content=artifact[2])
except (web.HTTPError, requests.HTTPError):
return responses.SVGResponse(content=file_or_artifact[2])
except Exception:
logger.debug(

Check warning on line 47 in backend/capellacollab/projects/toolmodels/modelbadge/routes.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelbadge/routes.py#L46-L47

Added lines #L46 - L47 were not covered by tests
"Failed fetching model badge artifact for %s on revision %s.",
"Failed fetching model badge file or artifact for %s on revision %s.",
git_handler.path,
git_handler.revision,
exc_info=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def get_last_successful_job_run(
created_at = datetime.datetime.fromisoformat(
latest_job["created_at"]
)
return (latest_job["id"], created_at)
return (str(latest_job["id"]), created_at)

raise git_exceptions.GitPipelineJobNotFoundError(

Check warning on line 35 in backend/capellacollab/projects/toolmodels/modelsources/git/github/handler.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/github/handler.py#L35

Added line #L35 was not covered by tests
job_name=job_name, revision=self.revision
Expand Down Expand Up @@ -86,7 +86,7 @@ def get_last_pipeline_runs(self) -> t.Any:
return response.json()["workflow_runs"]

def get_artifact_from_job(
self, job_id: str | int, trusted_path_to_artifact: str
self, job_id: str, trusted_path_to_artifact: str
) -> bytes:
artifact = self.__get_latest_artifact_metadata(job_id)
artifact_id = artifact["id"]
Expand Down Expand Up @@ -118,7 +118,7 @@ def get_last_updated_for_file(
response.json()[0]["commit"]["author"]["date"]
)

def get_started_at_for_job(self, job_id: str | int) -> datetime.datetime:
def get_started_at_for_job(self, job_id: str) -> datetime.datetime:
response = requests.get(

Check warning on line 122 in backend/capellacollab/projects/toolmodels/modelsources/git/github/handler.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/github/handler.py#L122

Added line #L122 was not covered by tests
f"{self.api_url}/repos/{self.repository_id}/actions/runs/{job_id}",
headers=self.__get_headers(),
Expand Down Expand Up @@ -153,7 +153,7 @@ def __get_latest_successful_job(
job_name, matched_jobs[0]["conclusion"]
)

def __get_latest_artifact_metadata(self, job_id: str | int):
def __get_latest_artifact_metadata(self, job_id: str):
response = requests.get(
f"{self.api_url}/repos/{self.repository_id}/actions/runs/{job_id}/artifacts",
headers=self.__get_headers(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def get_last_successful_job_run(
if job := await self.__get_job_id_for_job_name(
pipeline_id, job_name
):
return job
return (str(job[0]), job[1])

raise git_exceptions.GitPipelineJobNotFoundError(
job_name=job_name, revision=self.revision
Expand All @@ -70,7 +70,7 @@ def get_last_updated_for_file(
response.json()[0]["authored_date"]
)

def get_started_at_for_job(self, job_id: str | int) -> datetime.datetime:
def get_started_at_for_job(self, job_id: str) -> datetime.datetime:
response = requests.get(

Check warning on line 74 in backend/capellacollab/projects/toolmodels/modelsources/git/gitlab/handler.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/gitlab/handler.py#L74

Added line #L74 was not covered by tests
f"{self.api_url}/projects/{self.repository_id}/jobs/{job_id}",
headers={"PRIVATE-TOKEN": self.password},
Expand Down Expand Up @@ -117,7 +117,7 @@ async def __get_job_id_for_job_name(
return None

def get_artifact_from_job(
self, job_id: str | int, trusted_path_to_artifact: str
self, job_id: str, trusted_path_to_artifact: str
) -> bytes:
response = requests.get(
f"{self.api_url}/projects/{self.repository_id}/jobs/{job_id}/artifacts/{trusted_path_to_artifact}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get_file_data(
return None

Check warning on line 26 in backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py#L26

Added line #L26 was not covered by tests

def get_artifact_data(
self, job_id: str | int, file_path: str
self, job_id: str, file_path: str
) -> tuple[datetime.datetime, bytes] | None:
artifact_data = self._valkey.hmget(

Check warning on line 31 in backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py#L31

Added line #L31 was not covered by tests
name=self._get_artifact_key(job_id, file_path),
Expand Down Expand Up @@ -59,7 +59,7 @@ def put_file_data(

def put_artifact_data(
self,
job_id: str | int,
job_id: str,
file_path: str,
started_at: datetime.datetime,
content: bytes,
Expand All @@ -80,7 +80,7 @@ def clear(self) -> None:
def _get_file_key(self, file_path: str, revision: str) -> str:
return f"{self.git_model_id}:f:{self._escape_string(revision)}:{self._escape_string(file_path)}"

Check warning on line 81 in backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py#L81

Added line #L81 was not covered by tests

def _get_artifact_key(self, job_id: str | int, file_path: str) -> str:
def _get_artifact_key(self, job_id: str, file_path: str) -> str:
return (

Check warning on line 84 in backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/handler/cache.py#L84

Added line #L84 was not covered by tests
f"{self.git_model_id}:a:{job_id}:{self._escape_string(file_path)}"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,3 @@ def __init__(self):
),
err_code="GIT_INSTANCE_NO_API_ENDPOINT_DEFINED",
)


class GitRepositoryIdNotFoundError(core_exceptions.BaseError):
def __init__(self):
super().__init__(
status_code=status.HTTP_404_NOT_FOUND,
title="Git model repository id not found",
reason="The used Git model has no repository id. Please contact your administrator",
err_code="GIT_MODEL_REPOSITORY_ID_NOT_SET",
)
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ async def create_git_handler(

if not git_model.repository_id:
repository_id = await GitHandlerFactory._get_repository_id(
git_model, git_instance
git_model, git_instance.api_url, git_instance.type
)
git_crud.update_git_model_repository_id(
db, git_model, repository_id
)
else:
repository_id = git_model.repository_id

return GitHandlerFactory._create_specific_git_handler(
git_model, git_instance
git_model,
repository_id,
git_instance.api_url,
git_instance.type,
)

@staticmethod
Expand All @@ -73,55 +78,50 @@ def get_git_instance_for_git_model(
@staticmethod
async def _get_repository_id(
git_model: git_models.DatabaseGitModel,
git_instance: settings_git_models.DatabaseGitInstance,
git_instance_api_url: str,
git_instance_type: settings_git_models.GitType,
) -> str:
if not (api_url := git_instance.api_url):
raise exceptions.GitInstanceAPIEndpointNotFoundError()

match git_instance.type:
match git_instance_type:
case settings_git_models.GitType.GITLAB:
return await gitlab_handler.GitlabHandler.get_repository_id_by_git_url(
git_model.path, git_model.password, api_url
git_model.path, git_model.password, git_instance_api_url
)
case settings_git_models.GitType.GITHUB:
return await github_handler.GithubHandler.get_repository_id_by_git_url(
git_model.path, git_model.password
)
case _:
raise exceptions.GitInstanceUnsupportedError(
instance_name=str(git_instance.type)
instance_name=str(git_instance_type)
)

@staticmethod
def _create_specific_git_handler(
git_model: git_models.DatabaseGitModel,
git_instance: settings_git_models.DatabaseGitInstance,
git_model_repository_id: str,
git_instance_api_url: str,
git_instance_type: settings_git_models.GitType,
) -> handler.GitHandler:
if not (api_url := git_instance.api_url):
raise exceptions.GitInstanceAPIEndpointNotFoundError()
if not (repository_id := git_model.repository_id):
raise exceptions.GitRepositoryIdNotFoundError()

match git_instance.type:
match git_instance_type:
case settings_git_models.GitType.GITLAB:
return gitlab_handler.GitlabHandler(
git_model.id,
git_model.path,
git_model.revision,
git_model.password,
api_url,
repository_id,
git_instance_api_url,
git_model_repository_id,
)
case settings_git_models.GitType.GITHUB:
return github_handler.GithubHandler(
git_model.id,
git_model.path,
git_model.revision,
git_model.password,
api_url,
repository_id,
git_instance_api_url,
git_model_repository_id,
)
case _:
raise exceptions.GitInstanceUnsupportedError(

Check warning on line 125 in backend/capellacollab/projects/toolmodels/modelsources/git/handler/factory.py

View check run for this annotation

Codecov / codecov/patch

backend/capellacollab/projects/toolmodels/modelsources/git/handler/factory.py#L124-L125

Added lines #L124 - L125 were not covered by tests
instance_name=str(git_instance.type)
instance_name=str(git_instance_type)
)
Loading

0 comments on commit ab5061c

Please sign in to comment.