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(team-workflow): Add subscription for team in GroupSubscription #55825

Merged
merged 10 commits into from
Sep 12, 2023
2 changes: 1 addition & 1 deletion src/sentry/api/endpoints/group_notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def post(self, request: Request, group) -> Response:
)

GroupSubscription.objects.subscribe(
group=group, user=request.user, reason=GroupSubscriptionReason.comment
group=group, subscriber=request.user, reason=GroupSubscriptionReason.comment
)

mentioned_users = extract_user_ids_from_mentions(group.organization.id, mentions)
Expand Down
4 changes: 2 additions & 2 deletions src/sentry/api/helpers/group_index/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def self_subscribe_and_assign_issue(
# representation of current user
if acting_user:
GroupSubscription.objects.subscribe(
user=acting_user, group=group, reason=GroupSubscriptionReason.status_change
subscriber=acting_user, group=group, reason=GroupSubscriptionReason.status_change
)

if self_assign_issue == "1" and not group.assignee_set.exists():
Expand Down Expand Up @@ -736,7 +736,7 @@ def handle_is_bookmarked(
user_id=acting_user.id if acting_user else None,
)
GroupSubscription.objects.subscribe(
user=acting_user, group=group, reason=GroupSubscriptionReason.bookmark
subscriber=acting_user, group=group, reason=GroupSubscriptionReason.bookmark
)
elif is_bookmarked is False:
GroupBookmark.objects.filter(
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/issues/status_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def handle_status_update(
if not is_bulk:
if acting_user:
GroupSubscription.objects.subscribe(
user=acting_user,
subscriber=acting_user,
group=group,
reason=GroupSubscriptionReason.status_change,
)
Expand Down
39 changes: 27 additions & 12 deletions src/sentry/models/groupsubscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,33 @@ class GroupSubscriptionManager(BaseManager):
def subscribe(
self,
group: Group,
user: User | RpcUser,
subscriber: User | RpcUser | Team,
reason: int = GroupSubscriptionReason.unknown,
) -> bool:
"""
Subscribe a user to an issue, but only if the user has not explicitly
Subscribe a user or team to an issue, but only if that user or team has not explicitly
unsubscribed.
"""
from sentry.models import Team, User

try:
with transaction.atomic(router.db_for_write(GroupSubscription)):
self.create(
user_id=user.id,
group=group,
project=group.project,
is_active=True,
reason=reason,
)
if isinstance(subscriber, User) or isinstance(subscriber, RpcUser):
self.create(
user_id=subscriber.id,
group=group,
project=group.project,
is_active=True,
reason=reason,
)
elif isinstance(subscriber, Team):
isabellaenriquez marked this conversation as resolved.
Show resolved Hide resolved
self.create(
team=subscriber,
group=group,
project=group.project,
is_active=True,
reason=reason,
)
except IntegrityError:
pass
return True
Expand All @@ -60,14 +71,18 @@ def subscribe_actor(
actor: Union[Team, User, RpcUser],
reason: int = GroupSubscriptionReason.unknown,
) -> Optional[bool]:
from sentry import features
from sentry.models import Team, User

if isinstance(actor, RpcUser) or isinstance(actor, User):
return self.subscribe(group, actor, reason)
if isinstance(actor, Team):
# subscribe the members of the team
team_users_ids = list(actor.member_set.values_list("user_id", flat=True))
return self.bulk_subscribe(group, team_users_ids, reason)
if features.has("organizations:team-workflow-notifications", group.organization, actor):
return self.subscribe(group, actor, reason)
else:
# subscribe the members of the team
team_users_ids = list(actor.member_set.values_list("user_id", flat=True))
return self.bulk_subscribe(group, team_users_ids, reason)

raise NotImplementedError("Unknown actor type: %r" % type(actor))

Expand Down
2 changes: 1 addition & 1 deletion src/sentry/receivers/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def resolved_in_commit(instance, created, **kwargs):
# subscribe every user
for user in user_list:
GroupSubscription.objects.subscribe(
user=user,
subscriber=user,
group=group,
reason=GroupSubscriptionReason.status_change,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/sentry/models/test_groupsubscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def test_simple(self):
group = self.create_group()
user = self.create_user()
isabellaenriquez marked this conversation as resolved.
Show resolved Hide resolved

GroupSubscription.objects.subscribe(group=group, user=user)
GroupSubscription.objects.subscribe(group=group, subscriber=user)

assert GroupSubscription.objects.filter(group=group, user_id=user.id).exists()

# should not error
GroupSubscription.objects.subscribe(group=group, user=user)
GroupSubscription.objects.subscribe(group=group, subscriber=user)

def test_bulk(self):
group = self.create_group()
Expand Down
Loading