mirror of
https://gitee.com/dify_ai/dify.git
synced 2024-12-02 03:07:59 +08:00
refactor(version): simplify version comparison logic (#10109)
This commit is contained in:
parent
66e9bd90eb
commit
8b9fed75f3
@ -3,6 +3,7 @@ import logging
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from flask_restful import Resource, reqparse
|
from flask_restful import Resource, reqparse
|
||||||
|
from packaging import version
|
||||||
|
|
||||||
from configs import dify_config
|
from configs import dify_config
|
||||||
|
|
||||||
@ -47,43 +48,15 @@ class VersionApi(Resource):
|
|||||||
|
|
||||||
|
|
||||||
def _has_new_version(*, latest_version: str, current_version: str) -> bool:
|
def _has_new_version(*, latest_version: str, current_version: str) -> bool:
|
||||||
def parse_version(version: str) -> tuple:
|
try:
|
||||||
# Split version into parts and pre-release suffix if any
|
latest = version.parse(latest_version)
|
||||||
parts = version.split("-")
|
current = version.parse(current_version)
|
||||||
version_parts = parts[0].split(".")
|
|
||||||
pre_release = parts[1] if len(parts) > 1 else None
|
|
||||||
|
|
||||||
# Validate version format
|
# Compare versions
|
||||||
if len(version_parts) != 3:
|
return latest > current
|
||||||
raise ValueError(f"Invalid version format: {version}")
|
except version.InvalidVersion:
|
||||||
|
logging.warning(f"Invalid version format: latest={latest_version}, current={current_version}")
|
||||||
try:
|
|
||||||
# Convert version parts to integers
|
|
||||||
major, minor, patch = map(int, version_parts)
|
|
||||||
return (major, minor, patch, pre_release)
|
|
||||||
except ValueError:
|
|
||||||
raise ValueError(f"Invalid version format: {version}")
|
|
||||||
|
|
||||||
latest = parse_version(latest_version)
|
|
||||||
current = parse_version(current_version)
|
|
||||||
|
|
||||||
# Compare major, minor, and patch versions
|
|
||||||
for latest_part, current_part in zip(latest[:3], current[:3]):
|
|
||||||
if latest_part > current_part:
|
|
||||||
return True
|
|
||||||
elif latest_part < current_part:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# If versions are equal, check pre-release suffixes
|
|
||||||
if latest[3] is None and current[3] is not None:
|
|
||||||
return True
|
|
||||||
elif latest[3] is not None and current[3] is None:
|
|
||||||
return False
|
return False
|
||||||
elif latest[3] is not None and current[3] is not None:
|
|
||||||
# Simple string comparison for pre-release versions
|
|
||||||
return latest[3] > current[3]
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
api.add_resource(VersionApi, "/version")
|
api.add_resource(VersionApi, "/version")
|
||||||
|
@ -22,17 +22,3 @@ from controllers.console.version import _has_new_version
|
|||||||
)
|
)
|
||||||
def test_has_new_version(latest_version, current_version, expected):
|
def test_has_new_version(latest_version, current_version, expected):
|
||||||
assert _has_new_version(latest_version=latest_version, current_version=current_version) == expected
|
assert _has_new_version(latest_version=latest_version, current_version=current_version) == expected
|
||||||
|
|
||||||
|
|
||||||
def test_has_new_version_invalid_input():
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_has_new_version(latest_version="1.0", current_version="1.0.0")
|
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_has_new_version(latest_version="1.0.0", current_version="1.0")
|
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_has_new_version(latest_version="invalid", current_version="1.0.0")
|
|
||||||
|
|
||||||
with pytest.raises(ValueError):
|
|
||||||
_has_new_version(latest_version="1.0.0", current_version="invalid")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user