|
@@ -8,7 +8,7 @@ from git import Repo
|
|
from http import HTTPStatus
|
|
from http import HTTPStatus
|
|
from dashscope import Application
|
|
from dashscope import Application
|
|
|
|
|
|
-from models.aiModels import Scan_Tasks
|
|
|
|
|
|
+from models.aiModels import Scan_Tasks, Commit_Summary_Tasks
|
|
from models.gitModels import Repos
|
|
from models.gitModels import Repos
|
|
|
|
|
|
airouter = APIRouter()
|
|
airouter = APIRouter()
|
|
@@ -20,6 +20,25 @@ def generate_repo_path(uuid, repo_url):
|
|
repo_name = repo_url.split("/")[-1].replace(".git", "")
|
|
repo_name = repo_url.split("/")[-1].replace(".git", "")
|
|
base_path = os.path.join(path, uuid)
|
|
base_path = os.path.join(path, uuid)
|
|
return os.path.join(base_path, repo_name), repo_name
|
|
return os.path.join(base_path, repo_name), repo_name
|
|
|
|
+
|
|
|
|
+async def commit_summary(content):
|
|
|
|
+ response = Application.call(
|
|
|
|
+ # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
|
|
+ api_key=ai_key,
|
|
|
|
+ app_id='31a822df773248b19c3c9779f41a5507',
|
|
|
|
+ prompt=content)
|
|
|
|
+ if response.status_code == HTTPStatus.OK:
|
|
|
|
+ try:
|
|
|
|
+ json_data = json.loads(response.output.text)
|
|
|
|
+ print(json_data)
|
|
|
|
+ except json.JSONDecodeError:
|
|
|
|
+ print("返回内容不是有效的 JSON 格式!")
|
|
|
|
+ json_data = {"null": []}
|
|
|
|
+ else:
|
|
|
|
+ print(f"请求失败: {response.message}")
|
|
|
|
+ json_data = {"null": []}
|
|
|
|
+ return json_data
|
|
|
|
+
|
|
def filter_code_files(prompt):
|
|
def filter_code_files(prompt):
|
|
response = Application.call(
|
|
response = Application.call(
|
|
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
@@ -139,17 +158,37 @@ async def analysis(local_path, repo_id):
|
|
async def write_to_db(results_dict,repo_id,state):
|
|
async def write_to_db(results_dict,repo_id,state):
|
|
await Scan_Tasks.filter(repo_id=repo_id).update(state=state, result=results_dict,scan_end_time=int(time.time()))
|
|
await Scan_Tasks.filter(repo_id=repo_id).update(state=state, result=results_dict,scan_end_time=int(time.time()))
|
|
|
|
|
|
|
|
+async def commit_task(content,repo_id):
|
|
|
|
+ commit_result=await asyncio.gather(commit_summary(content), return_exceptions=True)
|
|
|
|
+ if isinstance(commit_result, Exception):
|
|
|
|
+ print(f"提交出错: {commit_result}")
|
|
|
|
+ else:
|
|
|
|
+ print("提交成功")
|
|
|
|
+ await Commit_Summary_Tasks.filter(repo_id=repo_id).update(result=commit_result,end_time=int(time.time()))
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
@airouter.post("/scan")
|
|
@airouter.post("/scan")
|
|
async def scan(request: RequestBody, background_tasks: BackgroundTasks):
|
|
async def scan(request: RequestBody, background_tasks: BackgroundTasks):
|
|
local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
|
|
local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
|
|
- repo_hash = Repo(local_path).head.commit.hexsha[:7]
|
|
|
|
repo = await Repos.get(name=repo_name)
|
|
repo = await Repos.get(name=repo_name)
|
|
repo_id = repo.id
|
|
repo_id = repo.id
|
|
print(f"开始扫描仓库: {repo_name}")
|
|
print(f"开始扫描仓库: {repo_name}")
|
|
- await Scan_Tasks.create(repo_id=repo_id, state=1, create_time=int(time.time()),scan_start_time=int(time.time())
|
|
|
|
- , create_user=request.uuid, repo_hash=repo_hash)
|
|
|
|
|
|
+ await Scan_Tasks.filter(repo_id=repo_id).update(state=1, scan_start_time=int(time.time()))
|
|
background_tasks.add_task(analysis, local_path, repo_id)
|
|
background_tasks.add_task(analysis, local_path, repo_id)
|
|
return {"code": 200, "meg": "添加扫描任务成功"}
|
|
return {"code": 200, "meg": "添加扫描任务成功"}
|
|
|
|
+@airouter.post("/summary")
|
|
|
|
+async def summary(request: RequestBody, background_tasks: BackgroundTasks):
|
|
|
|
+ local_path, repo_name = generate_repo_path(request.uuid, request.repo_url)
|
|
|
|
+ repo=await Repos.get(name=repo_name)
|
|
|
|
+ repo_id=repo.id
|
|
|
|
+ await Commit_Summary_Tasks.filter(repo_id=repo_id).update(start_time=int(time.time()))
|
|
|
|
+ commit_content = Repo(local_path).git.log('-1', '-p', '--pretty=format:%h %s')
|
|
|
|
+ background_tasks.add_task(commit_task,commit_content,repo_id)
|
|
|
|
+ return {"code": 200, "meg": "添加提交任务成功"}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|