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

chore: use helper functions to init test data and rm test instability #98

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
116 changes: 93 additions & 23 deletions interface/db/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .issues import DBIssue
from .interfaces import DBInterfaces
from .cache import RecordCount
from .activity import DBActivity, ActivityType


@dataclass
Expand All @@ -37,6 +38,8 @@ class DBComment:
belongs_to_issue: DBIssue
count = RecordCount("gitea_issue_comments")

id = None

__belongs_to_issue_id: int = None

def __set_sqlite_to_bools(self):
Expand All @@ -48,28 +51,44 @@ def __set_sqlite_to_bools(self):
"""
self.is_native = bool(self.is_native)

def __update(self):
def __update(self, from_db: "DBComment" = None):
"""
Update changes in database
Only fields that can be mutated on the forge will be updated in the DB
"""
conn = get_db()
cur = conn.cursor()
cur.execute(
"""
UPDATE gitea_issue_comments
SET
body = ?,
updated = ?
WHERE
comment_id = ?;
""",
(self.body, self.updated, self.comment_id),
)
conn.commit()
comment = from_db
if from_db is None:
comment = self.load_from_id(self.id)
if any([comment.body != self.body, comment.updated != self.updated]):
conn = get_db()
cur = conn.cursor()
cur.execute(
"""
UPDATE gitea_issue_comments
SET
body = ?,
updated = ?
WHERE
comment_id = ?;
""",
(self.body, self.updated, self.comment_id),
)
conn.commit()
DBActivity(
user_id=self.user.id,
activity=ActivityType.UPDATE,
created=self.updated,
comment_id=self.id,
).save()

def save(self):
"""Save COmment to database"""

comment = self.load_from_comment_url(self.html_url)
if comment is not None:
self.id = comment.id
self.__update(from_db=comment)

self.user.save()
self.belongs_to_issue.save()

Expand Down Expand Up @@ -106,13 +125,18 @@ def save(self):
conn.commit()
data = cur.execute(
"""
SELECT ID from gitea_issue_comments WHERE html_url = ?

""",
SELECT ID from gitea_issue_comments WHERE html_url = ?
""",
(self.html_url,),
).fetchone()
self.id = data[0]
self.__update()
DBActivity(
user_id=self.user.id,
activity=ActivityType.CREATE,
created=self.created,
comment_id=self.id,
).save()

@classmethod
def load_from_comment_url(cls, comment_url: str) -> "DBComment":
Expand All @@ -128,7 +152,8 @@ def load_from_comment_url(cls, comment_url: str) -> "DBComment":
updated,
comment_id,
is_native,
user
user,
ID
FROM
gitea_issue_comments
WHERE
Expand All @@ -151,6 +176,48 @@ def load_from_comment_url(cls, comment_url: str) -> "DBComment":
user=user,
belongs_to_issue=belongs_to_issue,
)
comment.id = data[7]
comment.__set_sqlite_to_bools()
return comment

@classmethod
def load_from_id(cls, db_id: int) -> "DBComment":
"""Load comment based on ID assigned by database"""
conn = get_db()
cur = conn.cursor()
data = cur.execute(
"""
SELECT
body,
belongs_to_issue,
created,
updated,
comment_id,
is_native,
user,
html_url
FROM
gitea_issue_comments
WHERE
ID = ?
""",
(db_id,),
).fetchone()
if data is None:
return None

user = DBUser.load_with_db_id(data[6])
belongs_to_issue = DBIssue.load_with_id(data[1])
comment = cls(
body=data[0],
html_url=data[7],
created=data[2],
updated=data[3],
comment_id=data[4],
is_native=data[5],
user=user,
belongs_to_issue=belongs_to_issue,
)
comment.__set_sqlite_to_bools()
return comment

