Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make github import-app mgmt command an atomic transaction #18

Merged
merged 1 commit into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()