pipeline-sdlc/wwwroot/api/workspace_tree.dspy

50 lines
1.7 KiB
Plaintext

# workspace_tree.dspy - 返回 Tree widget 需要的 {id, parentid, label} 格式
uid = await get_user()
if not uid:
uid = 'user-01'
dbname = get_module_dbname('pipeline-sdlc')
workspace_base = '/d/pipeline/workspaces'
import os
async with DBPools().sqlorContext(dbname) as sor:
recs = await sor.sqlExe(
"SELECT current_project_id FROM pipeline_agent_settings WHERE user_id=${u}$",
{"u": uid})
pid = getattr(recs[0], 'current_project_id', '') if recs else ''
ws_dir = ''
if pid:
proj = await sor.sqlExe(
"SELECT name, org_id, workspace_dir FROM sd_projects WHERE id=${p}$",
{"p": pid})
if proj:
pname = getattr(proj[0], 'name', '')
org_id = getattr(proj[0], 'org_id', '0') or '0'
ws = getattr(proj[0], 'workspace_dir', '') or ''
ws_dir = ws if ws.startswith('/') else workspace_base + '/' + org_id + '/' + pname
if not ws_dir or not os.path.isdir(ws_dir):
return [{"id": "__root__", "parentid": "", "label": "📁 工作空间(不可用)"}]
# 递归扫描目录,生成 {id, parentid, label}
items = [{"id": "__root__", "parentid": "", "label": "📁 " + os.path.basename(ws_dir)}]
def scan_dir(path, parent_id):
try:
entries = sorted(os.listdir(path))
except Exception:
return
for name in entries:
full = os.path.join(path, name)
if name.startswith('.') or name == '__pycache__':
continue
if os.path.isdir(full):
dir_id = full # 用全路径做 id
items.append({"id": dir_id, "parentid": parent_id, "label": "📁 " + name})
scan_dir(full, dir_id)
scan_dir(ws_dir, "__root__")
return items