Skip to content

Commit

Permalink
make github import-app mgmt command an atomic transaction (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas authored Nov 18, 2024
1 parent a211c0a commit 0a27959
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/

## [Unreleased]

### Fixed

- `github import-app` management command is now wrapped in an atomic transaction, in case any import steps fail.

## [0.2.0]

### Added
Expand Down
10 changes: 6 additions & 4 deletions src/django_github_app/management/commands/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Annotated
from typing import Any

from django.db import transaction
from django_typer.management import Typer
from typer import Option

Expand Down Expand Up @@ -34,7 +35,8 @@ def import_app(
"""
Import an existing GitHub App to database Models.
"""
installation = Installation.objects.create(installation_id=installation_id)
installation.refresh_from_gh(account_type=type, account_name=name)
repository_data = installation.get_repos()
Repository.objects.create_from_gh_data(repository_data, installation)
with transaction.atomic():
installation = Installation.objects.create(installation_id=installation_id)
installation.refresh_from_gh(account_type=type, account_name=name)
repository_data = installation.get_repos()
Repository.objects.create_from_gh_data(repository_data, installation)
20 changes: 20 additions & 0 deletions tests/integration/test_management_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,23 @@ def test_import_app_management_command(settings):
len(installation.get_repos())
== Repository.objects.filter(installation=installation).count()
)


def test_import_app_transaction(settings, monkeypatch):
def mock_create_from_gh_data(*args, **kwargs):
raise ValueError

monkeypatch.setattr(
Repository.objects, "create_from_gh_data", mock_create_from_gh_data
)

with pytest.raises(ValueError):
import_app(
type=settings.account_type,
name=settings.account_name,
installation_id=int(settings.installation_id),
)

assert not Installation.objects.filter(
installation_id=settings.installation_id
).exists()

0 comments on commit 0a27959

Please sign in to comment.