Expand All @@ -168,11 +235,13 @@ def load_issue_comments(cls, issue: DBIssue) -> "[DBComment]":
updated,
comment_id,
is_native,
user
user,
ID
FROM
gitea_issue_comments
WHERE
belongs_to_issue = ?
ORDER BY created
""",
(issue.id,),
).fetchall()
Expand All @@ -182,7 +251,7 @@ def load_issue_comments(cls, issue: DBIssue) -> "[DBComment]":
comments = []
for comment in data:
user = DBUser.load_with_db_id(comment[6])
comment = cls(
obj = cls(
body=comment[0],
html_url=comment[1],
created=comment[2],
Expand All @@ -192,8 +261,9 @@ def load_issue_comments(cls, issue: DBIssue) -> "[DBComment]":
user=user,
belongs_to_issue=issue,
)
comment.__set_sqlite_to_bools()
comments.append(comment)
obj.id = comment[7]
obj.__set_sqlite_to_bools()
comments.append(obj)

if len(comments) == 0:
return None
Expand Down
68 changes: 43 additions & 25 deletions interface/db/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,35 +125,52 @@ def set_open(self, updated: str):
self.is_merged = False
self.__update()

def __update(self):
def __update(self, from_db: "DBIssue" = None):
"""
Update changes in database
Only fields that can be mutated on the forge will be updated in the DB
"""
conn = get_db()
cur = conn.cursor()
cur.execute(
"""
UPDATE gitea_forge_issues
SET
title = ?,
description = ?,
updated = ?,
is_closed = ?,
is_merged = ?
WHERE
id = ?
""",
(
self.title,
self.description,
self.updated,
self.is_closed,
self.is_merged,
self.id,
),
)
conn.commit()
issue = from_db
if from_db is None:
issue = self.load_with_id(db_id=self.id)
if any(
[
issue.title != self.title,
issue.description != self.description,
issue.is_closed != self.is_closed,
issue.is_merged != self.is_merged,
]
):
conn = get_db()
cur = conn.cursor()
cur.execute(
"""
UPDATE gitea_forge_issues
SET
title = ?,
description = ?,
updated = ?,
is_closed = ?,
is_merged = ?
WHERE
id = ?
""",
(
self.title,
self.description,
self.updated,
self.is_closed,
self.is_merged,
self.id,
),
)
conn.commit()
DBActivity(
user_id=self.user.id,
activity=ActivityType.UPDATE,
created=self.updated,
issue_id=self.id,
).save()

def save(self):
"""Save Issue to database"""
Expand All @@ -164,6 +181,7 @@ def save(self):
self.user = issue.user
self.repository = issue.repository
self.id = issue.id
self.__update(from_db=issue)
return

self.user.save()
Expand Down
1 change: 1 addition & 0 deletions interface/db/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def save(self):
),
)
conn.commit()
self.id = self.load(self.user_id).id
break
except IntegrityError as e:
count += 1
Expand Down
7 changes: 7 additions & 0 deletions interface/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
from urllib.parse import urlparse
from functools import lru_cache

import rfc3339
from flask import g

Expand All @@ -30,6 +32,8 @@
from interface.forges.gitea import Gitea
from interface.forges.payload import RepositoryInfo

REPOSITORY_READ_LIMIT = 50


class Git:
def __init__(self, forge: Forge, admin_user: str, admin_email):
Expand Down Expand Up @@ -114,6 +118,7 @@ def get_forge() -> Git:
return g.git


@lru_cache(maxsize=20)
def get_user(username: str) -> DBUser:
"""
Get user from database.
Expand All @@ -128,6 +133,7 @@ def get_user(username: str) -> DBUser:
return user


@lru_cache(maxsize=20)
def __get_and_store_repo(owner: str, name: str) -> DBRepo:
git = get_forge()
print(f" requesting data for user {owner}")
Expand Down Expand Up @@ -167,6 +173,7 @@ def get_repo(owner: str, name: str) -> DBRepo:
return repo


@lru_cache(maxsize=20)
def __get_and_store_issue(owner: str, repo: str, issue_id: int) -> DBRepo:
git = get_forge()
issue_url = git.forge.get_issue_html_url(owner=owner, repo=repo, issue_id=issue_id)
Expand Down
Loading