Browse Source

新增commit分析

fulian23 4 months ago
parent
commit
791e2b1589
2 changed files with 53 additions and 4 deletions
  1. 43 4
      api/aiRouter.py
  2. 10 0
      models/aiModels.py

+ 43 - 4
api/aiRouter.py

@@ -8,7 +8,7 @@ from git import Repo
 from http import HTTPStatus
 from dashscope import Application
 
-from models.aiModels import Scan_Tasks
+from models.aiModels import Scan_Tasks, Commit_Summary_Tasks
 from models.gitModels import Repos
 
 airouter = APIRouter()
@@ -20,6 +20,25 @@ 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 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):
     response = Application.call(
         # 若没有配置环境变量,可用百炼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):
     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")
 async def scan(request: RequestBody, background_tasks: BackgroundTasks):
     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_id = repo.id
     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)
     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": "添加提交任务成功"}
+
+
+
+
 

+ 10 - 0
models/aiModels.py

@@ -12,5 +12,15 @@ class Scan_Tasks(Model):
     create_user = fields.CharField(max_length=36)
     repo_hash = fields.CharField(max_length=40)
 
+class Commit_Summary_Tasks(Model):
+    id = fields.IntField(pk=True)
+    create_user = fields.CharField(max_length=36)
+    create_time = fields.BigIntField()
+    start_time = fields.BigIntField()
+    end_time = fields.BigIntField()
+    result = fields.JSONField()
+    repo_id = fields.IntField()
+    repo_hash = fields.CharField(max_length=40)
+