Skip to content

fix(taskworker) Pass project_id to find_channel_id_for_rule #91810

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

Merged
merged 2 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/sentry/api/endpoints/project_rule_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ def put(self, request: Request, project, rule) -> Response:
"name": data["name"],
"environment": data.get("environment"),
"project": project,
"project_id": project.id,
"action_match": data["actionMatch"],
"filter_match": data.get("filterMatch"),
"conditions": conditions,
Expand Down
1 change: 1 addition & 0 deletions src/sentry/api/endpoints/project_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ def post(self, request: Request, project) -> Response:
"name": data["name"],
"environment": data.get("environment"),
"project": project,
"project_id": project.id,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because find_channel_id_for_rule accepts kwargs adding this parameter will be safe. I'll still need a second change to remove the project parameter.

"action_match": data["actionMatch"],
"filter_match": data.get("filterMatch"),
"conditions": conditions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,23 @@
),
)
def find_channel_id_for_rule(
project: Project,
project: Project | None,
actions: Sequence[dict[str, Any]],
uuid: str,
rule_id: int | None = None,
user_id: int | None = None,
project_id: int | None = None,
**kwargs: Any,
) -> None:
redis_rule_status = RedisRuleStatus(uuid)

try:
project = Project.objects.get(id=project.id)
except Project.DoesNotExist:
redis_rule_status.set_value("failed")
return
if not project and project_id:
try:
project = Project.objects.get_from_cache(id=project_id)
except Project.DoesNotExist:
redis_rule_status.set_value("failed")
return
assert project, "find_channel_id_for_rule requires a project or project_id"

organization = project.organization
integration_id: int | None = None
Expand Down
47 changes: 47 additions & 0 deletions tests/sentry/integrations/slack/tasks/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,52 @@ def test_task_new_rule(self, mock_set_value):
"name": "New Rule",
"environment": None,
"project": self.project,
"project_id": self.project.id,
"action_match": "all",
"filter_match": "all",
"conditions": [
{"id": "sentry.rules.conditions.first_seen_event.FirstSeenEventCondition"}
],
"actions": [
{
"channel": "#my-channel",
"id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction",
"tags": "",
"workspace": self.integration.id,
}
],
"frequency": 5,
"uuid": self.uuid,
"user_id": self.user.id,
}

with self.tasks():
find_channel_id_for_rule(**data)

rule = Rule.objects.exclude(label__in=[DEFAULT_RULE_LABEL]).get(project_id=self.project.id)
mock_set_value.assert_called_with("success", rule.id)
assert rule.label == "New Rule"
# check that the channel_id got added
assert rule.data["actions"] == [
{
"channel": "#my-channel",
"channel_id": "chan-id",
"id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction",
"tags": "",
"workspace": self.integration.id,
}
]
assert rule.created_by_id == self.user.id

@responses.activate
@patch.object(RedisRuleStatus, "set_value", return_value=None)
def test_task_new_rule_project_id(self, mock_set_value):
# Task should work if project_id is passed instead of project
data = {
"name": "New Rule",
"environment": None,
"project": None,
"project_id": self.project.id,
"action_match": "all",
"filter_match": "all",
"conditions": [
Expand Down Expand Up @@ -128,6 +174,7 @@ def test_task_existing_rule(self, mock_set_value):
"name": "Test Rule",
"environment": None,
"project": self.project,
"project_id": self.project.id,
"action_match": "all",
"filter_match": "all",
"conditions": [condition_data],
Expand Down
Loading