Skip to content

Commit

Permalink
fix: rework project sync on empty repo (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzwei authored Dec 3, 2024
1 parent 892af73 commit f21063a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 35 deletions.
16 changes: 11 additions & 5 deletions src/aap_eda/services/project/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

from aap_eda.core import models
from aap_eda.core.types import StrPath
from aap_eda.services.project.scm import ScmEmptyError, ScmRepository
from aap_eda.services.project.scm import ScmRepository

logger = logging.getLogger(__name__)

Expand All @@ -46,6 +46,10 @@ class ProjectImportError(Exception):
pass


class ProjectImportWarning(Exception):
pass


class MalformedError(Exception):
pass

Expand All @@ -61,8 +65,8 @@ def wrapper(self: ProjectImportService, project: models.Project):
try:
func(self, project)
project.import_state = models.Project.ImportState.COMPLETED
except ScmEmptyError as e:
# if a project is empty, sync status should show completed
except ProjectImportWarning as e:
# import status should show completed but with an error message
project.import_state = models.Project.ImportState.COMPLETED
project.import_error = str(e)
except Exception as e:
Expand Down Expand Up @@ -142,7 +146,9 @@ def _clone_and_process(self, project: models.Project):
)
yield repo_dir, repo.rev_parse("HEAD")
if project.rulebook_set.count() == 0:
raise ScmEmptyError("This project contains no rulebooks.")
raise ProjectImportWarning(
"This project contains no rulebooks."
)

def _temporary_directory(self) -> tempfile.TemporaryDirectory:
return tempfile.TemporaryDirectory(prefix=TMP_PREFIX)
Expand Down Expand Up @@ -207,7 +213,7 @@ def _find_rulebooks(self, repo: StrPath) -> Iterator[RulebookInfo]:
break

if not rulebooks_dir:
raise ProjectImportError(
raise ProjectImportWarning(
"The 'extensions/eda/rulebooks' or 'rulebooks' directory"
" doesn't exist within the project root."
)
Expand Down
14 changes: 0 additions & 14 deletions src/aap_eda/services/project/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ class ScmError(Exception):
pass


class ScmEmptyError(ScmError):
"""The checkout repository is empty which is treated as an error."""

pass


class ScmAuthenticationError(Exception):
"""SCM Authentication error."""

Expand Down Expand Up @@ -289,10 +283,6 @@ def add_gpg_key(cls, key_file: str, home_dir: str) -> None:

class GitAnsibleRunnerExecutor:
ERROR_PREFIX = "Project Import Error:"
PROJECT_EMPTY_ERROR_MSG = (
"Project folder is empty. Please add "
"content to your project and try syncing it again."
)

def __call__(
self,
Expand Down Expand Up @@ -330,9 +320,5 @@ def __call__(
):
err_msg = "Credentials not provided or incorrect"
raise ScmAuthenticationError(err_msg)
if "did not match any file" in err_msg and err_msg.startswith(
'"Failed to checkout branch'
):
raise ScmEmptyError(self.PROJECT_EMPTY_ERROR_MSG)
raise ScmError(f"{self.ERROR_PREFIX} {err_msg}")
raise ScmError(f"{self.ERROR_PREFIX} {outputs.getvalue().strip()}")
20 changes: 4 additions & 16 deletions tests/integration/services/test_project_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
import logging
import os
import re
import shutil
import tempfile
from pathlib import Path
Expand All @@ -23,7 +22,6 @@

from aap_eda.core import models
from aap_eda.services.project import ProjectImportService
from aap_eda.services.project.imports import ProjectImportError
from aap_eda.services.project.scm import ScmRepository

DATA_DIR = Path(__file__).parent / "data"
Expand Down Expand Up @@ -175,25 +173,15 @@ def test_project_import_rulebook_directory_missing(
)

service = ProjectImportService(scm_cls=git_mock)
with pytest.raises(
ProjectImportError,
match=re.escape(message_expected),
):
service.import_project(project)
service.import_project(project)

project = models.Project.objects.get(id=project.id)
assert project.import_state == models.Project.ImportState.FAILED
assert project.import_state == models.Project.ImportState.COMPLETED
assert project.import_error == message_expected

# resync the project does not clear the import_state and import_error
with pytest.raises(
ProjectImportError,
match=re.escape(message_expected),
):
service.sync_project(project)

service.sync_project(project)
project.refresh_from_db()
assert project.import_state == models.Project.ImportState.FAILED
assert project.import_state == models.Project.ImportState.COMPLETED
assert project.import_error == message_expected


Expand Down

0 comments on commit f21063a

Please sign in to comment.