Skip to content

Commit 5002e6a

Browse files
authored
fix: fix apigw version validate (TencentBlueKing#173)
1 parent f3c4a4e commit 5002e6a

5 files changed

Lines changed: 73 additions & 6 deletions

File tree

.github/workflows/apigw-manager.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
fail-fast: false
2020
matrix:
2121
python-version: ["3.6", "3.7"]
22-
os: [ubuntu-20.04, macos-latest, windows-latest]
22+
os: [ubuntu-20.04, macos-13, windows-latest]
2323
steps:
2424
- uses: actions/checkout@v3
2525

sdks/apigw-manager/CHANGE.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Change logs
2-
2+
### 3.0.3
3+
- 修复资源版本校验问题
34
### 3.0.2
45
- 更新依赖 future 版本
56

sdks/apigw-manager/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "apigw-manager"
3-
version = "3.0.2"
3+
version = "3.0.3"
44
description = "The SDK for managing blueking gateway resource."
55
readme = "README.md"
66
authors = ["blueking <blueking@tencent.com>"]

sdks/apigw-manager/src/apigw_manager/apigw/utils.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@
99
* specific language governing permissions and limitations under the License.
1010
"""
1111
import os
12+
import re
1213
import zipfile
1314

1415
import yaml
1516
from bkapi_client_core.config import SettingKeys, settings
16-
from packaging.version import Version as _Version
17+
from packaging.version import Version as _Version, InvalidVersion
1718

1819
from apigw_manager.core import configuration
1920

@@ -106,12 +107,54 @@ def _get_archived_files(cls, path):
106107
for root, _, files in os.walk(path):
107108
for name in files:
108109
file_path = os.path.join(root, name)
109-
path_to_name[file_path] = file_path[len(path) :]
110+
path_to_name[file_path] = file_path[len(path):]
110111

111112
return path_to_name
112113

113114

115+
# 自定义VERSION_PATTERN
116+
VERSION_PATTERN = r"""
117+
v?
118+
(?:
119+
(?:(?P<epoch>[0-9]+)!)? # epoch
120+
(?P<release>[0-9]+(?:\.[0-9]+)*) # release segment
121+
(?P<pre> # pre-release
122+
[-_\.]?
123+
(?P<pre_l>alpha|a|beta|b|preview|pre|c|rc)
124+
[-_\.]?
125+
(?P<pre_n>[0-9]+)?
126+
)?
127+
(?P<post> # post release
128+
(?:-(?P<post_n1>[0-9]+))
129+
|
130+
(?:
131+
[-_\.]?
132+
(?P<post_l>post|rev|r)
133+
[-_\.]?
134+
(?P<post_n2>[0-9]+)?
135+
)
136+
)?
137+
(?P<dev> # dev release
138+
[-_\.]?
139+
(?P<dev_l>dev)
140+
[-_\.]?
141+
(?P<dev_n>[0-9]+)?
142+
)?
143+
)
144+
(?:\+(?P<local>[a-z0-9]+(?:[-_\.][a-z0-9]+)*))? # local version
145+
(?:-(?P<extra>[a-zA-Z0-9]+(?:[-_\.][a-zA-Z0-9]+)*))? # extra pre-release tags
146+
"""
147+
148+
114149
class SemVersion(_Version):
150+
_regex = re.compile(r"^\s*" + VERSION_PATTERN + r"\s*$", re.VERBOSE | re.IGNORECASE)
151+
152+
def __init__(self, version):
153+
match = self._regex.match(version)
154+
if not match:
155+
raise InvalidVersion(f"Invalid version: '{version}'")
156+
super().__init__(version)
157+
115158
@property
116159
def pre(self):
117160
pre = self._version.pre

sdks/apigw-manager/tests/apigw_manager/apigw/test_utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515
import pytest
1616
from django.conf import settings
17+
from packaging.version import InvalidVersion
1718

18-
from apigw_manager.apigw.utils import ZipArchiveFile, get_configuration, parse_value_list
19+
from apigw_manager.apigw.utils import ZipArchiveFile, get_configuration, parse_value_list, parse_version
1920

2021

2122
class TestGetConfiguration:
@@ -140,3 +141,25 @@ def test_get_archived_files(self):
140141
file_path = os.path.join(settings.BASE_DIR, "tests", "files", "docs")
141142
result = ZipArchiveFile._get_archived_files(file_path)
142143
assert result == {os.path.join(file_path, "zh", "get.md"): os.path.join("zh", "get.md")}
144+
145+
146+
class TestVersion:
147+
148+
def test_valid_versions(self):
149+
valid_versions = [
150+
"1.0.0",
151+
"2.1.0-alpha",
152+
"3.0.0-beta.1",
153+
"4.0.0-rc.1",
154+
"5.0.0+build.1",
155+
"6.0.0-alpha+build.1",
156+
"7.0.0-feature-layered-alpha2",
157+
"8.0.0-feature-layered-alpha.2",
158+
"3.14.1-feature-layered-alpha2"
159+
]
160+
for version in valid_versions:
161+
try:
162+
parse_version(version)
163+
except InvalidVersion:
164+
pytest.fail(f"Valid version '{version}' raised InvalidVersion")
165+

0 commit comments

Comments
 (0)