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

fix: change get_or_create_author() to upsert_author(). track username/name/email changes #1058

Merged
merged 5 commits into from
Feb 3, 2025
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
66 changes: 41 additions & 25 deletions services/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
),
)
else:
commit_author = get_or_create_author(
commit_author = upsert_author(
db_session,
commit.repository.service,
author_info["id"],
Expand Down Expand Up @@ -281,37 +281,53 @@
db_session.commit()


def get_or_create_author(
def upsert_author(
db_session, service, service_id, username, email=None, name=None
) -> Owner:
query = db_session.query(Owner).filter(
Owner.service == service, Owner.service_id == str(service_id)
)
author = query.first()

if author:
return author
needs_update = False
db_session.begin(nested=True)
if author.username != username and username is not None:
author.username = username
needs_update = True
if author.name != name and name is not None:
author.name = name
matt-codecov marked this conversation as resolved.
Show resolved Hide resolved
needs_update = True
if author.email != email and email is not None:
author.email = email
needs_update = True

if needs_update:
db_session.commit()
else:
db_session.rollback()
matt-codecov marked this conversation as resolved.
Show resolved Hide resolved
else:
db_session.begin(nested=True)
try:
author = Owner(
service=service,
service_id=str(service_id),
username=username,
name=name,
email=email,
createstamp=datetime.now(),
)
db_session.add(author)
db_session.commit()
except IntegrityError:
log.warning(

Check warning on line 323 in services/repository.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/repository.py#L322-L323

Added lines #L322 - L323 were not covered by tests
"IntegrityError in upsert_author",
extra=dict(service=service, service_id=service_id, username=username),
)
db_session.rollback()
author = query.one()

Check warning on line 328 in services/repository.py

View check run for this annotation

Codecov Notifications / codecov/patch

services/repository.py#L327-L328

Added lines #L327 - L328 were not covered by tests

db_session.begin(nested=True)
try:
author = Owner(
service=service,
service_id=str(service_id),
username=username,
name=name,
email=email,
createstamp=datetime.now(),
)
db_session.add(author)
db_session.commit()
return author
except IntegrityError:
log.warning(
"IntegrityError in get_or_create_author",
extra=dict(service=service, service_id=service_id, username=username),
)
db_session.rollback()
author = query.one()
return author
return author


WEBHOOK_EVENTS = {
Expand Down Expand Up @@ -541,7 +557,7 @@
pull.compared_to = compared_to

if pull is not None and not pull.author:
pr_author = get_or_create_author(
pr_author = upsert_author(
db_session,
repository_service.service,
pull_information["author"]["id"],
Expand Down
76 changes: 58 additions & 18 deletions services/tests/test_repository_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
fetch_and_update_pull_request_information_from_commit,
fetch_appropriate_parent_for_commit,
fetch_commit_yaml_and_possibly_store,
get_or_create_author,
get_repo_provider_service,
get_repo_provider_service_by_id,
update_commit_from_provider_info,
upsert_author,
)
from tasks.notify import get_repo_provider_service_for_specific_commit

Expand Down Expand Up @@ -721,13 +721,13 @@ async def test_fetch_appropriate_parent_for_commit_multiple_parents(


@freeze_time("2024-03-28T00:00:00")
def test_get_or_create_author_doesnt_exist(dbsession):
def test_upsert_author_doesnt_exist(dbsession):
service = "github"
author_id = "123"
username = "username"
email = "email"
name = "name"
author = get_or_create_author(dbsession, service, author_id, username, email, name)
author = upsert_author(dbsession, service, author_id, username, email, name)
dbsession.flush()
assert author.free == 0
assert author is not None
Expand All @@ -746,31 +746,71 @@ def test_get_or_create_author_doesnt_exist(dbsession):
assert author.createstamp.isoformat() == "2024-03-28T00:00:00"


def test_get_or_create_author_already_exists(dbsession):
def test_upsert_author_already_exists(dbsession):
username = "username"
email = "[email protected]"
service = "bitbucket"
service_id = "975"
owner = OwnerFactory.create(
service="bitbucket",
service_id="975",
email="different_email@email.com",
username="whoknew",
service=service,
service_id=service_id,
email=email,
username=username,
yaml=dict(a=["12", "3"]),
)
dbsession.add(owner)
dbsession.flush()
service = "bitbucket"
author_id = "975"
username = "username"
email = "email"
name = "name"
author = get_or_create_author(dbsession, service, author_id, username, email, name)

author = upsert_author(dbsession, service, service_id, username, None, None)
dbsession.flush()
assert author.ownerid == owner.ownerid
assert author.free == 0
assert author is not None
assert author.service == "bitbucket"
assert author.service_id == "975"
assert author.service == service
assert author.service_id == service_id
assert author.name == owner.name
assert author.email == "[email protected]"
assert author.username == "whoknew"
assert author.email == email
assert author.username == username
assert author.plan_activated_users == []
assert author.admins == []
assert author.permission == []
assert author.integration_id is None
assert author.yaml == {"a": ["12", "3"]}
assert author.oauth_token == owner.oauth_token
assert author.bot_id == owner.bot_id


def test_upsert_author_needs_update(dbsession):
username = "username"
email = "[email protected]"
service = "bitbucket"
service_id = "975"
owner = OwnerFactory.create(
service=service,
service_id=service_id,
email=email,
username=username,
yaml=dict(a=["12", "3"]),
)
dbsession.add(owner)
dbsession.flush()

new_name = "Newt Namenheim"
new_username = "new_username"
new_email = "[email protected]"
author = upsert_author(
dbsession, service, service_id, new_username, new_email, new_name
)
dbsession.flush()

assert author is not None
assert author.ownerid == owner.ownerid
assert author.free == 0
assert author.service == service
assert author.service_id == service_id
assert author.name == new_name
assert author.email == new_email
assert author.username == new_username
assert author.plan_activated_users == []
assert author.admins == []
assert author.permission == []
Expand Down
Loading