Skip to content

Commit 5ee50a8

Browse files
Worker: Migrate to Plan / Tier Tables (#1007)
Co-authored-by: Rola Abuhasna <[email protected]>
1 parent da2ca29 commit 5ee50a8

27 files changed

+518
-21
lines changed

database/models/core.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ class Account(CodecovBaseModel, MixinBaseClassNoExternalID):
6060
name = Column(types.String(100), nullable=False, unique=True)
6161
is_active = Column(types.Boolean, nullable=False, default=True)
6262
plan = Column(
63-
types.String(50), nullable=False, default=PlanName.BASIC_PLAN_NAME.value
63+
types.String(50),
64+
nullable=False,
65+
default=PlanName.BASIC_PLAN_NAME.value, # TODO: UPDATE WITH NEW FREE PLAN NAME
6466
)
6567
plan_seat_count = Column(types.SmallInteger, nullable=False, default=1)
6668
free_seat_count = Column(types.SmallInteger, nullable=False, default=0)

database/tests/factories/core.py

+31-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import factory
66
from factory import Factory
7-
from shared.plan.constants import PlanName
7+
from shared.django_apps.codecov_auth.models import Plan, Tier
8+
from shared.plan.constants import PlanName, TierName
89

910
from database import enums, models
1011
from services.encryption import encryptor
@@ -300,6 +301,35 @@ class Meta:
300301
value = ""
301302

302303

304+
class TierFactory(Factory):
305+
class Meta:
306+
model = Tier
307+
308+
tier_name = TierName.BASIC.value
309+
bundle_analysis = False
310+
test_analytics = False
311+
flaky_test_detection = False
312+
project_coverage = False
313+
private_repo_support = False
314+
315+
316+
class PlanFactory(Factory):
317+
class Meta:
318+
model = Plan
319+
320+
tier = factory.SubFactory(TierFactory)
321+
base_unit_price = 0
322+
benefits = factory.LazyFunction(lambda: ["Benefit 1", "Benefit 2", "Benefit 3"])
323+
billing_rate = None
324+
is_active = True
325+
marketing_name = factory.Faker("catch_phrase")
326+
max_seats = 1
327+
monthly_uploads_limit = None
328+
name = PlanName.BASIC_PLAN_NAME.value
329+
paid_plan = False
330+
stripe_id = None
331+
332+
303333
class UploadErrorFactory(Factory):
304334
class Meta:
305335
model = models.UploadError

requirements.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
https://github.com/codecov/test-results-parser/archive/190bbc8a911099749928e13d5fe57f6027ca1e74.tar.gz#egg=test-results-parser
2-
https://github.com/codecov/shared/archive/ce6e1e9198b046af850606e8fe3633d9a25fea98.tar.gz#egg=shared
2+
https://github.com/codecov/shared/archive/74c0888070699b69a4da73f54be502ad05fde7b6.tar.gz#egg=shared
33
https://github.com/codecov/timestring/archive/d37ceacc5954dff3b5bd2f887936a98a668dda42.tar.gz#egg=timestring
44
asgiref>=3.7.2
55
analytics-python==1.3.0b1

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ sentry-sdk==2.13.0
377377
# shared
378378
setuptools==75.7.0
379379
# via nodeenv
380-
shared @ https://github.com/codecov/shared/archive/ce6e1e9198b046af850606e8fe3633d9a25fea98.tar.gz#egg=shared
380+
shared @ https://github.com/codecov/shared/archive/74c0888070699b69a4da73f54be502ad05fde7b6.tar.gz#egg=shared
381381
# via -r requirements.in
382382
six==1.16.0
383383
# via

services/bundle_analysis/notify/contexts/tests/test_comment_context.py

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
)
2323
from services.repository import EnrichedPull
2424
from services.seats import SeatActivationInfo, ShouldActivateSeat
25+
from tests.helpers import mock_all_plans_and_tiers
2526

2627

