Skip to content

Commit

Permalink
Merge pull request #125 from kuafuai/feat/create
Browse files Browse the repository at this point in the history
Feat/create
  • Loading branch information
yakeJiang authored Nov 14, 2023
2 parents 5e3820c + 0bac4ed commit eff1b3a
Show file tree
Hide file tree
Showing 55 changed files with 1,030 additions and 210 deletions.
6 changes: 5 additions & 1 deletion backend/app/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from .setting import bp as setting_bp
from .tenant_pro import bp as tenant_bp
from .pay_pro import bp as pay_bp
from .plugine_api import bp as plugine_bp
from .tencent_pro import bp as tencent_bp

def register_controllers(app):
app.register_blueprint(user_bp)
Expand All @@ -23,4 +25,6 @@ def register_controllers(app):
app.register_blueprint(step_devops_bp)
app.register_blueprint(setting_bp)
app.register_blueprint(tenant_bp)
app.register_blueprint(pay_bp)
app.register_blueprint(pay_bp)
app.register_blueprint(plugine_bp)
app.register_blueprint(tencent_bp)
6 changes: 4 additions & 2 deletions backend/app/controllers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ def add():
ApplicationService.delete_service_by_app_id(app_id)
appID = app_id
else:
app = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch, git_config, ci_config, cd_config)
app, success = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch, git_config, ci_config, cd_config)
if not success:
raise Exception(app)
appID = app.app_id

for service in services:
if "service_name" in service:
newService = ApplicationService.create_service(appID, service["service_name"], service["service_git_path"], service["service_workflow"], service["service_role"], service["service_language"], service["service_framework"], service["service_database"], service["service_api_type"], service["service_api_location"], service["service_container_name"], service["service_container_group"], service["service_region"], '', service["service_security_group"], service["service_cd_subnet"], service["service_struct_cache"], '', service["service_service_type"])
newService = ApplicationService.create_service(appID, service["service_name"], service["service_git_path"], service["service_workflow"], service["service_role"], service["service_language"], service["service_framework"], service["service_database"], service["service_api_type"], service["service_api_location"], service["service_container_name"], service["service_container_group"], service["service_region"], '', service["service_security_group"], service["service_cd_subnet"], service["service_struct_cache"], '', service["service_service_type"], service["service_cd_subnet2"], service["service_cd_execution_role_arn"], service["service_cd_vpc"])

ApplicationServiceLib.create_libs(newService.service_id, service["service_libs_name"])

Expand Down
31 changes: 30 additions & 1 deletion backend/app/controllers/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from functools import wraps
import traceback
from flask import jsonify
from flask_limiter import RateLimitExceeded
from app.pkgs.analyzer_code_exception import AnalyzerCodeException, AnalyzerCodeProcessException


def json_response(func):
@wraps(func) # Preserve the original function's name and docstring
Expand All @@ -11,12 +14,38 @@ def decorated_function(*args, **kwargs):
'success': True,
'data': result
}
except RateLimitExceeded as e:
response = {
'success': False,
'data': {
'message': str(e)
}
}
except AnalyzerCodeException as e:
response = {
'success': False,
'data': {
'message': str(e),
'error_code': e.error_code
}
}
except AnalyzerCodeProcessException as e:
response = {
'success': False,
'data': {
'message': str(e),
'error_code': e.error_code,
'task_no': e.task_no,
'repo': e.repo
}
}
except Exception as e:
response = {
'success': False,
'error': str(e)
}
traceback.print_exc()

return jsonify(response)

return decorated_function
return decorated_function
60 changes: 60 additions & 0 deletions backend/app/controllers/plugine_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import json
from app.controllers.common import json_response
from app.flask_ext import limiter_ip
from flask import Blueprint, request
from app.pkgs.tools.i18b import getI18n
from app.models.async_task import AsyncTask
from app.pkgs.analyzer_code_exception import AnalyzerCodeException, AnalyzerCodeProcessException

bp = Blueprint('plugine', __name__, url_prefix='/plugine')


@bp.route('/repo_analyzer', methods=['GET'])
@json_response
@limiter_ip.limit("1 per 5 second")
def repo_analyzer_plugine():
_ = getI18n("controllers")

ip = str(request.headers.get("X-Forwarded-For", '127.0.0.1'))
type = request.args.get("type")
repo = request.args.get("repo")
if type is None or repo is None:
raise Exception("param error")
if len(type) == 0 or len(repo) == 0:
raise Exception("param error")

