Skip to content

Commit eff1b3a

Browse files
authored
Merge pull request kuafuai#125 from kuafuai/feat/create
Feat/create
2 parents 5e3820c + 0bac4ed commit eff1b3a

55 files changed

Lines changed: 1030 additions & 210 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/app/controllers/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from .setting import bp as setting_bp
1111
from .tenant_pro import bp as tenant_bp
1212
from .pay_pro import bp as pay_bp
13+
from .plugine_api import bp as plugine_bp
14+
from .tencent_pro import bp as tencent_bp
1315

1416
def register_controllers(app):
1517
app.register_blueprint(user_bp)
@@ -23,4 +25,6 @@ def register_controllers(app):
2325
app.register_blueprint(step_devops_bp)
2426
app.register_blueprint(setting_bp)
2527
app.register_blueprint(tenant_bp)
26-
app.register_blueprint(pay_bp)
28+
app.register_blueprint(pay_bp)
29+
app.register_blueprint(plugine_bp)
30+
app.register_blueprint(tencent_bp)

backend/app/controllers/app.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ def add():
3636
ApplicationService.delete_service_by_app_id(app_id)
3737
appID = app_id
3838
else:
39-
app = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch, git_config, ci_config, cd_config)
39+
app, success = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch, git_config, ci_config, cd_config)
40+
if not success:
41+
raise Exception(app)
4042
appID = app.app_id
4143

4244
for service in services:
4345
if "service_name" in service:
44-
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"])
46+
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"])
4547

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

backend/app/controllers/common.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from functools import wraps
22
import traceback
33
from flask import jsonify
4+
from flask_limiter import RateLimitExceeded
5+
from app.pkgs.analyzer_code_exception import AnalyzerCodeException, AnalyzerCodeProcessException
6+
47

58
def json_response(func):
69
@wraps(func) # Preserve the original function's name and docstring
@@ -11,12 +14,38 @@ def decorated_function(*args, **kwargs):
1114
'success': True,
1215
'data': result
1316
}
17+
except RateLimitExceeded as e:
18+
response = {
19+
'success': False,
20+
'data': {
21+
'message': str(e)
22+
}
23+
}
24+
except AnalyzerCodeException as e:
25+
response = {
26+
'success': False,
27+
'data': {
28+
'message': str(e),
29+
'error_code': e.error_code
30+
}
31+
}
32+
except AnalyzerCodeProcessException as e:
33+
response = {
34+
'success': False,
35+
'data': {
36+
'message': str(e),
37+
'error_code': e.error_code,
38+
'task_no': e.task_no,
39+
'repo': e.repo
40+
}
41+
}
1442
except Exception as e:
1543
response = {
1644
'success': False,
1745
'error': str(e)
1846
}
1947
traceback.print_exc()
48+
2049
return jsonify(response)
2150

22-
return decorated_function
51+
return decorated_function
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import json
2+
from app.controllers.common import json_response
3+
from app.flask_ext import limiter_ip
4+
from flask import Blueprint, request
5+
from app.pkgs.tools.i18b import getI18n
6+
from app.models.async_task import AsyncTask
7+
from app.pkgs.analyzer_code_exception import AnalyzerCodeException, AnalyzerCodeProcessException
8+
9+
bp = Blueprint('plugine', __name__, url_prefix='/plugine')
10+
11+
12+
@bp.route('/repo_analyzer', methods=['GET'])
13+
@json_response
14+
@limiter_ip.limit("1 per 5 second")
15+
def repo_analyzer_plugine():
16+
_ = getI18n("controllers")
17+
18+
ip = str(request.headers.get("X-Forwarded-For", '127.0.0.1'))
19+
type = request.args.get("type")
20+
repo = request.args.get("repo")
21+
if type is None or repo is None:
22+
raise Exception("param error")
23+
if len(type) == 0 or len(repo) == 0:
24+
raise Exception("param error")
25+
26+
count = AsyncTask.get_today_analyzer_code_count(ip, AsyncTask.Search_Process_Key)
27+
if count > 0:
28+
process_task = AsyncTask.get_today_analyzer_code_list(ip, AsyncTask.Search_Process_Key)
29+
if process_task:
30+
content = json.loads(process_task.task_content)
31+
repo = content['repo']
32+
task_no = process_task.token
33+
raise AnalyzerCodeProcessException("There are currently tasks being processed, please wait...", 1001, task_no, repo)
34+
raise AnalyzerCodeException("There are currently tasks being processed, please wait...", 1001)
35+
36+
count = AsyncTask.get_today_analyzer_code_count(ip, AsyncTask.Search_Done_key)
37+
if count >= 3:
38+
raise AnalyzerCodeException("The analysis frequency for today has been used up. You can register for use on the platform", 3001)
39+
40+
data = {"type": type, "repo": repo}
41+
42+
task = AsyncTask.create_task(AsyncTask.Type_Analyzer_Code, type + ":" + repo, json.dumps(data), ip)
43+
if task:
44+
return {"task_no": task.token}
45+
else:
46+
raise AnalyzerCodeException("Server exception, please contact the administrator", 5001)
47+
48+
49+
@bp.route('/repo_analyzer_check', methods=['GET'])
50+
@json_response
51+
def repo_analyzer_check():
52+
task_no = request.args.get("task_no")
53+
if task_no is None or len(task_no) == 0:
54+
raise Exception("param error")
55+
56+
task = AsyncTask.get_task_by_token(task_no)
57+
if task:
58+
return {"task_no": task.token, "status": task.task_status, "message": task.task_status_message}
59+
else:
60+
raise Exception("查询数据不存在")

