Skip to content

Commit 77ba466

Browse files
authored
Refactor validation checks for version directories
1 parent cbbde57 commit 77ba466

1 file changed

Lines changed: 45 additions & 43 deletions

File tree

.github/scripts/validate_pr.py

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,57 +51,59 @@ def validate_tool_dir(tool_path, valid_tags):
5151
f"[{tool_name}] 目录名前缀无效,必须以 {VALID_PREFIXES} 之一开头"
5252
)
5353

54-
# 2. 必要文件
55-
for fname in REQUIRED_FILES:
56-
check(
57-
os.path.isfile(os.path.join(tool_path, fname)),
58-
f"[{tool_name}] 缺少必要文件: {fname}"
59-
)
60-
61-
# 3. 至少一个版本目录
54+
# 2. 至少一个版本目录
6255
version_dirs = [
6356
d for d in os.listdir(tool_path)
6457
if os.path.isdir(os.path.join(tool_path, d)) and VERSION_RE.match(d)
6558
] if os.path.isdir(tool_path) else []
6659
check(len(version_dirs) > 0, f"[{tool_name}] 缺少版本目录(如 1.0.0)")
6760

68-
# 4. data.yaml 内容校验
69-
yaml_path = os.path.join(tool_path, 'data.yaml')
70-
if os.path.isfile(yaml_path):
71-
try:
72-
with open(yaml_path, encoding='utf-8') as f:
73-
data = yaml.safe_load(f)
74-
for field in REQUIRED_YAML_FIELDS:
75-
check(
76-
field in data and data[field],
77-
f"[{tool_name}] data.yaml 缺少字段或为空: {field}"
78-
)
79-
if valid_tags and 'tags' in data and isinstance(data['tags'], list):
80-
for tag in data['tags']:
81-
check(
82-
tag in valid_tags,
83-
f"[{tool_name}] data.yaml 中 tag '{tag}' 不合法,可选值: {sorted(valid_tags)}"
84-
)
85-
except yaml.YAMLError as e:
86-
errors.append(f"[{tool_name}] data.yaml 解析失败: {e}")
87-
88-
# 5. README.md 非空
89-
readme_path = os.path.join(tool_path, 'README.md')
90-
if os.path.isfile(readme_path):
91-
content = open(readme_path, encoding='utf-8').read().strip()
92-
check(len(content) > 50, f"[{tool_name}] README.md 内容过少,请补充工具说明")
93-
if tool_name.startswith('tool_'):
61+
# 3. 每个版本目录下检查必要文件
62+
for version in version_dirs:
63+
version_path = os.path.join(tool_path, version)
64+
for fname in REQUIRED_FILES:
9465
check(
95-
'参数' in content or 'parameter' in content.lower(),
96-
f"[{tool_name}] 工具类 README.md 建议包含参数说明",
97-
is_warn=True
66+
os.path.isfile(os.path.join(version_path, fname)),
67+
f"[{tool_name}/{version}] 缺少必要文件: {fname}"
9868
)
9969

100-
# 6. logo.png 大小
101-
logo_path = os.path.join(tool_path, 'logo.png')
102-
if os.path.isfile(logo_path):
103-
size_kb = os.path.getsize(logo_path) / 1024
104-
check(size_kb <= 500, f"[{tool_name}] logo.png 过大 ({size_kb:.1f}KB),建议不超过 500KB", is_warn=True)
70+
# 4. data.yaml 内容校验
71+
yaml_path = os.path.join(version_path, 'data.yaml')
72+
if os.path.isfile(yaml_path):
73+
try:
74+
with open(yaml_path, encoding='utf-8') as f:
75+
data = yaml.safe_load(f)
76+
for field in REQUIRED_YAML_FIELDS:
77+
check(
78+
field in data and data[field],
79+
f"[{tool_name}/{version}] data.yaml 缺少字段或为空: {field}"
80+
)
81+
if valid_tags and 'tags' in data and isinstance(data['tags'], list):
82+
for tag in data['tags']:
83+
check(
84+
tag in valid_tags,
85+
f"[{tool_name}/{version}] data.yaml 中 tag '{tag}' 不合法,可选值: {sorted(valid_tags)}"
86+
)
87+
except yaml.YAMLError as e:
88+
errors.append(f"[{tool_name}/{version}] data.yaml 解析失败: {e}")
89+
90+
# 5. README.md 非空
91+
readme_path = os.path.join(version_path, 'README.md')
92+
if os.path.isfile(readme_path):
93+
content = open(readme_path, encoding='utf-8').read().strip()
94+
check(len(content) > 50, f"[{tool_name}/{version}] README.md 内容过少,请补充工具说明")
95+
if tool_name.startswith('tool_'):
96+
check(
97+
'参数' in content or 'parameter' in content.lower(),
98+
f"[{tool_name}/{version}] 工具类 README.md 建议包含参数说明",
99+
is_warn=True
100+
)
101+
102+
# 6. logo.png 大小
103+
logo_path = os.path.join(version_path, 'logo.png')
104+
if os.path.isfile(logo_path):
105+
size_kb = os.path.getsize(logo_path) / 1024
106+
check(size_kb <= 500, f"[{tool_name}/{version}] logo.png 过大 ({size_kb:.1f}KB),建议不超过 500KB", is_warn=True)
105107

106108
def main():
107109
changed_files_path = sys.argv[1]
@@ -133,4 +135,4 @@ def main():
133135
sys.exit(0)
134136

135137
if __name__ == '__main__':
136-
main()
138+
main()

0 commit comments

Comments
 (0)