count = AsyncTask.get_today_analyzer_code_count(ip, AsyncTask.Search_Process_Key)
if count > 0:
process_task = AsyncTask.get_today_analyzer_code_list(ip, AsyncTask.Search_Process_Key)
if process_task:
content = json.loads(process_task.task_content)
repo = content['repo']
task_no = process_task.token
raise AnalyzerCodeProcessException("There are currently tasks being processed, please wait...", 1001, task_no, repo)
raise AnalyzerCodeException("There are currently tasks being processed, please wait...", 1001)

count = AsyncTask.get_today_analyzer_code_count(ip, AsyncTask.Search_Done_key)
if count >= 3:
raise AnalyzerCodeException("The analysis frequency for today has been used up. You can register for use on the platform", 3001)

data = {"type": type, "repo": repo}

task = AsyncTask.create_task(AsyncTask.Type_Analyzer_Code, type + ":" + repo, json.dumps(data), ip)
if task:
return {"task_no": task.token}
else:
raise AnalyzerCodeException("Server exception, please contact the administrator", 5001)


@bp.route('/repo_analyzer_check', methods=['GET'])
@json_response
def repo_analyzer_check():
task_no = request.args.get("task_no")
if task_no is None or len(task_no) == 0:
raise Exception("param error")

task = AsyncTask.get_task_by_token(task_no)
if task:
return {"task_no": task.token, "status": task.task_status, "message": task.task_status_message}
else:
raise Exception("查询数据不存在")
2 changes: 1 addition & 1 deletion backend/app/controllers/step_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def check_file():
@json_response
def merge_file():
_ = getI18n("controllers")
baseCode = request.json.get('base_code')
baseCode = request.json.get('old_code')
newCode = request.json.get('new_code')
fileTask = request.json.get('file_task')
userName = storage.get("username")
Expand Down
16 changes: 10 additions & 6 deletions backend/app/controllers/step_subtask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from app.pkgs.tools.i18b import getI18n
from flask import Blueprint
from app.pkgs.prompt.prompt import splitTask, splitTaskDo
from app.pkgs.knowledge.app_info import getServiceBasePrompt, getServiceIntro, getServiceLib, getServiceStruct
from app.pkgs.knowledge.app_info import getServiceBasePrompt, getServiceInfo, getServiceIntro, getServiceLib, getServiceStruct
from app.models.requirement import Requirement
from app.models.application_service import ApplicationService
from app.pkgs.tools.file_tool import get_base_path, get_ws_path

bp = Blueprint('step_subtask', __name__, url_prefix='/step_subtask')

Expand Down Expand Up @@ -40,11 +41,12 @@ def analysis():
newfeature = requirementDoc

appBasePrompt, _ = getServiceBasePrompt(req["app_id"], serviceName)
projectInfo, _ = getServiceIntro(req["app_id"], serviceName, tenantID)
projectIntro, _ = getServiceIntro(req["app_id"], serviceName, tenantID)
projectLib, _ = getServiceLib(req["app_id"], serviceName)
serviceStruct, _ = getServiceStruct(req["app_id"], serviceName)
projectInfo, _ = getServiceInfo(req["app_id"], serviceName, tenantID)

subtask, success = splitTask(requirementID, newfeature, serviceName, appBasePrompt, projectInfo, projectLib, serviceStruct, req["app_id"])
subtask, success = splitTask(projectInfo, requirementID, newfeature, serviceName, appBasePrompt, projectIntro, projectLib, serviceStruct, req["app_id"], tenantID)

if success and subtask:
return {'message': subtask, 'service_name': serviceName}
Expand All @@ -64,18 +66,20 @@ def task_split():
req_info = Requirement.get_requirement_by_id(task_id, tenant_id)
service_info = ApplicationService.get_service_by_name(req_info["app_id"], service_name)

filesToEdit, success = splitTaskDo(req_info, service_info, tec_doc)
filesToEdit, success = splitTaskDo(req_info, service_info, tec_doc, tenant_id)

git_path = service_info["git_path"]
bath_path = get_base_path(task_id, git_path)
if success and filesToEdit:
for index, file in enumerate(filesToEdit):
file_path = file["file-path"] if 'file-path' in file else file["file_path"]
isSuccess, oldCode = getFileContent(file_path, service_name)
isSuccess, oldCode = getFileContent(file_path, bath_path)
filesToEdit[index]["old-code"] = oldCode
if not isSuccess:
filesToEdit[index]["old-code"] = ''

