Skip to content

Commit

Permalink
Add next_version to ecosystem helpers. (#270)
Browse files Browse the repository at this point in the history
This is useful for converting ranges from other formats.
  • Loading branch information
oliverchang authored Nov 29, 2021
1 parent a2bc342 commit 10b90c6
Show file tree
Hide file tree
Showing 9 changed files with 560 additions and 2 deletions.
3 changes: 3 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ steps:
- name: 'gcr.io/oss-vdb/ci'
dir: docker/importer
args: ['bash', '-x', 'run_tests.sh']
- name: 'gcr.io/oss-vdb/ci'
dir: lib
args: ['bash', '-x', 'run_tests.sh']
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args: [ '-c', "gcloud secrets versions access latest --secret=integration-test-account --format='get(payload.data)' | tr '_-' '/+' | base64 -d > /workspace/service_account.json" ]
Expand Down
1 change: 1 addition & 0 deletions docker/ci/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ RUN apt-get update && \
google-cloud-sdk-datastore-emulator

COPY daemon.json /etc/docker/daemon.json
ENTRYPOINT []
12 changes: 12 additions & 0 deletions lib/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true
name = "pypi"

[packages]
google-cloud-ndb = "*"
semver = "*"
pyyaml = "*"
pygit2 = "*"

[dev-packages]
471 changes: 471 additions & 0 deletions lib/Pipfile.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion lib/osv/bug_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import unittest

import bug
from . import bug


class NormalizeTest(unittest.TestCase):
Expand Down
22 changes: 22 additions & 0 deletions lib/osv/ecosystems.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ def _before_limits(self, version, limits):
return any(
self.sort_key(version) < self.sort_key(limit) for limit in limits)

def next_version(self, package, version):
"""Get the next version after the given version."""
versions = self.enumerate_versions(package, version, fixed=None)
if versions and versions[0] != version:
# Version does not exist, so use the first one that would sort
# after it (which is what enumerate_versions returns).
return versions[0]

if len(versions) > 1:
return versions[1]

return None

def sort_key(self, version):
"""Sort key."""
raise NotImplementedError
Expand Down Expand Up @@ -88,6 +101,15 @@ def enumerate_versions(self, package, introduced, fixed, limits=None):
del fixed
del limits

def next_version(self, package, version):
"""Get the next version after the given version."""
del package # Unused.
parsed_version = semver_index.parse(version)
if parsed_version.prerelease:
return version + '.0'

return version + '-0'

@property
def is_semver(self):
return True
Expand Down
43 changes: 43 additions & 0 deletions lib/osv/ecosystems_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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.
"""Bug helper tests."""

import unittest

from . import ecosystems


class GetNextVersionTest(unittest.TestCase):
"""get_next_version tests."""

def test_pypi(self):
ecosystem = ecosystems.get('PyPI')
self.assertEqual('1.36.0rc1', ecosystem.next_version('grpcio', '1.35.0'))
self.assertEqual('1.36.1', ecosystem.next_version('grpcio', '1.36.0'))
self.assertEqual('0.3.0', ecosystem.next_version('grpcio', '0'))

def test_maven(self):
ecosystem = ecosystems.get('Maven')
self.assertEqual('1.36.0',
ecosystem.next_version('io.grpc:grpc-core', '1.35.1'))
self.assertEqual('0.7.0', ecosystem.next_version('io.grpc:grpc-core', '0'))

def test_semver(self):
ecosystem = ecosystems.get('Go')
self.assertEqual('1.0.0-0', ecosystem.next_version('blah', '1.0.0'))
self.assertEqual('1.0.0-pre.0', ecosystem.next_version('blah', '1.0.0-pre'))


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions lib/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -ex

unset PIP_NO_BINARY
pipenv sync
pipenv run python -m unittest osv.bug_test
pipenv run python -m unittest osv.ecosystems_test
2 changes: 1 addition & 1 deletion lib/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setuptools.setup(
name='osv',
version='0.0.7',
version='0.0.8',
author='OSV authors',
author_email='osv-discuss@googlegroups.com',
description='Open Source Vulnerabilities library',
Expand Down

0 comments on commit 10b90c6

Please sign in to comment.