fix: cockpit_project_tasks 所有查询放同一个 async with 块

This commit is contained in:
yumoqing 2026-08-07 17:12:13 +08:00
parent bcd160d38e
commit eb4f5ce017

View File

@ -9,52 +9,49 @@ if not uid:
dbname = get_module_dbname('pipeline-sdlc') dbname = get_module_dbname('pipeline-sdlc')
# Get current project from agent_settings # Get current project and tasks in one session
async with DBPools().sqlorContext(dbname) as sor: async with DBPools().sqlorContext(dbname) as sor:
recs = await sor.sqlExe( recs = await sor.sqlExe(
"SELECT current_project_id FROM pipeline_agent_settings WHERE user_id=${uid}$", "SELECT current_project_id FROM pipeline_agent_settings WHERE user_id=${uid}$",
{"uid": uid}) {"uid": uid})
pid = getattr(recs[0], 'current_project_id', '') if recs else '' pid = getattr(recs[0], 'current_project_id', '') if recs else ''
if not pid: if not pid:
return json.dumps({"tasks": [], "error": "没有当前项目"}, ensure_ascii=False) return json.dumps({"tasks": [], "error": "没有当前项目"}, ensure_ascii=False)
# Get project name # Get project name
pname = '' pname = ''
precs = await sor.sqlExe( precs = await sor.sqlExe(
"SELECT name FROM sd_projects WHERE id=${pid}$", {"pid": pid}) "SELECT name FROM sd_projects WHERE id=${pid}$", {"pid": pid})
if precs and len(precs) > 0: if precs and len(precs) > 0:
pname = getattr(precs[0], 'name', '') or '' pname = getattr(precs[0], 'name', '') or ''
# Get tasks - filter by tenant_id matching project's org # Get tasks
# Use pipeline_task's DB or shared pipeline DB tasks = await sor.sqlExe(
tasks = await sor.sqlExe( "SELECT id, title, state, current_version, created_at, params, role "
"SELECT id, title, state, current_version, created_at, params, role " "FROM pipeline_tasks WHERE tenant_id='0' ORDER BY created_at DESC LIMIT 50",
"FROM pipeline_tasks WHERE tenant_id='0' ORDER BY created_at DESC LIMIT 50", {})
{})
task_list = [] task_list = []
for t in tasks: for t in tasks:
d = {} d = {}
# Only copy real data fields, skip methods raw = vars(t) if hasattr(t, '__dict__') else {}
raw = vars(t) if hasattr(t, '__dict__') else {} for k, v in raw.items():
for k, v in raw.items(): if not callable(v) and not k.startswith('_'):
if not callable(v) and not k.startswith('_'): if k == 'params' and v:
# Extract short description from params JSON try:
if k == 'params' and v: p = json.loads(v) if isinstance(v, str) else v
try: desc = p.get('description', '') or p.get('input_text', '') or ''
p = json.loads(v) if isinstance(v, str) else v d['description'] = desc[:60]
desc = p.get('description', '') or p.get('input_text', '') or '' except:
d['description'] = desc[:60] d['description'] = ''
except: elif k not in ('clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'to_dict'):
d['description'] = '' v_str = str(v) if v is not None else ''
elif k not in ('clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'to_dict'): d[k] = v_str
v_str = str(v) if v is not None else '' task_list.append(d)
d[k] = v_str
task_list.append(d)
return json.dumps({ return json.dumps({
"project_name": pname, "project_name": pname,
"project_id": pid, "project_id": pid,
"tasks": task_list "tasks": task_list
}, ensure_ascii=False) }, ensure_ascii=False)