2728
class TestBundleAnalysisPRCommentNotificationContext:
@@ -272,7 +273,9 @@ def test_evaluate_should_use_upgrade_message(
272273
builder.evaluate_should_use_upgrade_message()
273274
assert builder._notification_context.should_use_upgrade_comment == expected
274275

276+
@pytest.mark.django_db
275277
def test_build_context(self, dbsession, mocker, mock_storage):
278+
mock_all_plans_and_tiers()
276279
head_commit, base_commit = get_commit_pair(dbsession)
277280
repository = head_commit.repository
278281
head_commit_report, base_commit_report = get_report_pair(
@@ -305,7 +308,9 @@ def test_build_context(self, dbsession, mocker, mock_storage):
305308
== head_commit_report.external_id
306309
)
307310

311+
@pytest.mark.django_db
308312
def test_initialize_from_context(self, dbsession, mocker):
313+
mock_all_plans_and_tiers()
309314
head_commit, base_commit = get_commit_pair(dbsession)
310315
user_yaml = UserYaml.from_dict(PATCH_CENTRIC_DEFAULT_CONFIG)
311316
builder = BundleAnalysisPRCommentContextBuilder().initialize(

services/bundle_analysis/notify/contexts/tests/test_commit_status_context.py

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from services.bundle_analysis.notify.types import NotificationUserConfig
2626
from services.repository import EnrichedPull
2727
from services.seats import SeatActivationInfo, ShouldActivateSeat
28+
from tests.helpers import mock_all_plans_and_tiers
2829

2930

3031
class TestBundleAnalysisPRCommentNotificationContext:
@@ -217,7 +218,9 @@ def test_load_commit_status_level(
217218
context = builder.get_result()
218219
assert context.commit_status_level == expected
219220

221+
@pytest.mark.django_db
220222
def test_build_context(self, dbsession, mocker, mock_storage):
223+
mock_all_plans_and_tiers()
221224
head_commit, base_commit = get_commit_pair(dbsession)
222225
repository = head_commit.repository
223226
head_commit_report, base_commit_report = get_report_pair(
@@ -259,7 +262,9 @@ def test_build_context(self, dbsession, mocker, mock_storage):
259262
assert context.cache_ttl == 600
260263
assert context.commit_status_url is not None
261264

265+
@pytest.mark.django_db
262266
def test_initialize_from_context(self, dbsession, mocker):
267+
mock_all_plans_and_tiers()
263268
head_commit, base_commit = get_commit_pair(dbsession)
264269
user_yaml = UserYaml.from_dict(PATCH_CENTRIC_DEFAULT_CONFIG)
265270
builder = CommitStatusNotificationContextBuilder().initialize(

services/bundle_analysis/notify/messages/tests/test_comment.py

+3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
)
2727
from services.bundle_analysis.notify.types import NotificationUserConfig
2828
from services.notification.notifiers.base import NotificationResult
29+
from tests.helpers import mock_all_plans_and_tiers
2930

3031

3132
class TestCommentMesage:
33+
@pytest.mark.django_db
3234
def test_build_message_from_samples(self, dbsession, mocker, mock_storage):
35+
mock_all_plans_and_tiers()
3336
head_commit, base_commit = get_commit_pair(dbsession)
3437
repository = head_commit.repository
3538
head_commit_report, base_commit_report = get_report_pair(

services/bundle_analysis/notify/messages/tests/test_commit_status.py

+11
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
)
2525
from services.notification.notifiers.base import NotificationResult
2626
from services.seats import SeatActivationInfo, ShouldActivateSeat
27+
from tests.helpers import mock_all_plans_and_tiers
2728

2829

2930
class FakeRedis(object):
@@ -55,6 +56,10 @@ def mock_cache(mocker):
5556

5657

5758
class TestCommitStatusMessage:
59+
@pytest.fixture(autouse=True)
60+
def setup(self):
61+
mock_all_plans_and_tiers()
62+
5863
@pytest.mark.parametrize(
5964
"user_config, expected",
6065
[
@@ -81,6 +86,7 @@ class TestCommitStatusMessage:
8186
),
8287
],
8388
)
89+
@pytest.mark.django_db
8490
def test_build_message_from_samples_negative_changes(
8591
self, user_config, expected, dbsession, mocker, mock_storage
8692
):
@@ -133,6 +139,7 @@ def test_build_message_from_samples_negative_changes(
133139
),
134140
],
135141
)
142+
@pytest.mark.django_db
136143
def test_build_message_from_samples(
137144
self, user_config, expected, dbsession, mocker, mock_storage
138145
):
@@ -159,6 +166,7 @@ def test_build_message_from_samples(
159166
message = CommitStatusMessageStrategy().build_message(context)
160167
assert message == expected
161168

169+
@pytest.mark.django_db
162170
def _setup_send_message_tests(
163171
self, dbsession, mocker, torngit_ghapp_data, mock_storage
164172
):
@@ -207,6 +215,7 @@ def _setup_send_message_tests(
207215
"Passed with Warnings - Bundle change: 95.64% (Threshold: 5.0%)",
208216
)
209217

218+
@pytest.mark.django_db
210219
@pytest.mark.parametrize(
211220
"torngit_ghapp_data",
212221
[
@@ -246,6 +255,7 @@ def test_send_message_success(
246255
# Side effect of sending message is updating the cache
247256
assert mock_cache.get_backend().get(strategy._cache_key(context)) == message
248257

258+
@pytest.mark.django_db
249259
def test_send_message_fail(self, dbsession, mocker, mock_storage):
250260
fake_repo_provider, context, message = self._setup_send_message_tests(
251261
dbsession, mocker, None, mock_storage
@@ -259,6 +269,7 @@ def test_send_message_fail(self, dbsession, mocker, mock_storage):
259269
explanation="TorngitClientError",
260270
)
261271

272+
@pytest.mark.django_db
262273
def test_skip_payload_unchanged(self, dbsession, mocker, mock_storage, mock_cache):
263274
fake_repo_provider, context, message = self._setup_send_message_tests(
264275
dbsession, mocker, None, mock_storage

services/bundle_analysis/notify/tests/test_notify_service.py

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
)
3030
from services.bundle_analysis.notify.types import NotificationType
3131
from services.notification.notifiers.base import NotificationResult
32+
from tests.helpers import mock_all_plans_and_tiers
3233

3334

3435
def override_comment_builder_and_message_strategy(mocker):
@@ -114,7 +115,9 @@ def test_build_base_context(self, mocker, dbsession, mock_storage):
114115
assert base_context.commit_report == head_commit_report
115116
assert base_context.bundle_analysis_report.session_count() == 19
116117

118+
@pytest.mark.django_db
117119
def test_create_context_success(self, dbsession, mock_storage, mocker):
120+
mock_all_plans_and_tiers()
118121
current_yaml = UserYaml.from_dict(PATCH_CENTRIC_DEFAULT_CONFIG)
119122
head_commit, base_commit = get_commit_pair(dbsession)
120123
head_commit_report, base_commit_report = get_report_pair(

services/bundle_analysis/tests/test_bundle_analysis.py

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
)
3131
from services.repository import EnrichedPull
3232
from services.urls import get_bundle_analysis_pull_url
33+
from tests.helpers import mock_all_plans_and_tiers
3334

3435

3536
class MockBundleReport:
@@ -690,6 +691,7 @@ async def test_bundle_analysis_save_measurements_error(dbsession, mocker, mock_s
690691
),
691692
],
692693
)
694+
@pytest.mark.django_db
693695
def test_bundle_analysis_notify_bundle_summary(
694696
bundle_changes: list[BundleChange],
695697
percent_change: float,
@@ -700,6 +702,7 @@ def test_bundle_analysis_notify_bundle_summary(
700702
mock_storage,
701703
mock_repo_provider,
702704
):
705+
mock_all_plans_and_tiers()
703706
hook_mock_repo_provider(mocker, mock_repo_provider)
704707
base_commit = CommitFactory()
705708
dbsession.add(base_commit)
@@ -1483,6 +1486,7 @@ def contributing_modules(self, pr_changed_files):
14831486
),
14841487
],
14851488
)
1489+
@pytest.mark.django_db
14861490
def test_bundle_analysis_notify_individual_bundle_data(
14871491
bundle_changes: list[BundleChange],
14881492
route_changes: Dict[str, List[RouteChange]],
@@ -1493,6 +1497,7 @@ def test_bundle_analysis_notify_individual_bundle_data(
14931497
mock_storage,
14941498
mock_repo_provider,
14951499
):
1500+
mock_all_plans_and_tiers()
14961501
percent_change = 5.56
14971502
user_config = {
14981503
**PATCH_CENTRIC_DEFAULT_CONFIG,

services/notification/__init__.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
from celery.exceptions import CeleryError, SoftTimeLimitExceeded
1212
from shared.config import get_config
13+
from shared.django_apps.codecov_auth.models import Plan
1314
from shared.helpers.yaml import default_if_true
14-
from shared.plan.constants import TEAM_PLAN_REPRESENTATIONS
15+
from shared.plan.constants import TierName
1516
from shared.torngit.base import TorngitBaseAdapter
1617
from shared.yaml import UserYaml
1718

@@ -68,9 +69,13 @@ def __init__(
6869
def _should_use_status_notifier(self, status_type: StatusType) -> bool:
6970
owner: Owner = self.repository.owner
7071

71-
if owner.plan in TEAM_PLAN_REPRESENTATIONS:
72-
if status_type != StatusType.PATCH.value:
73-
return False
72+
plan = Plan.objects.select_related("tier").get(name=owner.plan)
73+
74+
if (
75+
plan.tier.tier_name == TierName.TEAM.value
76+
and status_type != StatusType.PATCH.value
77+
):
78+
return False
7479

7580
return True
7681

@@ -83,9 +88,13 @@ def _should_use_checks_notifier(self, status_type: StatusType) -> bool:
8388
if owner.service not in ["github", "github_enterprise"]:
8489
return False
8590

86-
if owner.plan in TEAM_PLAN_REPRESENTATIONS:
87-
if status_type != StatusType.PATCH.value:
88-
return False
91+
plan = Plan.objects.select_related("tier").get(name=owner.plan)
92+
93+
if (
94+
plan.tier.tier_name == TierName.TEAM.value
95+
and status_type != StatusType.PATCH.value
96+
):
97+
return False
8998

9099
app_installation_filter = filter(
91100
lambda obj: (

services/notification/notifiers/tests/integration/test_comment.py

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from services.comparison.types import Comparison, EnrichedPull, FullCommit
99
from services.decoration import Decoration
1010
from services.notification.notifiers.comment import CommentNotifier
11+
from tests.helpers import mock_all_plans_and_tiers
1112

1213

1314
@pytest.fixture
@@ -335,6 +336,11 @@ def sample_comparison_for_limited_upload(
335336

336337
@pytest.mark.usefixtures("is_not_first_pull")
337338
class TestCommentNotifierIntegration(object):
339+
@pytest.fixture(autouse=True)
340+
def setup(self):
341+
mock_all_plans_and_tiers()
342+
343+
@pytest.mark.django_db
338344
def test_notify(self, sample_comparison, codecov_vcr, mock_configuration):
339345
sample_comparison.context = ComparisonContext(
340346
all_tests_passed=True, test_results_error=None
@@ -407,6 +413,7 @@ def test_notify(self, sample_comparison, codecov_vcr, mock_configuration):
407413
assert result.data_sent == {"commentid": None, "message": message, "pullid": 9}
408414
assert result.data_received == {"id": 1699669247}
409415

416+
@pytest.mark.django_db
410417
def test_notify_test_results_error(
411418
self, sample_comparison, codecov_vcr, mock_configuration
412419
):
@@ -482,6 +489,7 @@ def test_notify_test_results_error(
482489
assert result.data_sent == {"commentid": None, "message": message, "pullid": 9}
483490
assert result.data_received == {"id": 1699669247}
484491

492+
@pytest.mark.django_db
485493
def test_notify_upgrade(
486494
self, dbsession, sample_comparison_for_upgrade, codecov_vcr, mock_configuration
487495
):
@@ -516,6 +524,7 @@ def test_notify_upgrade(
516524
}
517525
assert result.data_received == {"id": 1361234119}
518526

527+
@pytest.mark.django_db
519528
def test_notify_upload_limited(
520529
self,
521530
dbsession,
@@ -559,6 +568,7 @@ def test_notify_upload_limited(
559568
}
560569
assert result.data_received == {"id": 1111984446}
561570

571+
@pytest.mark.django_db
562572
def test_notify_gitlab(
563573
self, sample_comparison_gitlab, codecov_vcr, mock_configuration
564574
):
@@ -625,6 +635,7 @@ def test_notify_gitlab(
625635
assert result.data_sent == {"commentid": None, "message": message, "pullid": 5}
626636
assert result.data_received == {"id": 1457135397}
627637

638+
@pytest.mark.django_db
628639
def test_notify_new_layout(
629640
self, sample_comparison, codecov_vcr, mock_configuration
630641
):
@@ -695,6 +706,7 @@ def test_notify_new_layout(
695706
assert result.data_sent == {"commentid": None, "message": message, "pullid": 9}
696707
assert result.data_received == {"id": 1699669290}
697708

709+
@pytest.mark.django_db
698710
def test_notify_with_components(
699711
self, sample_comparison, codecov_vcr, mock_configuration
700712
):

0 commit comments

Comments
 (0)