backend/app/controllers/step_code.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def check_file():
5050
@json_response
5151
def merge_file():
5252
_ = getI18n("controllers")
53-
baseCode = request.json.get('base_code')
53+
baseCode = request.json.get('old_code')
5454
newCode = request.json.get('new_code')
5555
fileTask = request.json.get('file_task')
5656
userName = storage.get("username")

backend/app/controllers/step_subtask.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
from app.pkgs.tools.i18b import getI18n
66
from flask import Blueprint
77
from app.pkgs.prompt.prompt import splitTask, splitTaskDo
8-
from app.pkgs.knowledge.app_info import getServiceBasePrompt, getServiceIntro, getServiceLib, getServiceStruct
8+
from app.pkgs.knowledge.app_info import getServiceBasePrompt, getServiceInfo, getServiceIntro, getServiceLib, getServiceStruct
99
from app.models.requirement import Requirement
1010
from app.models.application_service import ApplicationService
11+
from app.pkgs.tools.file_tool import get_base_path, get_ws_path
1112

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

@@ -40,11 +41,12 @@ def analysis():
4041
newfeature = requirementDoc
4142

4243
appBasePrompt, _ = getServiceBasePrompt(req["app_id"], serviceName)
43-
projectInfo, _ = getServiceIntro(req["app_id"], serviceName, tenantID)
44+
projectIntro, _ = getServiceIntro(req["app_id"], serviceName, tenantID)
4445
projectLib, _ = getServiceLib(req["app_id"], serviceName)
4546
serviceStruct, _ = getServiceStruct(req["app_id"], serviceName)
47+
projectInfo, _ = getServiceInfo(req["app_id"], serviceName, tenantID)
4648

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

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

67-
filesToEdit, success = splitTaskDo(req_info, service_info, tec_doc)
69+
filesToEdit, success = splitTaskDo(req_info, service_info, tec_doc, tenant_id)
6870

71+
git_path = service_info["git_path"]
72+
bath_path = get_base_path(task_id, git_path)
6973
if success and filesToEdit:
7074
for index, file in enumerate(filesToEdit):
7175
file_path = file["file-path"] if 'file-path' in file else file["file_path"]
72-
isSuccess, oldCode = getFileContent(file_path, service_name)
76+
isSuccess, oldCode = getFileContent(file_path, bath_path)
7377
filesToEdit[index]["old-code"] = oldCode
7478
if not isSuccess:
7579
filesToEdit[index]["old-code"] = ''
7680

7781
reference_file = file["reference-file"] if 'reference-file' in file else ''
78-
isSuccess, referenceCode = getFileContent(reference_file, service_name)
82+
isSuccess, referenceCode = getFileContent(reference_file, bath_path)
7983
filesToEdit[index]["reference-code"] = referenceCode
8084
if not isSuccess:
8185
filesToEdit[index]["reference-code"] = ''
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from flask import Blueprint
2+
3+
bp = Blueprint('tencent', __name__, url_prefix='/tencent')
4+
5+
def test():
6+
pass

