Skip to content

Commit 0a27959

Browse files
make github import-app mgmt command an atomic transaction (#18)
1 parent a211c0a commit 0a27959

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ and this project attempts to adhere to [Semantic Versioning](https://semver.org/
1818

1919
## [Unreleased]
2020

21+
### Fixed
22+
23+
- `github import-app` management command is now wrapped in an atomic transaction, in case any import steps fail.
24+
2125
## [0.2.0]
2226

2327
### Added

src/django_github_app/management/commands/github.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Annotated
44
from typing import Any
55

6+
from django.db import transaction
67
from django_typer.management import Typer
78
from typer import Option
89

@@ -34,7 +35,8 @@ def import_app(
3435
"""
3536
Import an existing GitHub App to database Models.
3637
"""
37-
installation = Installation.objects.create(installation_id=installation_id)
38-
installation.refresh_from_gh(account_type=type, account_name=name)
39-
repository_data = installation.get_repos()
40-
Repository.objects.create_from_gh_data(repository_data, installation)
38+
with transaction.atomic():
39+
installation = Installation.objects.create(installation_id=installation_id)
40+
installation.refresh_from_gh(account_type=type, account_name=name)
41+
repository_data = installation.get_repos()
42+
Repository.objects.create_from_gh_data(repository_data, installation)

tests/integration/test_management_commands.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,23 @@ def test_import_app_management_command(settings):
8080
len(installation.get_repos())
8181
== Repository.objects.filter(installation=installation).count()
8282
)
83+
84+
85+
def test_import_app_transaction(settings, monkeypatch):
86+
def mock_create_from_gh_data(*args, **kwargs):
87+
raise ValueError
88+
89+
monkeypatch.setattr(
90+
Repository.objects, "create_from_gh_data", mock_create_from_gh_data
91+
)
92+
93+
with pytest.raises(ValueError):
94+
import_app(
95+
type=settings.account_type,
96+
name=settings.account_name,
97+
installation_id=int(settings.installation_id),
98+
)
99+
100+
assert not Installation.objects.filter(
101+
installation_id=settings.installation_id
102+
).exists()

0 commit comments

Comments
 (0)