Skip to content

Commit

Permalink
ref get_latest_release and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ameliahsu committed Jan 8, 2025
1 parent c2073ae commit 1931d96
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,42 @@
from sentry.workflow_engine.types import DataConditionHandler, WorkflowJob


def get_latest_release_for_env(
environment: Environment | None, event: GroupEvent
) -> Release | None:
cache_key = get_project_release_cache_key(
event.group.project_id, environment.id if environment else None
)
latest_release = cache.get(cache_key)
if latest_release is not None:
return latest_release

organization_id = event.group.project.organization_id
environments = [environment] if environment else None
try:
latest_release_version = get_latest_release(
[event.group.project],
environments,
organization_id,
)[0]
except Release.DoesNotExist:
return None
latest_release = Release.objects.get(
version=latest_release_version, organization_id=organization_id
)
cache.set(cache_key, latest_release or False, 600)
return latest_release


@condition_handler_registry.register(Condition.LATEST_RELEASE)
class LatestReleaseConditionHandler(DataConditionHandler[WorkflowJob]):
@staticmethod
def get_latest_release(environment_id: int | None, event: GroupEvent) -> Release | None:
cache_key = get_project_release_cache_key(event.group.project_id, environment_id)
cached_latest_release = cache.get(cache_key)
if cached_latest_release is None:
organization_id = event.group.project.organization_id
environments = None
if environment_id:
environments = [Environment.objects.get(id=environment_id)]
try:
latest_release_version = get_latest_release(
[event.group.project],
environments,
organization_id,
)[0]
except Release.DoesNotExist:
return None
latest_release = Release.objects.filter(
version=latest_release_version, organization_id=organization_id
).first()
if latest_release:
cache.set(cache_key, latest_release, 600)
return latest_release
else:
cache.set(cache_key, False, 600)
return cached_latest_release

@staticmethod
def evaluate_value(job: WorkflowJob, comparison: Any) -> bool:
event = job["event"]
workflow = job.get("workflow")
environment_id = workflow.environment_id if workflow else None
latest_release = LatestReleaseConditionHandler.get_latest_release(environment_id, event)
environment = workflow.environment if workflow else None

latest_release = get_latest_release_for_env(environment, event)
if not latest_release:
return False

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import UTC, datetime
from unittest.mock import patch

import pytest

Expand Down Expand Up @@ -152,3 +153,19 @@ def test_latest_release_with_environment(self):

self.event.data["tags"] = (("release", other_env_release.version),)
self.assert_does_not_pass(self.dc, self.job)

@patch("sentry.search.utils.get_latest_release")
def test_release_does_not_exist(self, mock_get_latest_release):
mock_get_latest_release.side_effect = Release.DoesNotExist
self.assert_does_not_pass(self.dc, self.job)

@patch.object(Release.objects, "get", return_value=None)
def test_no_release_object(self, mock_get):
newRelease = Release.objects.create(
organization_id=self.organization.id,
version="2",
date_added=datetime(2020, 9, 2, 3, 8, 24, 880386, tzinfo=UTC),
)
newRelease.add_project(self.project)

self.assert_does_not_pass(self.dc, self.job)

0 comments on commit 1931d96

Please sign in to comment.