|
@@ -8,7 +8,7 @@ from git import Repo
|
|
|
from http import HTTPStatus
|
|
|
from dashscope import Application
|
|
|
|
|
|
-from models.aiModels import Scan_Tasks, Commit_Summary_Tasks
|
|
|
+from models.aiModels import Scan_Tasks, Commit_Summary_Tasks, File_Summary_Tasks
|
|
|
from models.gitModels import Repos
|
|
|
|
|
|
airouter = APIRouter()
|
|
@@ -16,11 +16,34 @@ class RequestBody(BaseModel):
|
|
|
uuid: str
|
|
|
repo_url: str
|
|
|
|
|
|
+class RequestFile(BaseModel):
|
|
|
+ uuid: str
|
|
|
+ repo_url: str
|
|
|
+ file_path: str
|
|
|
+
|
|
|
def generate_repo_path(uuid, repo_url):
|
|
|
repo_name = repo_url.split("/")[-1].replace(".git", "")
|
|
|
base_path = os.path.join(path, uuid)
|
|
|
return os.path.join(base_path, repo_name), repo_name
|
|
|
|
|
|
+async def file_summary(content):
|
|
|
+ response = Application.call(
|
|
|
+ # 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
|
+ api_key=ai_key,
|
|
|
+ app_id='5e3df30ad8bf44b68209e1ef089ff15c',
|
|
|
+ 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 = {"summary": []}
|
|
|
+ else:
|
|
|
+ print(f"请求失败: {response.message}")
|
|
|
+ json_data = {"summary": []}
|
|
|
+ return json_data
|
|
|
+
|
|
|
async def commit_summary(content):
|
|
|
response = Application.call(
|
|
|
# 若没有配置环境变量,可用百炼API Key将下行替换为:api_key="sk-xxx"。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。
|
|
@@ -166,6 +189,16 @@ async def commit_task(content,repo_id):
|
|
|
print("提交成功")
|
|
|
await Commit_Summary_Tasks.filter(repo_id=repo_id).update(result=commit_result,end_time=int(time.time()))
|
|
|
|
|
|
+async def file_task(file_path,repo_id):
|
|
|
+ with open(file_path, 'r', encoding="utf8") as f:
|
|
|
+ content = f.read()
|
|
|
+ file_result = await asyncio.gather(file_summary(content), return_exceptions=True)
|
|
|
+ if isinstance(file_result, Exception):
|
|
|
+ print(f"提交出错: {file_result}")
|
|
|
+ else:
|
|
|
+ print("提交成功")
|
|
|
+ await File_Summary_Tasks.filter(repo_id=repo_id).update(result=file_result, end_time=int(time.time()))
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
@@ -177,16 +210,31 @@ async def scan(request: RequestBody, background_tasks: BackgroundTasks):
|
|
|
print(f"开始扫描仓库: {repo_name}")
|
|
|
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)
|
|
|
- return {"code": 200, "meg": "添加扫描任务成功"}
|
|
|
-@airouter.post("/summary")
|
|
|
-async def summary(request: RequestBody, background_tasks: BackgroundTasks):
|
|
|
+ return {"code": 200, "msg": "添加扫描任务成功"}
|
|
|
+
|
|
|
+@airouter.post("/summaryCommit")
|
|
|
+async def summaryCommit(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": "添加提交任务成功"}
|
|
|
+ return {"code": 200, "msg": "添加提交任务成功"}
|
|
|
+
|
|
|
+@airouter.post("/summaryFile")
|
|
|
+async def summaryFile(request: RequestFile,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 File_Summary_Tasks.filter(repo_id=repo_id).update(start_time=int(time.time()))
|
|
|
+ background_tasks.add_task(file_task, request.file_path, repo_id)
|
|
|
+ return {"code": 200, "msg": "添加提交任务成功"}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
|