Skip to content

Commit f737cc1

Browse files
roagaandrewshie-sentry
authored andcommitted
feat(autofix): Track if autofix state is fetched through endpoint or elsewhere (#93088)
Tracking if a user is viewing the state or the state is just being accessed by the sentry backend. Will be used for stopping automated runs if the user is looking at it. Frontend PR to set flag: #93168
1 parent c617bb5 commit f737cc1

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

src/sentry/api/endpoints/group_ai_autofix.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ def get(self, request: Request, group: Group) -> Response:
6666
if not access_check_cache_value:
6767
check_repo_access = True
6868

69-
autofix_state = get_autofix_state(group_id=group.id, check_repo_access=check_repo_access)
69+
is_user_watching = request.GET.get("isUserWatching", False)
70+
71+
autofix_state = get_autofix_state(
72+
group_id=group.id,
73+
check_repo_access=check_repo_access,
74+
is_user_fetching=bool(is_user_watching),
75+
)
7076

7177
if check_repo_access:
7278
cache.set(access_check_cache_key, True, timeout=60) # 1 minute timeout

src/sentry/autofix/utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,19 @@ def get_autofix_repos_from_project_code_mappings(project: Project) -> list[dict]
8585

8686

8787
def get_autofix_state(
88-
*, group_id: int | None = None, run_id: int | None = None, check_repo_access: bool = False
88+
*,
89+
group_id: int | None = None,
90+
run_id: int | None = None,
91+
check_repo_access: bool = False,
92+
is_user_fetching: bool = False,
8993
) -> AutofixState | None:
9094
path = "/v1/automation/autofix/state"
9195
body = orjson.dumps(
9296
{
9397
"group_id": group_id,
9498
"run_id": run_id,
9599
"check_repo_access": check_repo_access,
100+
"is_user_fetching": is_user_fetching,
96101
}
97102
)
98103

tests/sentry/api/endpoints/test_group_ai_autofix.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ def test_ai_autofix_get_endpoint_with_autofix(
5050
assert "profile" not in response.data["autofix"]["request"]
5151
assert "issue_summary" not in response.data["autofix"]["request"]
5252

53-
mock_get_autofix_state.assert_called_once_with(group_id=group.id, check_repo_access=True)
53+
mock_get_autofix_state.assert_called_once_with(
54+
group_id=group.id, check_repo_access=True, is_user_fetching=False
55+
)
5456

5557
@patch("sentry.api.endpoints.group_ai_autofix.get_autofix_state")
5658
def test_ai_autofix_get_endpoint_without_autofix(
@@ -65,7 +67,9 @@ def test_ai_autofix_get_endpoint_without_autofix(
6567
assert response.status_code == 200
6668
assert response.data["autofix"] is None
6769

68-
mock_get_autofix_state.assert_called_once_with(group_id=group.id, check_repo_access=True)
70+
mock_get_autofix_state.assert_called_once_with(
71+
group_id=group.id, check_repo_access=True, is_user_fetching=False
72+
)
6973

7074
@patch("sentry.api.endpoints.group_ai_autofix.get_autofix_state")
7175
@patch("sentry.api.endpoints.group_ai_autofix.get_sorted_code_mapping_configs")
@@ -650,7 +654,7 @@ def test_ai_autofix_get_endpoint_cache_miss(
650654
# Verify cache behavior - cache miss should trigger repo access check
651655
mock_cache.get.assert_called_once_with(f"autofix_access_check:{self.group.id}")
652656
mock_get_autofix_state.assert_called_once_with(
653-
group_id=self.group.id, check_repo_access=True
657+
group_id=self.group.id, check_repo_access=True, is_user_fetching=False
654658
)
655659

656660
# Verify the cache was set with a 60-second timeout
@@ -681,7 +685,7 @@ def test_ai_autofix_get_endpoint_cache_hit(
681685
# Verify cache behavior - cache hit should skip repo access check
682686
mock_cache.get.assert_called_once_with(f"autofix_access_check:{self.group.id}")
683687
mock_get_autofix_state.assert_called_once_with(
684-
group_id=self.group.id, check_repo_access=False
688+
group_id=self.group.id, check_repo_access=False, is_user_fetching=False
685689
)
686690

687691
# Verify the cache was not set again
@@ -713,7 +717,9 @@ def test_ai_autofix_get_endpoint_polling_behavior(
713717

714718
# Verify first request behavior
715719
mock_cache.get.assert_called_once_with(f"autofix_access_check:{group.id}")
716-
mock_get_autofix_state.assert_called_once_with(group_id=group.id, check_repo_access=True)
720+
mock_get_autofix_state.assert_called_once_with(
721+
group_id=group.id, check_repo_access=True, is_user_fetching=False
722+
)
717723
mock_cache.set.assert_called_once_with(f"autofix_access_check:{group.id}", True, timeout=60)
718724

719725
# Reset mocks for second request
@@ -728,7 +734,9 @@ def test_ai_autofix_get_endpoint_polling_behavior(
728734

729735
# Verify second request behavior
730736
mock_cache.get.assert_called_once_with(f"autofix_access_check:{group.id}")
731-
mock_get_autofix_state.assert_called_once_with(group_id=group.id, check_repo_access=False)
737+
mock_get_autofix_state.assert_called_once_with(
738+
group_id=group.id, check_repo_access=False, is_user_fetching=False
739+
)
732740
mock_cache.set.assert_not_called()
733741

734742
# Reset mocks for third request
@@ -743,5 +751,7 @@ def test_ai_autofix_get_endpoint_polling_behavior(
743751

744752
# Verify third request behavior - should be like the first request
745753
mock_cache.get.assert_called_once_with(f"autofix_access_check:{group.id}")
746-
mock_get_autofix_state.assert_called_once_with(group_id=group.id, check_repo_access=True)
754+
mock_get_autofix_state.assert_called_once_with(
755+
group_id=group.id, check_repo_access=True, is_user_fetching=False
756+
)
747757
mock_cache.set.assert_called_once_with(f"autofix_access_check:{group.id}", True, timeout=60)

tests/sentry/autofix/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def test_get_autofix_state_success_with_group_id(self, mock_post):
123123

124124
mock_post.assert_called_once_with(
125125
f"{settings.SEER_AUTOFIX_URL}/v1/automation/autofix/state",
126-
data=b'{"group_id":123,"run_id":null,"check_repo_access":false}',
126+
data=b'{"group_id":123,"run_id":null,"check_repo_access":false,"is_user_fetching":false}',
127127
headers={"content-type": "application/json;charset=utf-8"},
128128
)
129129

@@ -154,7 +154,7 @@ def test_get_autofix_state_success_with_run_id(self, mock_post):
154154

155155
mock_post.assert_called_once_with(
156156
f"{settings.SEER_AUTOFIX_URL}/v1/automation/autofix/state",
157-
data=b'{"group_id":null,"run_id":456,"check_repo_access":false}',
157+
data=b'{"group_id":null,"run_id":456,"check_repo_access":false,"is_user_fetching":false}',
158158
headers={"content-type": "application/json;charset=utf-8"},
159159
)
160160

0 commit comments

Comments
 (0)