Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 8 additions & 6 deletions src/sentry/uptime/endpoints/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,16 @@ def update(self, instance: Detector, data):
)

if "environment" in data:
env_name = data["environment"]
else:
env_name = instance.config["environment"]

if env_name is None:
environment = None
else:
environment = Environment.get_or_create(
project=self.context["project"],
name=data["environment"],
)
else:
environment = Environment.objects.get(
projects=self.context["project"],
name=instance.config["environment"],
name=env_name,
)

if "mode" in data:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ def test_enviroment(self) -> None:
assert detector.name == "test"
assert detector.config.get("environment") == "uptime-prod"

def test_update_without_environment_when_monitor_has_none(self) -> None:
"""Regression test: updating a monitor created without an environment
must not raise Environment.DoesNotExist when environment is omitted from
the PUT payload."""
detector = self.create_uptime_detector()
# Simulate a monitor that was created without an environment by clearing
# the environment from the config (mirrors the pre-existing bug scenario).
detector.config["environment"] = None
detector.save(update_fields=["config"])
assert detector.config.get("environment") is None

resp = self.get_success_response(
self.organization.slug,
detector.linked_project.slug,
detector.id,
name="updated-name",
)
detector.refresh_from_db()
assert resp.data == serialize(detector, self.user, UptimeDetectorSerializer())
assert detector.name == "updated-name"
# Environment should remain None
assert detector.config.get("environment") is None

def test_user(self) -> None:
detector = self.create_uptime_detector()

Expand Down
Loading