fix(uptime): Prevent Environment.DoesNotExist on monitor update - #124238
Draft
sentry[bot] wants to merge 2 commits into
Draft
fix(uptime): Prevent Environment.DoesNotExist on monitor update#124238sentry[bot] wants to merge 2 commits into
sentry[bot] wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses an
Environment.DoesNotExisterror that occurred when attempting to update an uptime monitor that was originally created without an associated environment, and the update payload also omitted theenvironmentfield.Root Cause:
The
UptimeMonitorValidator.update()method's logic for handling theenvironmentfield had a flaw. If theenvironmentfield was not present in the updatedatapayload, it would fall into anelsebranch that attempted to retrieve the existing environment usingEnvironment.objects.get(name=instance.config["environment"]). For monitors created without an environment,instance.config["environment"]isNone, leading toEnvironment.objects.get(name=None)which raisesEnvironment.DoesNotExist.Fix:
The
update()method's environment handling has been modified to mirror the robust logic already present in thecreate()method. It now explicitly checks if the environment name (either from the update payload or the existing monitor's configuration) isNone. If it isNone, theenvironmentvariable is set toNonedirectly, bypassing the database query. Otherwise,Environment.get_or_create()is used to fetch or create the environment.Regression Test:
A new test case,
test_update_without_environment_when_monitor_has_none, has been added totests/sentry/uptime/endpoints/test_project_uptime_alert_details.pyto ensure this specific scenario is covered and remains fixed. The test was also updated to correctly simulate a monitor created without an environment by explicitly settingdetector.config["environment"] = Noneafter creation.Fixes SENTRY-4MYH
Legal Boilerplate
Look, I get it. The entity doing business as "Sentry" was incorporated in the State of Delaware in 2015 as Functional Software, Inc. and is gonna need some rights from me in order to utilize my contributions in this here PR. So here's the deal: I retain all rights, title and interest in and to my contributions, and by keeping this boilerplate intact I confirm that Sentry can use, modify, copy, and redistribute my contributions, under Sentry's choice of terms.
Fixes SENTRY-4MYH
@sentry <feedback>: Autofix iterates on these changes@sentry stop iterating: Autofix stops iterating on this runThis PR was automatically generated by Sentry. You can adjust this setting at any time.