From 33a3087a7ea73bb06343884720a8f9f9c62dacae Mon Sep 17 00:00:00 2001 From: Oliver Chang Date: Tue, 29 Oct 2024 13:15:02 +1100 Subject: [PATCH] Remove model_helpers.py for now. We had duplicate definitions, which led to different implementations being used in different settings. In models.py, we used the ones defined in models.py. When called from the top level `osv` module (e.g. our API), we used the one in model_helpers.py --- osv/__init__.py | 1 - osv/model_helpers.py | 78 -------------------------------------------- osv/models.py | 6 ++-- 3 files changed, 3 insertions(+), 82 deletions(-) delete mode 100644 osv/model_helpers.py diff --git a/osv/__init__.py b/osv/__init__.py index 45e00d7e271..e836fede8e4 100644 --- a/osv/__init__.py +++ b/osv/__init__.py @@ -18,5 +18,4 @@ from .repos import * from .sources import * from .models import * -from .model_helpers import * from .ecosystems import * diff --git a/osv/model_helpers.py b/osv/model_helpers.py deleted file mode 100644 index a802c384755..00000000000 --- a/osv/model_helpers.py +++ /dev/null @@ -1,78 +0,0 @@ -# Copyright 2021 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -"""Datastore types.""" - -import datetime - -from google.cloud import ndb - -# pylint: disable=relative-beyond-top-level -from .models import AliasGroup, AffectedEvent, SourceRepository, Bug -from . import ecosystems - - -def utcnow(): - """For mocking.""" - return datetime.datetime.utcnow() - - -def get_source_repository(source_name: str) -> SourceRepository: - """Get source repository.""" - return SourceRepository.get_by_id(source_name) # type: ignore - - -def sorted_events(ecosystem, range_type, events) -> list[AffectedEvent]: - """Sort events.""" - if range_type == 'GIT': - # No need to sort. - return events - - if range_type == 'SEMVER': - ecosystem_helper = ecosystems.SemverEcosystem() - else: - ecosystem_helper = ecosystems.get(ecosystem) - - if ecosystem_helper is None or not ecosystem_helper.supports_ordering: - raise ValueError('Unsupported ecosystem ' + ecosystem) - - # Remove any magic '0' values. - sorted_copy = [] - zero_event = None - for event in events: - if event.value == '0': - zero_event = event - continue - - sorted_copy.append(event) - - sorted_copy.sort(key=lambda e: ecosystem_helper.sort_key(e.value)) - if zero_event: - sorted_copy.insert(0, zero_event) - - return sorted_copy - - -@ndb.tasklet -def get_aliases_async(bug_id: str) -> ndb.Future: - """Gets aliases asynchronously.""" - alias_group = yield AliasGroup.query(AliasGroup.bug_ids == bug_id).get_async() - return alias_group - - -@ndb.tasklet -def get_related_async(bug_id: str) -> ndb.Future: - """Gets related bugs asynchronously.""" - related_bugs = yield Bug.query(Bug.related == bug_id).fetch_async() - related_bug_ids = [bug.db_id for bug in related_bugs] - return related_bug_ids diff --git a/osv/models.py b/osv/models.py index a27af584633..6d53c0a6f4f 100644 --- a/osv/models.py +++ b/osv/models.py @@ -920,7 +920,7 @@ class ImportFinding(ndb.Model): last_attempt: datetime = ndb.DateTimeProperty() -def get_source_repository(source_name): +def get_source_repository(source_name: str) -> SourceRepository: """Get source repository.""" return SourceRepository.get_by_id(source_name) @@ -957,14 +957,14 @@ def sorted_events(ecosystem, range_type, events) -> list[AffectedEvent]: @ndb.tasklet -def get_aliases_async(bug_id) -> ndb.Future: +def get_aliases_async(bug_id: str) -> ndb.Future: """Gets aliases asynchronously.""" alias_group = yield AliasGroup.query(AliasGroup.bug_ids == bug_id).get_async() return alias_group @ndb.tasklet -def get_related_async(bug_id) -> ndb.Future: +def get_related_async(bug_id: str) -> ndb.Future: """Gets related bugs asynchronously.""" related_bugs = yield Bug.query( Bug.related == bug_id, projection=[Bug.db_id]).fetch_async()