reference_file = file["reference-file"] if 'reference-file' in file else ''
isSuccess, referenceCode = getFileContent(reference_file, service_name)
isSuccess, referenceCode = getFileContent(reference_file, bath_path)
filesToEdit[index]["reference-code"] = referenceCode
if not isSuccess:
filesToEdit[index]["reference-code"] = ''
Expand Down
6 changes: 6 additions & 0 deletions backend/app/controllers/tencent_pro.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from flask import Blueprint

bp = Blueprint('tencent', __name__, url_prefix='/tencent')

def test():
pass
21 changes: 21 additions & 0 deletions backend/app/flask_ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from flask_limiter import Limiter
from flask import request
from flask_limiter.util import get_remote_address


def limit_ip_func():
ip = str(request.headers.get("X-Forwarded-For", '127.0.0.1'))
remote_ip = get_remote_address()

print("limit ip : ", ip, remote_ip)

if ip != '127.0.0.1':
return ip

if remote_ip != '127.0.0.1':
return remote_ip

return '127.0.0.1'


limiter_ip = Limiter(key_func=limit_ip_func)
7 changes: 6 additions & 1 deletion backend/app/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def create(tenant_id, creater, name, description, default_source_branch, default
if not tenant_id:
tenant_id = 0

if len(name) < 2:
return "The name field cannot be empty", False
if len(description) < 2:
return "The description field cannot be empty", False

app = Application(
tenant_id=tenant_id,
creater=creater,
Expand All @@ -30,7 +35,7 @@ def create(tenant_id, creater, name, description, default_source_branch, default
)
db.session.add(app)
db.session.commit()
return app
return app, True

@staticmethod
def get_all_application(tenant_id, appID):
Expand Down
16 changes: 14 additions & 2 deletions backend/app/models/application_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class ApplicationService(db.Model):
cd_public_ip = db.Column(db.String(50))
cd_security_group = db.Column(db.String(100))
cd_subnet = db.Column(db.String(100))
cd_subnet2 = db.Column(db.String(100))
cd_vpc = db.Column(db.String(100))
cd_execution_role_arn = db.Column(db.String(200))
cd_default_image = db.Column(db.String(200))
created_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP'))
updated_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP'))
Expand All @@ -32,7 +35,7 @@ class ApplicationService(db.Model):
LANGUAGE_JAVA = "Java"

def create_service(app_id, name, git_path, git_workflow, role, language, framework, database_type, api_type, api_location,
cd_container_name, cd_container_group, cd_region, cd_public_ip, cd_security_group, cd_subnet, struct_cache, cd_default_image="", service_type=""):
cd_container_name, cd_container_group, cd_region, cd_public_ip, cd_security_group, cd_subnet, struct_cache, cd_default_image="", service_type="", cd_subnet2="", cd_execution_role_arn="", cd_vpc=""):
service = ApplicationService(
app_id=app_id,
name=name,
Expand All @@ -53,7 +56,10 @@ def create_service(app_id, name, git_path, git_workflow, role, language, framewo
cd_security_group=cd_security_group,
cd_subnet=cd_subnet,
cd_default_image=cd_default_image,
struct_cache=struct_cache
struct_cache=struct_cache,
cd_subnet2=cd_subnet2,
cd_vpc=cd_vpc,
cd_execution_role_arn=cd_execution_role_arn,
)
db.session.add(service)
db.session.commit()
Expand Down Expand Up @@ -93,6 +99,9 @@ def get_service_by_name(appID, service_name):
'cd_public_ip': service.cd_public_ip,
'cd_security_group': service.cd_security_group,
'cd_subnet': service.cd_subnet,
'cd_subnet2': service.cd_subnet2,
'cd_vpc': service.cd_vpc,
'cd_execution_role_arn': service.cd_execution_role_arn,
'cd_default_image': service.cd_default_image,
'struct_cache': service.struct_cache,
'libs': ApplicationServiceLib.get_libs_by_service_id(service.service_id)
Expand Down Expand Up @@ -155,6 +164,9 @@ def get_services_by_app_id(cls, app_id):
'cd_subnet': service.cd_subnet,
'cd_default_image': service.cd_default_image,
'struct_cache': service.struct_cache,
'cd_subnet2': service.cd_subnet2,
'cd_vpc': service.cd_vpc,
'cd_execution_role_arn': service.cd_execution_role_arn,
'libs': ApplicationServiceLib.get_libs_by_service_id(service.service_id)
}
services_list.append(service_dict)
Expand Down
Loading

0 comments on commit eff1b3a

Please sign in to comment.