Skip to content

Commit ff38dd3

Browse files
add Installation manager method for creating from GH API data
1 parent 6b05aa7 commit ff38dd3

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
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+
### Added
22+
23+
- Added `Installation.objects.acreate_from_gh_data`/`Installation.objects.create_from_gh_data` manager methods.
24+
2125
## [0.1.0]
2226

2327
### Added

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ async with AsyncGitHubAPI(installation_id=installation.installation_id) as gh:
249249
##### Manager methods
250250
251251
- `acreate_from_event`/`create_from_event`: Create from installation events _(primarily for internal use)_
252+
- `acreate_from_gh_data`/`create_from_gh_data`: Create from GitHub API response data _(primarily for internal use)_
252253
- `aget_from_event`/`get_from_event`: Retrieve installation from webhook events (`gidgethub.sansio.Event`)
253254
254255
##### Model methods

src/django_github_app/models.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,34 +64,26 @@ def action(self) -> str | None:
6464

6565
class InstallationManager(models.Manager["Installation"]):
6666
async def acreate_from_event(self, event: sansio.Event):
67-
installation_data = event.data["installation"]
68-
69-
app_id = installation_data["app_id"]
67+
app_id = event.data["installation"]["app_id"]
7068

7169
if str(app_id) == app_settings.APP_ID:
72-
installation = await self.acreate(
73-
installation_id=installation_data["id"],
74-
data=installation_data,
75-
)
76-
77-
repository_data = event.data["repositories"]
70+
installation = await self.acreate_from_gh_data(event.data["installation"])
7871

79-
repositories = [
80-
Repository(
81-
installation=installation,
82-
repository_id=repository["id"],
83-
repository_node_id=repository["node_id"],
84-
full_name=repository["full_name"],
85-
)
86-
for repository in repository_data
87-
]
88-
await Repository.objects.abulk_create(repositories)
72+
await Repository.objects.acreate_from_gh_data(
73+
event.data["repositories"], installation
74+
)
8975

9076
return installation
9177

9278
def create_from_event(self, event: sansio.Event):
9379
return async_to_sync(self.acreate_from_event)(event)
9480

81+
async def acreate_from_gh_data(self, data: dict[str, str]):
82+
return await self.acreate(installation_id=data["id"], data=data)
83+
84+
def create_from_gh_data(self, data: dict[str, str]):
85+
return async_to_sync(self.acreate_from_gh_data)(data)
86+
9587
async def aget_from_event(self, event: sansio.Event):
9688
try:
9789
installation_id = event.data["installation"]["id"]

tests/test_models.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,31 @@ def test_create_from_event(self, create_event, override_app_settings):
160160
repositories
161161
)
162162

163+
@pytest.mark.asyncio
164+
async def test_acreate_from_gh_data(self):
165+
installation_data = {
166+
"id": seq.next(),
167+
"app_id": seq.next(),
168+
}
169+
170+
installation = await Installation.objects.acreate_from_gh_data(
171+
installation_data
172+
)
173+
174+
assert installation.installation_id == installation_data["id"]
175+
assert installation.data == installation_data
176+
177+
def test_create_from_gh_data(self):
178+
installation_data = {
179+
"id": seq.next(),
180+
"app_id": seq.next(),
181+
}
182+
183+
installation = Installation.objects.create_from_gh_data(installation_data)
184+
185+
assert installation.installation_id == installation_data["id"]
186+
assert installation.data == installation_data
187+
163188
@pytest.mark.asyncio
164189
async def test_aget_from_event(self, ainstallation, create_event):
165190
installation = await ainstallation

0 commit comments

Comments
 (0)