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

feat: update pull and branch head in commit update #915

Merged
merged 1 commit into from
Dec 2, 2024
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
77 changes: 76 additions & 1 deletion tasks/commit_update.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import datetime as dt
import logging

from shared.celery_config import commit_update_task_name
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 @@ -44,6 +45,80 @@
commit, repository_service
)

if isinstance(commit.timestamp, str):
commit.timestamp = dt.datetime.fromisoformat(commit.timestamp).replace(
tzinfo=None
)

if commit.pullid is not None:
# 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 62 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L62

Added line #L62 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 68 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L68

Added line #L68 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

db_session.flush()

if commit.branch is not None:
# 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 93 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L93

Added line #L93 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 99 in tasks/commit_update.py

View check run for this annotation

Codecov Notifications / codecov/patch

tasks/commit_update.py#L99

Added line #L99 was not covered by tests
else:
if commit.author_id is not None:
if branch.authors is None:
branch.authors = [commit.author_id]
elif commit.author_id not in branch.authors:
branch.authors.append(commit.author_id)

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

db_session.flush()

except RepositoryWithoutValidBotError:
log.warning(
"Unable to reach git provider because repo doesn't have a valid bot",
Expand Down
72 changes: 69 additions & 3 deletions tasks/tests/unit/test_commit_update.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import datetime as dt

import pytest
from shared.torngit.exceptions import (
TorngitClientError,
TorngitObjectNotFoundError,
TorngitRepoNotFoundError,
)

from database.tests.factories import CommitFactory
from database.models import Branch
from database.tests.factories import BranchFactory, CommitFactory, PullFactory
from helpers.exceptions import RepositoryWithoutValidBotError
from tasks.commit_update import CommitUpdateTask

Expand Down Expand Up @@ -168,6 +171,9 @@ def test_update_commit_not_found(
assert commit.message == ""
assert commit.parent_commit_id is None

@pytest.mark.parametrize("branch_authors", [None, False, True])
@pytest.mark.parametrize("prev_head", ["old_head", "new_head"])
@pytest.mark.parametrize("deleted", [False, True])
def test_update_commit_already_populated(
self,
mocker,
Expand All @@ -176,6 +182,9 @@ def test_update_commit_already_populated(
mock_redis,
mock_repo_provider,
mock_storage,
branch_authors,
prev_head,
deleted,
):
commit = CommitFactory.create(
message="commit_msg",
Expand All @@ -184,14 +193,71 @@ 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,
timestamp=dt.datetime.fromisoformat("2019-02-01T17:59:47"),
)
dbsession.add(commit)
dbsession.flush()

commit.branch = "featureA"
commit.pullid = 1
dbsession.flush()

old_head = CommitFactory.create(
message="",
commitid="b2d3e3c30547a000f026daa47610bb3f7b63aece",
repository=commit.repository,
timestamp=dt.datetime.fromisoformat("2019-01-01T17:59:47"),
)
dbsession.add(old_head)
dbsession.flush()

old_head.branch = "featureA"
old_head.pullid = 1
dbsession.flush()

pull = PullFactory(
repository=commit.repository, pullid=1, head=old_head.commitid
)
dbsession.add(pull)
dbsession.flush()

if branch_authors is False:
branch_authors = []
elif branch_authors is True:
branch_authors = [commit.author_id]

if prev_head == "old_head":
prev_head = old_head
elif prev_head == "new_head":
prev_head = commit

if deleted:
prev_head.deleted = True
dbsession.flush()

b = dbsession.query(Branch).first()
dbsession.delete(b)
dbsession.flush()

branch = BranchFactory(
repository=commit.repository,
branch="featureA",
head=prev_head.commitid,
authors=branch_authors,
)
dbsession.add(branch)
dbsession.flush()

result = CommitUpdateTask().run_impl(dbsession, commit.repoid, commit.commitid)
expected_result = {"was_updated": False}
assert expected_result == result
assert commit.message == "commit_msg"
assert commit.parent_commit_id is None
assert commit.timestamp == dt.datetime.fromisoformat("2019-02-01T17:59:47")
assert commit.branch == "featureA"

dbsession.refresh(pull)
assert pull.head == commit.commitid

dbsession.refresh(branch)
assert branch.head == commit.commitid
Loading