Skip to content

Commit

Permalink
feat: Add different comment message for 1st time pulls
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-codecov committed Sep 8, 2023
1 parent c828650 commit bca9aee
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 4 deletions.
12 changes: 11 additions & 1 deletion database/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import string
import uuid
from datetime import datetime
from functools import cached_property

from sqlalchemy import Column, ForeignKey, Index, UniqueConstraint, types
from sqlalchemy.dialects import postgresql
Expand Down Expand Up @@ -297,7 +298,9 @@ class Pull(CodecovBaseModel):
behind_by_commit = Column(types.Text)

author = relationship(Owner)
repository = relationship(Repository, backref=backref("pulls", cascade="delete"))
repository = relationship(
Repository, backref=backref("pulls", cascade="delete", lazy="dynamic")
)

__table_args__ = (Index("pulls_repoid_pullid", "repoid", "pullid", unique=True),)

Expand Down Expand Up @@ -355,6 +358,13 @@ def should_write_to_storage(self) -> bool:
repoid=self.repository.repoid,
)

@cached_property
def is_first_pull(self):
first_pull = (
self.repository.pulls.with_entities(Pull.id_).order_by(Pull.id_).first()
)
return first_pull.id_ == self.id_

_flare = Column("flare", postgresql.JSON)
_flare_storage_path = Column("flare_storage_path", types.Text, nullable=True)
flare = ArchiveField(
Expand Down
12 changes: 12 additions & 0 deletions services/notification/notifiers/comment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
)

from database.enums import Notification
from database.models import Pull
from helpers.metrics import metrics
from services.comparison.types import Comparison
from services.license import requires_license
Expand Down Expand Up @@ -358,11 +359,22 @@ async def build_message(self, comparison: Comparison) -> List[str]:
return self._create_empty_upload_message()
if self.should_use_upload_limit_decoration():
return self._create_reached_upload_limit_message(comparison)
if comparison.pull.is_first_pull:
return self._create_welcome_message()
pull_dict = comparison.enriched_pull.provider_pull
return await self.create_message(
comparison, pull_dict, self.notifier_yaml_settings
)

def _create_welcome_message(self):
return [
"## Welcome to [Codecov](https://codecov.io) :tada:",
"",
"Once merged to your default branch, Codecov will compare your coverage reports and display the results in this comment.",
"",
"Thanks for integrating Codecov - We've got you covered :open_umbrella:",
]

def _create_empty_upload_message(self):
if self.is_passing_empty_upload():
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from unittest.mock import patch
from unittest.mock import PropertyMock, patch

import pytest
from shared.reports.readonly import ReadOnlyReport
Expand All @@ -10,6 +10,15 @@
from services.notification.notifiers.comment import CommentNotifier


@pytest.fixture
def is_not_first_pull(mocker):
mocker.patch(
"database.models.core.Pull.is_first_pull",
return_value=False,
new_callable=PropertyMock,
)


@pytest.fixture
def codecove2e_comparison(dbsession, request, sample_report, small_report):
repository = RepositoryFactory.create(
Expand Down Expand Up @@ -319,6 +328,7 @@ def sample_comparison_for_limited_upload(
)


@pytest.mark.usefixtures("is_not_first_pull")
class TestCommentNotifierIntegration(object):
@pytest.mark.asyncio
async def test_notify(self, sample_comparison, codecov_vcr, mock_configuration):
Expand Down
39 changes: 38 additions & 1 deletion services/notification/notifiers/tests/unit/test_comment.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from decimal import Decimal
from unittest.mock import patch
from unittest.mock import PropertyMock, patch

import pytest
from shared.reports.readonly import ReadOnlyReport
Expand Down Expand Up @@ -36,6 +36,15 @@
from services.yaml.reader import get_components_from_yaml


@pytest.fixture
def is_not_first_pull(mocker):
mocker.patch(
"database.models.core.Pull.is_first_pull",
return_value=False,
new_callable=PropertyMock,
)


@pytest.fixture
def sample_comparison_bunch_empty_flags(request, dbsession, mocker):
"""
Expand Down Expand Up @@ -500,6 +509,7 @@ async def test__possibly_write_gh_app_login_announcement_enterprise(
assert mock_write.call_count == 0


@pytest.mark.usefixtures("is_not_first_pull")
class TestCommentNotifier(object):
@pytest.mark.asyncio
async def test_is_enabled_settings_individual_settings_false(self, dbsession):
Expand Down Expand Up @@ -3837,6 +3847,7 @@ async def test_footer_section_writer_with_project_cov_hidden(self, mocker):
assert res == []


@pytest.mark.usefixtures("is_not_first_pull")
class TestCommentNotifierInNewLayout(object):
@pytest.mark.asyncio
async def test_create_message_files_section_with_critical_files_new_layout(
Expand Down Expand Up @@ -4689,3 +4700,29 @@ async def test_write_message_component_section_no_base(
"| [py_files](urlurl/components?src=pr&el=component) | `50.00% <0.00%> (?)` | |",
]
assert message == expected


class TestCommentNotifierWelcome:
@pytest.mark.asyncio
async def test_build_message(
self, dbsession, mock_configuration, mock_repo_provider, sample_comparison
):
mock_configuration.params["setup"]["codecov_dashboard_url"] = "test.example.br"
notifier = CommentNotifier(
repository=sample_comparison.head.commit.repository,
title="title",
notifier_yaml_settings={"layout": "reach, diff, flags, files, footer"},
notifier_site_settings=True,
current_yaml={},
)
result = await notifier.build_message(sample_comparison)
expected_result = [
"## Welcome to [Codecov](https://codecov.io) :tada:",
"",
"Once merged to your default branch, Codecov will compare your coverage reports and display the results in this comment.",
"",
"Thanks for integrating Codecov - We've got you covered :open_umbrella:",
]
for exp, res in zip(expected_result, result):
assert exp == res
assert result == expected_result
4 changes: 3 additions & 1 deletion tasks/tests/integration/test_notify_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from database.models import Pull
from database.tests.factories import CommitFactory, RepositoryFactory
from database.tests.factories import CommitFactory, PullFactory, RepositoryFactory
from services.archive import ArchiveService
from services.notification.notifiers.base import NotificationResult
from tasks.notify import NotifyTask
Expand Down Expand Up @@ -807,6 +807,8 @@ async def test_simple_call_status_and_notifiers(
repository=repository,
author=repository.owner,
)
# create another pull so that we don't trigger the 1st time comment message
dbsession.add(PullFactory.create(repository=repository, pullid=8))
commit = CommitFactory.create(
message="",
pullid=9,
Expand Down

0 comments on commit bca9aee

Please sign in to comment.