backend/app/flask_ext.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from flask_limiter import Limiter
2+
from flask import request
3+
from flask_limiter.util import get_remote_address
4+
5+
6+
def limit_ip_func():
7+
ip = str(request.headers.get("X-Forwarded-For", '127.0.0.1'))
8+
remote_ip = get_remote_address()
9+
10+
print("limit ip : ", ip, remote_ip)
11+
12+
if ip != '127.0.0.1':
13+
return ip
14+
15+
if remote_ip != '127.0.0.1':
16+
return remote_ip
17+
18+
return '127.0.0.1'
19+
20+
21+
limiter_ip = Limiter(key_func=limit_ip_func)

backend/app/models/application.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ def create(tenant_id, creater, name, description, default_source_branch, default
1717
if not tenant_id:
1818
tenant_id = 0
1919

20+
if len(name) < 2:
21+
return "The name field cannot be empty", False
22+
if len(description) < 2:
23+
return "The description field cannot be empty", False
24+
2025
app = Application(
2126
tenant_id=tenant_id,
2227
creater=creater,
@@ -30,7 +35,7 @@ def create(tenant_id, creater, name, description, default_source_branch, default
3035
)
3136
db.session.add(app)
3237
db.session.commit()
33-
return app
38+
return app, True
3439

3540
@staticmethod
3641
def get_all_application(tenant_id, appID):

backend/app/models/application_service.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class ApplicationService(db.Model):
2222
cd_public_ip = db.Column(db.String(50))
2323
cd_security_group = db.Column(db.String(100))
2424
cd_subnet = db.Column(db.String(100))
25+
cd_subnet2 = db.Column(db.String(100))
26+
cd_vpc = db.Column(db.String(100))
27+
cd_execution_role_arn = db.Column(db.String(200))
2528
cd_default_image = db.Column(db.String(200))
2629
created_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP'))
2730
updated_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP'))
@@ -32,7 +35,7 @@ class ApplicationService(db.Model):
3235
LANGUAGE_JAVA = "Java"
3336

3437
def create_service(app_id, name, git_path, git_workflow, role, language, framework, database_type, api_type, api_location,
35-
cd_container_name, cd_container_group, cd_region, cd_public_ip, cd_security_group, cd_subnet, struct_cache, cd_default_image="", service_type=""):
38+
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=""):
3639
service = ApplicationService(
3740
app_id=app_id,
3841
name=name,
@@ -53,7 +56,10 @@ def create_service(app_id, name, git_path, git_workflow, role, language, framewo
5356
cd_security_group=cd_security_group,
5457
cd_subnet=cd_subnet,
5558
cd_default_image=cd_default_image,
56-
struct_cache=struct_cache
59+
struct_cache=struct_cache,
60+
cd_subnet2=cd_subnet2,
61+
cd_vpc=cd_vpc,
62+
cd_execution_role_arn=cd_execution_role_arn,
5763
)
5864
db.session.add(service)
5965
db.session.commit()
@@ -93,6 +99,9 @@ def get_service_by_name(appID, service_name):
9399
'cd_public_ip': service.cd_public_ip,
94100
'cd_security_group': service.cd_security_group,
95101
'cd_subnet': service.cd_subnet,
102+
'cd_subnet2': service.cd_subnet2,
103+
'cd_vpc': service.cd_vpc,
104+
'cd_execution_role_arn': service.cd_execution_role_arn,
96105
'cd_default_image': service.cd_default_image,
97106
'struct_cache': service.struct_cache,
98107
'libs': ApplicationServiceLib.get_libs_by_service_id(service.service_id)
@@ -155,6 +164,9 @@ def get_services_by_app_id(cls, app_id):
155164
'cd_subnet': service.cd_subnet,
156165
'cd_default_image': service.cd_default_image,
157166
'struct_cache': service.struct_cache,
167+
'cd_subnet2': service.cd_subnet2,
168+
'cd_vpc': service.cd_vpc,
169+
'cd_execution_role_arn': service.cd_execution_role_arn,
158170
'libs': ApplicationServiceLib.get_libs_by_service_id(service.service_id)
159171
}
160172
services_list.append(service_dict)

0 commit comments

Comments
 (0)