예제 #1
0
 async def receive(self, text_data):
     try:
         text_data_json = json.loads(text_data)
     except json.decoder.JSONDecodeError:
         await self.send(text_data=json.dumps({
             "success": False,
             "error": "Illegal data type"
         }))
         return False
     while True:
         token = text_data_json.get('token')
         project_id = text_data_json.get('project_id')
         branch_name = text_data_json.get('branch_name')
         if not all([token, project_id, branch_name]):
             await self.send(
                 text_data=json.dumps({
                     "success": False,
                     "error": "Missing required parameters"
                 }))
             return False
         else:
             private_token = decrypt_token(token)
             cache_key = "private_token:" + private_token + "-" + "project_id:" + str(
                 project_id) + "-" + "branch_name:" + branch_name
             branch_status = cache.get(cache_key)
             progress = cache.get(cache_key + "progress")
             if progress == 100:
                 cache.set(cache_key + "progress", 0, 1)
             if not branch_status:
                 await asyncio.sleep(2)
                 # 缓存中没有值 从mysql读取
                 gitlab_info = GitlabModel.objects.filter(
                     private_token=private_token).first()
                 branch_status = GitCaseModel.objects.filter(
                     gitlab_url=gitlab_info.gitlab_url,
                     gitlab_project_id=project_id,
                     branch_name=branch_name).first().status
             response_data = {
                 "project_id": project_id,
                 "branch_name": branch_name,
                 "status": branch_status,
                 "progress": progress
             }
             await self.send(text_data=json.dumps(response_data))
             await asyncio.sleep(2)
예제 #2
0
 def post(self, request):
     '''项目下所有分支'''
     encrypt_token = request.data.get('token')
     project_id = request.data.get('project_id')
     if not all([encrypt_token, project_id]):
         return Response(status=status.HTTP_400_BAD_REQUEST)
     _decrypt_token = decrypt_token(encrypt_token)
     gitlab_info = GitlabModel.objects.filter(
         private_token=_decrypt_token).first()
     if gitlab_info:
         instance = GitlabAPI(gitlab_url=gitlab_info.gitlab_url,
                              private_token=gitlab_info.private_token)
         project = instance.gl.projects.get(project_id)
         branches = project.branches.list()
         branches_list = [branch.name for branch in branches]
         return Response({"success": True, "result": branches_list})
     else:
         return Response(status=status.HTTP_404_NOT_FOUND)
예제 #3
0
 def post(self, request):
     '''pull指定分支'''
     encrypt_token = request.data.get('token')
     project_id = request.data.get('project_id')
     branch_name = request.data.get('branch')
     if not all([encrypt_token, project_id]):
         return Response(status=status.HTTP_400_BAD_REQUEST)
     _decrypt_token = decrypt_token(encrypt_token)
     gitlab_info = GitlabModel.objects.filter(
         private_token=_decrypt_token).first()
     serializer = GitlabAuthenticationSerializer(instance=gitlab_info,
                                                 many=False)
     if gitlab_info:  # gitlab token存在于数据库中
         branch_instance = GitCaseModel.objects.filter(
             gitlab_url=gitlab_info.gitlab_url,
             gitlab_project_id=project_id,
             branch_name=branch_name).first()
         if not branch_instance or branch_instance.status != BranchState.PULLING:
             branch_pull.delay(serializer.data, project_id, branch_name)
         return Response({'success': True, 'pull_status': 'STARTING'})
     else:
         # gitlab token不存在数据库
         return Response(status=status.HTTP_404_NOT_FOUND)