Skip to content

Commit cf3fd9c

Browse files
authored
fix(taskworker) Pass project_id to find_channel_id_for_rule (#91810)
We can't pass project to this task as projects cannot be json encoded.
1 parent 59474fb commit cf3fd9c

File tree

5 files changed

+59
-6
lines changed

5 files changed

+59
-6
lines changed

src/sentry/api/endpoints/project_rule_details.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ def put(self, request: Request, project, rule) -> Response:
245245
"name": data["name"],
246246
"environment": data.get("environment"),
247247
"project": project,
248+
"project_id": project.id,
248249
"action_match": data["actionMatch"],
249250
"filter_match": data.get("filterMatch"),
250251
"conditions": conditions,

src/sentry/api/endpoints/project_rules.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,7 @@ def post(self, request: Request, project) -> Response:
800800
"name": data["name"],
801801
"environment": data.get("environment"),
802802
"project": project,
803+
"project_id": project.id,
803804
"action_match": data["actionMatch"],
804805
"filter_match": data.get("filterMatch"),
805806
"conditions": conditions,

src/sentry/integrations/slack/tasks/find_channel_id_for_rule.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,23 @@
3232
),
3333
)
3434
def find_channel_id_for_rule(
35-
project: Project,
35+
project: Project | None,
3636
actions: Sequence[dict[str, Any]],
3737
uuid: str,
3838
rule_id: int | None = None,
3939
user_id: int | None = None,
40+
project_id: int | None = None,
4041
**kwargs: Any,
4142
) -> None:
4243
redis_rule_status = RedisRuleStatus(uuid)
4344

44-
try:
45-
project = Project.objects.get(id=project.id)
46-
except Project.DoesNotExist:
47-
redis_rule_status.set_value("failed")
48-
return
45+
if not project and project_id:
46+
try:
47+
project = Project.objects.get_from_cache(id=project_id)
48+
except Project.DoesNotExist:
49+
redis_rule_status.set_value("failed")
50+
return
51+
assert project, "find_channel_id_for_rule requires a project or project_id"
4952

5053
organization = project.organization
5154
integration_id: int | None = None

tests/sentry/api/endpoints/test_project_rules.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ def test_kicks_off_slack_async_job(
727727
payload["actions"][0].pop("name")
728728
kwargs = {
729729
"name": payload["name"],
730+
"project_id": self.project.id,
730731
"environment": payload.get("environment"),
731732
"action_match": payload["actionMatch"],
732733
"filter_match": payload.get("filterMatch"),

tests/sentry/integrations/slack/tasks/test_tasks.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,52 @@ def test_task_new_rule(self, mock_set_value):
7979
"name": "New Rule",
8080
"environment": None,
8181
"project": self.project,
82+
"project_id": self.project.id,
83+
"action_match": "all",
84+
"filter_match": "all",
85+
"conditions": [
86+
{"id": "sentry.rules.conditions.first_seen_event.FirstSeenEventCondition"}
87+
],
88+
"actions": [
89+
{
90+
"channel": "#my-channel",
91+
"id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction",
92+
"tags": "",
93+
"workspace": self.integration.id,
94+
}
95+
],
96+
"frequency": 5,
97+
"uuid": self.uuid,
98+
"user_id": self.user.id,
99+
}
100+
101+
with self.tasks():
102+
find_channel_id_for_rule(**data)
103+
104+
rule = Rule.objects.exclude(label__in=[DEFAULT_RULE_LABEL]).get(project_id=self.project.id)
105+
mock_set_value.assert_called_with("success", rule.id)
106+
assert rule.label == "New Rule"
107+
# check that the channel_id got added
108+
assert rule.data["actions"] == [
109+
{
110+
"channel": "#my-channel",
111+
"channel_id": "chan-id",
112+
"id": "sentry.integrations.slack.notify_action.SlackNotifyServiceAction",
113+
"tags": "",
114+
"workspace": self.integration.id,
115+
}
116+
]
117+
assert rule.created_by_id == self.user.id
118+
119+
@responses.activate
120+
@patch.object(RedisRuleStatus, "set_value", return_value=None)
121+
def test_task_new_rule_project_id(self, mock_set_value):
122+
# Task should work if project_id is passed instead of project
123+
data = {
124+
"name": "New Rule",
125+
"environment": None,
126+
"project": None,
127+
"project_id": self.project.id,
82128
"action_match": "all",
83129
"filter_match": "all",
84130
"conditions": [
@@ -128,6 +174,7 @@ def test_task_existing_rule(self, mock_set_value):
128174
"name": "Test Rule",
129175
"environment": None,
130176
"project": self.project,
177+
"project_id": self.project.id,
131178
"action_match": "all",
132179
"filter_match": "all",
133180
"conditions": [condition_data],

0 commit comments

Comments
 (0)