|
33 | 33 | OrganizationMember,
|
34 | 34 | OrganizationStatus,
|
35 | 35 | )
|
| 36 | +from sentry.notifications.helpers import should_use_notifications_v2 |
| 37 | +from sentry.notifications.notificationcontroller import NotificationController |
| 38 | +from sentry.notifications.types import NotificationSettingEnum |
| 39 | +from sentry.services.hybrid_cloud.user.model import RpcUser |
36 | 40 | from sentry.services.hybrid_cloud.user_option import user_option_service
|
37 | 41 | from sentry.silo import SiloMode
|
38 | 42 | from sentry.snuba.dataset import Dataset
|
@@ -225,10 +229,16 @@ def prepare_organization_report(
|
225 | 229 | )
|
226 | 230 | return
|
227 | 231 |
|
| 232 | + use_notifications_v2 = should_use_notifications_v2(ctx.organization) |
| 233 | + |
228 | 234 | # Finally, deliver the reports
|
229 | 235 | with sentry_sdk.start_span(op="weekly_reports.deliver_reports"):
|
230 | 236 | deliver_reports(
|
231 |
| - ctx, dry_run=dry_run, target_user=target_user, email_override=email_override |
| 237 | + ctx, |
| 238 | + dry_run=dry_run, |
| 239 | + target_user=target_user, |
| 240 | + email_override=email_override, |
| 241 | + use_notifications_v2=use_notifications_v2, |
232 | 242 | )
|
233 | 243 |
|
234 | 244 |
|
@@ -637,10 +647,33 @@ def fetch_key_performance_issue_groups(ctx):
|
637 | 647 | # For all users in the organization, we generate the template context for the user, and send the email.
|
638 | 648 |
|
639 | 649 |
|
640 |
| -def deliver_reports(ctx, dry_run=False, target_user=None, email_override=None): |
| 650 | +def deliver_reports( |
| 651 | + ctx, dry_run=False, target_user=None, email_override=None, use_notifications_v2=False |
| 652 | +): |
641 | 653 | # Specify a sentry user to send this email.
|
642 | 654 | if email_override:
|
643 | 655 | send_email(ctx, target_user, dry_run=dry_run, email_override=email_override)
|
| 656 | + elif use_notifications_v2: |
| 657 | + user_list = list( |
| 658 | + OrganizationMember.objects.filter( |
| 659 | + user_is_active=True, |
| 660 | + organization_id=ctx.organization.id, |
| 661 | + ) |
| 662 | + .filter(flags=F("flags").bitand(~OrganizationMember.flags["member-limit:restricted"])) |
| 663 | + .values_list("user_id", flat=True) |
| 664 | + ) |
| 665 | + |
| 666 | + users = [RpcUser(id=user_id) for user_id in user_list] |
| 667 | + controller = NotificationController( |
| 668 | + recipients=users, |
| 669 | + organization_id=ctx.organization.id, |
| 670 | + type=NotificationSettingEnum.REPORTS, |
| 671 | + ) |
| 672 | + |
| 673 | + user_ids = controller.get_users_for_weekly_reports() |
| 674 | + for user_id in user_ids: |
| 675 | + send_email(ctx, user_id, dry_run=dry_run) |
| 676 | + |
644 | 677 | else:
|
645 | 678 | # We save the subscription status of the user in a field in UserOptions.
|
646 | 679 | user_list = list(
|
|
0 commit comments