Skip to content

Commit

Permalink
fix: upsert branch and pull when updating commit
Browse files Browse the repository at this point in the history
when we update a commit, we want to ensure the commit's corresponding
pull and branch are also upserted to reflect whatever changes were made
to the commit
  • Loading branch information
joseph-sentry committed Nov 19, 2024
1 parent 3676a01 commit 2dcfba2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
65 changes: 64 additions & 1 deletion tasks/commit_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from shared.torngit.exceptions import TorngitClientError, TorngitRepoNotFoundError

from app import celery_app
from database.models import Commit
from database.models import Branch, Commit, Pull
from helpers.exceptions import RepositoryWithoutValidBotError
from helpers.github_installation import get_installation_name_for_owner_for_task
from services.repository import (
Expand Down Expand Up @@ -43,6 +43,69 @@ def run_impl(
was_updated = possibly_update_commit_from_provider_info(
commit, repository_service
)

# upsert pull
pull = (
db_session.query(Pull)
.filter(Pull.repoid == repoid, Pull.pullid == commit.pullid)
.first()
)

if pull is None:
pull = Pull(

Check warning on line 55 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L55

Added line #L55 was not covered by tests
repoid=repoid,
pullid=commit.pullid,
author_id=commit.author_id,
head=commit.commitid,
)
db_session.add(pull)

Check warning on line 61 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L61

Added line #L61 was not covered by tests
else:
previous_pull_head = (
db_session.query(Commit)
.filter(Commit.repoid == repoid, Commit.commitid == pull.head)
.first()
)
if (
previous_pull_head is None
or previous_pull_head.deleted == True
or previous_pull_head.timestamp < commit.timestamp
):
pull.head = commit.commitid

Check warning on line 73 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L73

Added line #L73 was not covered by tests

# upsert branch
branch = (
db_session.query(Branch)
.filter(Branch.repoid == repoid, Branch.branch == commit.branch)
.first()
)

if branch is None:
branch = Branch(

Check warning on line 83 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L83

Added line #L83 was not covered by tests
repoid=repoid,
branch=commit.branch,
head=commit.commitid,
authors=[commit.author_id],
)
db_session.add(branch)

Check warning on line 89 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L89

Added line #L89 was not covered by tests
else:
if commit.author_id not in branch.authors:
branch.authors.append(commit.author_id)

Check warning on line 92 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L92

Added line #L92 was not covered by tests

previous_branch_head = (
db_session.query(Commit)
.filter(Commit.repoid == repoid, Commit.commitid == branch.head)
.first()
)

if (
previous_branch_head is None
or previous_branch_head.deleted == True
or previous_branch_head.timestamp < commit.timestamp
):
branch.head = commit.commitid

Check warning on line 105 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L105

Added line #L105 was not covered by tests

db_session.flush()

except RepositoryWithoutValidBotError:
log.warning(
"Unable to reach git provider because repo doesn't have a valid bot",
Expand Down
19 changes: 19 additions & 0 deletions tasks/tests/unit/test_commit_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
TorngitRepoNotFoundError,
)

from database.models import Branch, Pull
from database.tests.factories import CommitFactory
from helpers.exceptions import RepositoryWithoutValidBotError
from tasks.commit_update import CommitUpdateTask
Expand Down Expand Up @@ -184,6 +185,8 @@ def test_update_commit_already_populated(
repository__owner__username="test-acc9",
repository__yaml={"codecov": {"max_report_age": "764y ago"}},
repository__name="test_example",
branch="main",
pullid=1,
)
dbsession.add(commit)
dbsession.flush()
Expand All @@ -193,3 +196,19 @@ def test_update_commit_already_populated(
assert expected_result == result
assert commit.message == "commit_msg"
assert commit.parent_commit_id is None

branch = (
dbsession.query(Branch)
.filter(Branch.repoid == commit.repoid, Branch.branch == commit.branch)
.first()
)
assert branch.head == commit.commitid
assert branch.authors == [commit.author_id]

pull = (
dbsession.query(Pull)
.filter(Pull.repoid == commit.repoid, Pull.pullid == commit.pullid)
.first()
)
assert pull.head == commit.commitid
assert pull.author_id == commit.author_id

0 comments on commit 2dcfba2

Please sign in to comment.