代码测试

This commit is contained in:
Dang Zerong
2026-03-13 11:26:01 +08:00
parent 8f9e5bf4f5
commit cb90b66f09
6 changed files with 1291 additions and 201 deletions

32
db.py
View File

@@ -7,12 +7,17 @@
import sqlite3
import json
import os
from datetime import datetime
from datetime import datetime, timezone, timedelta
from typing import List, Dict, Any, Optional
DB_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'data', 'pr_scans.db')
def get_cst_now():
"""获取当前中国时区时间 (UTC+8)"""
return datetime.now(timezone(timedelta(hours=8))).strftime('%Y-%m-%d %H:%M:%S')
def get_db_connection():
"""获取数据库连接"""
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
@@ -110,6 +115,7 @@ class PRScanDB:
if existing:
# 更新现有记录
cst_time = get_cst_now()
cursor.execute('''
UPDATE pr_scans SET
pr_title = ?,
@@ -123,7 +129,7 @@ class PRScanDB:
security_issues = ?,
ai_review = ?,
report_path = ?,
updated_at = CURRENT_TIMESTAMP
updated_at = ?
WHERE repo_name = ? AND pr_number = ?
''', (
pr_info.get('pr_title'),
@@ -137,19 +143,22 @@ class PRScanDB:
security_issues,
json.dumps(scan_results.get('ai', {}), ensure_ascii=False),
report_path,
cst_time,
pr_info.get('repo_name'),
pr_info.get('pr_number')
))
scan_id = existing['id']
else:
# 插入新记录
cst_time = get_cst_now()
cursor.execute('''
INSERT INTO pr_scans (
pr_number, repo_name, pr_title, pr_url,
source_branch, target_branch, author,
state, scan_status, scan_result, scan_details_with_code,
issues_count, security_issues, ai_review, report_path
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
issues_count, security_issues, ai_review, report_path,
created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (
pr_info.get('pr_number'),
pr_info.get('repo_name'),
@@ -165,7 +174,9 @@ class PRScanDB:
issues_count,
security_issues,
json.dumps(scan_results.get('ai', {}), ensure_ascii=False),
report_path
report_path,
cst_time,
cst_time
))
scan_id = cursor.lastrowid
@@ -239,23 +250,24 @@ class PRScanDB:
"""更新 PR 状态"""
conn = get_db_connection()
cursor = conn.cursor()
cst_time = get_cst_now()
if state == 'merged':
cursor.execute('''
UPDATE pr_scans SET
state = ?,
merged_at = CURRENT_TIMESTAMP,
merged_at = ?,
merged_by = ?,
updated_at = CURRENT_TIMESTAMP
updated_at = ?
WHERE id = ?
''', (state, merged_by, scan_id))
''', (state, cst_time, merged_by, cst_time, scan_id))
else:
cursor.execute('''
UPDATE pr_scans SET
state = ?,
updated_at = CURRENT_TIMESTAMP
updated_at = ?
WHERE id = ?
''', (state, scan_id))
''', (state, cst_time, scan_id))
conn.commit()
conn.close()