Address code review feedback: improve pattern matching and error handling

Co-authored-by: whyour <22700758+whyour@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2025-12-27 08:49:41 +00:00
parent 023d0f1cc4
commit f3ec352066
2 changed files with 69 additions and 35 deletions

View File

@ -535,16 +535,30 @@ export async function setSystemTimezone(timezone: string): Promise<boolean> {
} }
} }
// Helper function to check if a name is a GitHub URL
function isGitHubUrl(name: string): boolean {
return !!name.match(/^(https?:\/\/|git\+)/i);
}
// Helper function to check if a name is a requirements file
function isRequirementsFile(name: string): boolean {
return !!name.match(/requirements.*\.(txt|in)$/i) || name === 'requirements.txt';
}
// Helper function to check if a name is a pyproject.toml file
function isPyprojectToml(name: string): boolean {
return name.endsWith('pyproject.toml');
}
export function getGetCommand(type: DependenceTypes, name: string): string { export function getGetCommand(type: DependenceTypes, name: string): string {
const trimmedName = name.trim(); const trimmedName = name.trim();
// For Python dependencies installed from GitHub or requirements files, // For Python dependencies installed from GitHub or requirements files,
// we can't reliably check if they're installed, so skip the check // we can't reliably check if they're installed, so skip the check
if (type === DependenceTypes.python3) { if (type === DependenceTypes.python3) {
if (trimmedName.match(/^(https?:\/\/|git\+)/i) || if (isGitHubUrl(trimmedName) ||
trimmedName.match(/\.(txt|in)$/i) || isRequirementsFile(trimmedName) ||
trimmedName.includes('requirements') || isPyprojectToml(trimmedName)) {
trimmedName.endsWith('pyproject.toml')) {
// Return a command that will always indicate not installed // Return a command that will always indicate not installed
// This ensures GitHub URLs and requirements files are always installed // This ensures GitHub URLs and requirements files are always installed
return 'echo ""'; return 'echo ""';
@ -590,18 +604,24 @@ export function getInstallCommand(type: DependenceTypes, name: string): string {
// Handle different installation methods for Python // Handle different installation methods for Python
if (type === DependenceTypes.python3) { if (type === DependenceTypes.python3) {
// Check if it's a GitHub URL (support both git+ and direct URLs) // Check if it's a GitHub URL (support both git+ and direct URLs)
if (trimmedName.match(/^(https?:\/\/|git\+)/i)) { if (isGitHubUrl(trimmedName)) {
return `${command} ${trimmedName}`; return `${command} ${trimmedName}`;
} }
// Check if it's a requirements file path // Check if it's a requirements file path
if (trimmedName.match(/\.(txt|in)$/i) || trimmedName.includes('requirements')) { if (isRequirementsFile(trimmedName)) {
return `${command} -r ${trimmedName}`; return `${command} -r ${trimmedName}`;
} }
// Check if it's a pyproject.toml file // Check if it's a pyproject.toml file
if (trimmedName.endsWith('pyproject.toml')) { if (isPyprojectToml(trimmedName)) {
// For pyproject.toml, install from the directory containing it // For pyproject.toml, install from the directory containing it
const dir = trimmedName.replace(/\/pyproject\.toml$/, ''); const pathMatch = trimmedName.match(/^(.+)\/pyproject\.toml$/);
return dir && dir !== 'pyproject.toml' ? `${command} ${dir}` : `${command} .`; if (pathMatch) {
// Has a path prefix, use the directory
return `${command} ${pathMatch[1]}`;
} else {
// Just "pyproject.toml", install current directory
return `${command} .`;
}
} }
} }

View File

@ -124,22 +124,29 @@ auto_install_python_deps() {
if [[ -f "${repo_path}/requirements.txt" ]]; then if [[ -f "${repo_path}/requirements.txt" ]]; then
echo -e "发现 requirements.txt开始自动安装依赖...\n" echo -e "发现 requirements.txt开始自动安装依赖...\n"
local req_file="${dir_scripts}/${uniq_path}/requirements.txt" local req_file="${dir_scripts}/${uniq_path}/requirements.txt"
cp -f "${repo_path}/requirements.txt" "${req_file}"
# 调用API添加依赖安装任务 # 确保目标目录存在
local dep_name="${uniq_path}/requirements.txt" make_dir "${dir_scripts}/${uniq_path}"
local currentTimeStamp=$(date +%s)
local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \
-X POST \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer ${__ql_token__}" \
--data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null)
local code=$(echo "$result" | jq -r '.code' 2>/dev/null) # 复制文件并检查结果
if [[ "$code" == "200" ]]; then if cp -f "${repo_path}/requirements.txt" "${req_file}" 2>/dev/null; then
echo -e "已添加 requirements.txt 依赖安装任务\n" # 调用API添加依赖安装任务
local dep_name="${uniq_path}/requirements.txt"
local currentTimeStamp=$(date +%s)
local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \
-X POST \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer ${__ql_token__}" \
--data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null)
local code=$(echo "$result" | jq -r '.code' 2>/dev/null)
if [[ "$code" == "200" ]]; then
echo -e "已添加 requirements.txt 依赖安装任务\n"
else
echo -e "添加 requirements.txt 依赖失败,请手动添加\n"
fi
else else
echo -e "添加 requirements.txt 依赖失败,请手动添加\n" echo -e "复制 requirements.txt 失败,跳过自动安装\n"
fi fi
fi fi
@ -147,22 +154,29 @@ auto_install_python_deps() {
if [[ -f "${repo_path}/pyproject.toml" ]]; then if [[ -f "${repo_path}/pyproject.toml" ]]; then
echo -e "发现 pyproject.toml开始自动安装依赖...\n" echo -e "发现 pyproject.toml开始自动安装依赖...\n"
local pyproject_file="${dir_scripts}/${uniq_path}/pyproject.toml" local pyproject_file="${dir_scripts}/${uniq_path}/pyproject.toml"
cp -f "${repo_path}/pyproject.toml" "${pyproject_file}"
# 调用API添加依赖安装任务 # 确保目标目录存在
local dep_name="${uniq_path}/pyproject.toml" make_dir "${dir_scripts}/${uniq_path}"
local currentTimeStamp=$(date +%s)
local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \
-X POST \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer ${__ql_token__}" \
--data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null)
local code=$(echo "$result" | jq -r '.code' 2>/dev/null) # 复制文件并检查结果
if [[ "$code" == "200" ]]; then if cp -f "${repo_path}/pyproject.toml" "${pyproject_file}" 2>/dev/null; then
echo -e "已添加 pyproject.toml 依赖安装任务\n" # 调用API添加依赖安装任务
local dep_name="${uniq_path}/pyproject.toml"
local currentTimeStamp=$(date +%s)
local result=$(curl -s --noproxy "*" "http://127.0.0.1:${ql_port}/open/dependencies?t=$currentTimeStamp" \
-X POST \
-H "Content-Type: application/json;charset=UTF-8" \
-H "Authorization: Bearer ${__ql_token__}" \
--data-raw "[{\"name\":\"${dep_name}\",\"type\":1,\"remark\":\"自动检测:${uniq_path} 订阅依赖\"}]" 2>/dev/null)
local code=$(echo "$result" | jq -r '.code' 2>/dev/null)
if [[ "$code" == "200" ]]; then
echo -e "已添加 pyproject.toml 依赖安装任务\n"
else
echo -e "添加 pyproject.toml 依赖失败,请手动添加\n"
fi
else else
echo -e "添加 pyproject.toml 依赖失败,请手动添加\n" echo -e "复制 pyproject.toml 失败,跳过自动安装\n"
fi fi
fi fi
} }