fix(workspace): 工作空间树挂载项目关联仓库——apps/modules数据源增加sd_project_repos(按实际目录归类),根治元景项目无spec.json时看不到模块/应用仓库

This commit is contained in:
ymq 2026-09-01 14:14:53 +08:00
parent 8993a10106
commit 493732e062

View File

@ -345,34 +345,57 @@ async def get_project_dir_pl(sor, uid, session_id='', pipeline_id=''):
async def get_project_apps_modules(sor, uid, session_id=''): async def get_project_apps_modules(sor, uid, session_id=''):
"""读项目目录下 {应用名}_spec.json,返回 (apps, modules)。 """返回项目用到的 (apps, modules)。
apps = 各 spec.json 的 app 字段;modules = referenced_modules + generated_modules 去重。 数据源(并集去重):
1. 项目目录下各 *_spec.json 的 app / referenced_modules / generated_modules 字段
2. sd_project_repos 关联仓库——按仓库在机构空间的实际位置归类({space}/apps/ 或
{space}/modules/);两个目录都不存在则不挂载(仓库可能尚未克隆)。
(2026-09-01 修复:元景项目无 spec.json 但有 7 个关联仓库,工作空间树
因此看不到任何模块/应用仓库——仓库表是项目用哪些仓库的权威来源之一)
""" """
import json as _json import json as _json
project_dir, _ = await get_project_dir(sor, uid, session_id) project_dir, _ = await get_project_dir(sor, uid, session_id)
if not project_dir or not os.path.isdir(project_dir): space_dir, _ = await get_space_dir(sor, uid, session_id)
return [], []
apps = [] apps = []
modules = set() modules = set()
if project_dir and os.path.isdir(project_dir):
try:
for f in sorted(os.listdir(project_dir)):
if not f.endswith('_spec.json'):
continue
fp = os.path.join(project_dir, f)
try:
with open(fp, 'r', encoding='utf-8') as fh:
data = _json.loads(fh.read())
except Exception:
continue
if not isinstance(data, dict):
continue
app = (data.get('app') or '').strip()
if app:
apps.append(app)
for m in (data.get('referenced_modules') or []) + (data.get('generated_modules') or []):
if isinstance(m, str) and m.strip():
modules.add(m.strip())
except Exception:
pass
# 关联仓库:按机构空间实际目录归类(仓库表是项目用哪些仓库的权威来源)
try: try:
for f in sorted(os.listdir(project_dir)): pid = await get_session_project_id(sor, uid, session_id)
if not f.endswith('_spec.json'): if pid and space_dir:
continue recs = await sor.sqlExe(
fp = os.path.join(project_dir, f) "SELECT repo_name FROM sd_project_repos WHERE project_id=${pid}$",
try: {"pid": pid})
with open(fp, 'r', encoding='utf-8') as fh: await sor.sqlExe("COMMIT", {})
data = _json.loads(fh.read()) for r in (recs or []):
except Exception: rn = (getattr(r, 'repo_name', '') or '').strip()
continue if not rn or '/' in rn:
if not isinstance(data, dict): continue
continue if os.path.isdir(os.path.join(space_dir, 'apps', rn)):
app = (data.get('app') or '').strip() apps.append(rn)
if app: elif os.path.isdir(os.path.join(space_dir, 'modules', rn)):
apps.append(app) modules.add(rn)
for m in (data.get('referenced_modules') or []) + (data.get('generated_modules') or []):
if isinstance(m, str) and m.strip():
modules.add(m.strip())
except Exception: except Exception:
pass pass
return sorted(set(apps)), sorted(modules) return sorted(set(apps)), sorted(modules)