Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shtrom committed Jan 24, 2025
1 parent 716b813 commit 6f86f9c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
46 changes: 35 additions & 11 deletions src/lando/pushlog/pushlog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime
import logging
from contextlib import contextmanager
from typing import Optional

from django.db import transaction

Expand All @@ -11,8 +13,8 @@


@contextmanager
def PushLogForRepo(repo: Repo):
pushlog = PushLog(repo)
def PushLogForRepo(repo: Repo, user: str):
pushlog = PushLog(repo, user)
try:
yield pushlog
except Exception as e:
Expand All @@ -31,31 +33,53 @@ def PushLogForRepo(repo: Repo):
class PushLog:
repo: Repo
push: Push
user: str

commits: list
files: dict[str, list[str]]

def __repr__(self):
return f"<{self.__class__.__name__}({self.repo}, {self.commits})>"

def __init__(self, repo: Repo):
def __init__(self, repo: Repo, user: str, commits: list = [],
files: dict[str, list[str]] = {}):
self.repo = repo
self.commits = []
self.user = user

if not commits:
# We cannot us the default value of the argument as a mutable type, and will
# get reused on every initialisation.
commits = []
self.commits = commits

if not files:
# Same a commits above.
files = {}
self.files = files

def __repr__(self):
return f"<{self.__class__.__name__}({self.repo}, {self.user}, {self.commits})>"

# def add_commit(self, revision: Revision) -> Commit:
def add_commit(self, hash: str):
def add_commit(self, hash: str, files: list[str]):
# XXX need a container with
# author
# date
# desc
# files
# PARENTS
self.commits.append(hash)

# We create a commit object in memory, but will only write it into the DB when
# the whole push is done, and we have a transaction open.
self.commits.append(Commit(hash=hash, date=datetime.now()))
# We can only attach files to the Commit when it exists in the DB, so we hold
# them here in the meantime.
self.files[hash] = files

@transaction.atomic
def record_push(self):
push = Push.objects.create(self.repo)
"""Flush all push data to the database"""
push = Push.objects.create(repo=self.repo, user=self.user)
for commit in self.commits:
commit = Commit.objects.create(self.repo, hash=hash)
commit.save()
commit.add_files(self.files[commit.hash])
push.commits.add(commit)

push.save()
1 change: 0 additions & 1 deletion src/lando/pushlog/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from datetime import datetime

import pytest

from lando.main.models import Repo
Expand Down
13 changes: 8 additions & 5 deletions src/lando/pushlog/tests/test_pushlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,38 @@
def test__pushlog__PushLog(make_repo):
repo = make_repo(1)

with PushLogForRepo(repo) as pushlog:
with PushLogForRepo(repo, '[email protected]') as pushlog:
print(pushlog)
# r = pushlog.add_revision(...)
# r.add_files( ...)

raise AssertionError('unfinished')

@pytest.mark.django_db()
def test__pushlog__PushLog_no_commit_on_exception(make_repo):
repo = make_repo(1)

try:
with PushLogForRepo(repo) as pushlog:
with PushLogForRepo(repo, '[email protected]') as pushlog:
print(pushlog)
raise Exception()

except Exception:
pass

assert False # XXX test that pushlog not updated
raise AssertionError('unfinished')


@pytest.mark.django_db()
def test__pushlog__PushLog__no_deadlock(make_repo):
repo1 = make_repo(1)
repo2 = make_repo(2)

with PushLogForRepo(repo1) as pushlog1:
with PushLogForRepo(repo2) as pushlog2:
with PushLogForRepo(repo1, '[email protected]') as pushlog1:
with PushLogForRepo(repo2, '[email protected]') as pushlog2:
print(pushlog1)
print(pushlog2)
# r = pushlog.add_revision(...)
# r.add_files( ...)

raise AssertionError('unfinished')

0 comments on commit 6f86f9c

Please sign in to comment.