Skip to content

Commit

Permalink
fix: fix apigw version validate (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun authored Jun 7, 2024
1 parent f3c4a4e commit 5002e6a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/apigw-manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
fail-fast: false
matrix:
python-version: ["3.6", "3.7"]
os: [ubuntu-20.04, macos-latest, windows-latest]
os: [ubuntu-20.04, macos-13, windows-latest]
steps:
- uses: actions/checkout@v3

Expand Down
3 changes: 2 additions & 1 deletion sdks/apigw-manager/CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Change logs

### 3.0.3
- 修复资源版本校验问题
### 3.0.2
- 更新依赖 future 版本

Expand Down
2 changes: 1 addition & 1 deletion sdks/apigw-manager/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "apigw-manager"
version = "3.0.2"
version = "3.0.3"
description = "The SDK for managing blueking gateway resource."
readme = "README.md"
authors = ["blueking <[email protected]>"]
Expand Down
47 changes: 45 additions & 2 deletions sdks/apigw-manager/src/apigw_manager/apigw/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
* specific language governing permissions and limitations under the License.
"""
import os
import re
import zipfile

import yaml
from bkapi_client_core.config import SettingKeys, settings
from packaging.version import Version as _Version
from packaging.version import Version as _Version, InvalidVersion

from apigw_manager.core import configuration

Expand Down Expand Up @@ -106,12 +107,54 @@ def _get_archived_files(cls, path):
for root, _, files in os.walk(path):
for name in files:
file_path = os.path.join(root, name)
path_to_name[file_path] = file_path[len(path) :]
path_to_name[file_path] = file_path[len(path):]

return path_to_name


# 自定义VERSION_PATTERN
VERSION_PATTERN = r"""
v?
(?:
(?:(?P<epoch>[0-9]+)!)? # epoch
(?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
(?P<pre> # pre-release
[-_\.]?
(?P<pre_l>alpha|a|beta|b|preview|pre|c|rc)
[-_\.]?
(?P<pre_n>[0-9]+)?
)?
(?P<post> # post release
(?:-(?P<post_n1>[0-9]+))
|
(?:
[-_\.]?
(?P<post_l>post|rev|r)
[-_\.]?
(?P<post_n2>[0-9]+)?
)
)?
(?P<dev> # dev release
[-_\.]?
(?P<dev_l>dev)
[-_\.]?
(?P<dev_n>[0-9]+)?
)?
)
(?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version
(?:-(?P<extra>[a-zA-Z0-9]+(?:[-_\.][a-zA-Z0-9]+)*))? # extra pre-release tags
"""


class SemVersion(_Version):
_regex = re.compile(r"^\s*" + VERSION_PATTERN + r"\s*$", re.VERBOSE | re.IGNORECASE)

def __init__(self, version):
match = self._regex.match(version)
if not match:
raise InvalidVersion(f"Invalid version: '{version}'")
super().__init__(version)

@property
def pre(self):
pre = self._version.pre
Expand Down
25 changes: 24 additions & 1 deletion sdks/apigw-manager/tests/apigw_manager/apigw/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

import pytest
from django.conf import settings
from packaging.version import InvalidVersion

from apigw_manager.apigw.utils import ZipArchiveFile, get_configuration, parse_value_list
from apigw_manager.apigw.utils import ZipArchiveFile, get_configuration, parse_value_list, parse_version


class TestGetConfiguration:
Expand Down Expand Up @@ -140,3 +141,25 @@ def test_get_archived_files(self):
file_path = os.path.join(settings.BASE_DIR, "tests", "files", "docs")
result = ZipArchiveFile._get_archived_files(file_path)
assert result == {os.path.join(file_path, "zh", "get.md"): os.path.join("zh", "get.md")}


class TestVersion:

def test_valid_versions(self):
valid_versions = [
"1.0.0",
"2.1.0-alpha",
"3.0.0-beta.1",
"4.0.0-rc.1",
"5.0.0+build.1",
"6.0.0-alpha+build.1",
"7.0.0-feature-layered-alpha2",
"8.0.0-feature-layered-alpha.2",
"3.14.1-feature-layered-alpha2"
]
for version in valid_versions:
try:
parse_version(version)
except InvalidVersion:
pytest.fail(f"Valid version '{version}' raised InvalidVersion")

0 comments on commit 5002e6a

Please sign in to comment.