149 lines
6.2 KiB
Python
149 lines
6.2 KiB
Python
#-*- coding:utf-8 -*-
|
|
from werkzeug.datastructures import ImmutableMultiDict
|
|
|
|
__author__ = 'qiangge'
|
|
|
|
from flask import Flask, jsonify
|
|
from flask import abort, request
|
|
from flask import make_response
|
|
import re
|
|
import json
|
|
import random
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
questionType = {
|
|
1:"选择题",
|
|
2:"问答题"
|
|
# ,3:"判断题"
|
|
}
|
|
|
|
messageList = ["Refactor this code to not nest more than 3 if/for/while/switch/try statements.",
|
|
"Replace this if-then-else statement by a single return statement.",
|
|
"Merge this if statement with the enclosing one.",
|
|
"Remove the literal true boolean value.",
|
|
"Rename this local variable name to match the regular expression '^[a-z][a-zA-Z0-9]*$'. ",
|
|
"At most one statement is allowed per line, but 2 statements were found on this line.",
|
|
"Move this variable to comply with Java Code Conventions.",
|
|
"Use isEmpty() to check whether the collection is empty or not."]
|
|
|
|
|
|
|
|
path = "json"
|
|
with open(path+"/tasks",'r') as load_f:
|
|
tasks = json.load(load_f)
|
|
with open(path+"/transform",'r') as load_f:
|
|
transform = json.load(load_f)
|
|
|
|
|
|
def getTaskId(message):
|
|
# TODO 从错误信息到taskId的映射
|
|
|
|
results = filter(lambda t: re.match(t["re"],message), transform)
|
|
return [result["id"] for result in results]
|
|
|
|
def genSevrityQuestion(project_id,blob_id,blob_linenum,severity,id,message,function_id,function_linenum):
|
|
templateQustion = {"answer": "",
|
|
"type": 1,
|
|
"question": u"Line:"+repr(blob_linenum)+u"\n该行代码或者代码块可能存在如下潜在风险或者问题,"+"error:"+message+u"\n其严重程度属于下面哪一级?",
|
|
"option": ["BLOCKER — Bug with a high probability to impact the behavior of the application in production",
|
|
"CRITICAL — Either a bug with a low probability toimpact the behavior of the application in production or an issue which represents a security flaw reviewed.",
|
|
"MAJOR — Quality flaw which can highly impact the developer productivity",
|
|
"MINOR — Quality flaw which can slightly impact the developer productivity",
|
|
"INFO — Neither a bug nor a quality flaw, just a finding."],
|
|
# "id":"",
|
|
"project_id":"",
|
|
"blob_id":"",
|
|
"blob_linenum":"",
|
|
"id":""
|
|
}
|
|
templateQustion["blob_linenum"] = blob_linenum
|
|
templateQustion["answer"] = [severity]
|
|
templateQustion["project_id"] = project_id
|
|
templateQustion["blob_id"] = blob_id
|
|
templateQustion["id"] = [id]
|
|
templateQustion["function_id"] = function_id
|
|
templateQustion["function_linenum"] = function_linenum
|
|
return templateQustion
|
|
|
|
def genLocationQuestion(project_id,blob_id,blob_linenum,id,message,function_id,function_linenum):
|
|
templateQustion = {"answer": "",
|
|
"type": 2,
|
|
"question": u"在该段代码中,哪一行代码块存在如下潜在风险或者问题:\nerror:"+message,
|
|
"option": "",
|
|
# "id":"",
|
|
"project_id":"",
|
|
"blob_id":"",
|
|
"blob_linenum":"",
|
|
"id":""
|
|
}
|
|
templateQustion["blob_linenum"] = blob_linenum
|
|
templateQustion["answer"] = [blob_linenum]
|
|
templateQustion["project_id"] = project_id
|
|
templateQustion["blob_id"] = blob_id
|
|
templateQustion["id"] = [id]
|
|
templateQustion["function_id"] = function_id
|
|
templateQustion["function_linenum"] = function_linenum
|
|
return templateQustion
|
|
|
|
def genErrorChoice(project_id,blob_id,blob_linenum,messages,id,function_id,function_linenum):
|
|
templateQustion = {"answer": "",
|
|
"type": 1,
|
|
"question": u"Line:"+repr(blob_linenum)+u"\n该行代码或者代码块可能存在下面哪种潜在风险或者问题?",
|
|
"option": "",
|
|
"project_id": "",
|
|
"blob_id": "",
|
|
"blob_linenum": "",
|
|
"id":""
|
|
}
|
|
tempMessages = messages
|
|
if len(tempMessages) < 4:
|
|
tempMessagesChoice = [i for i in messageList if i not in messages]
|
|
b = random.sample(tempMessagesChoice, 4-len(tempMessages))
|
|
tempMessages = list(set(tempMessages).union(set(b)))
|
|
templateQustion["option"] = tempMessages
|
|
templateQustion["answer"] = messages
|
|
templateQustion["project_id"] = project_id
|
|
templateQustion["blob_id"] = blob_id
|
|
templateQustion["blob_linenum"] = blob_linenum
|
|
templateQustion["id"] = id
|
|
templateQustion["function_id"] = function_id
|
|
templateQustion["function_linenum"] = function_linenum
|
|
return templateQustion
|
|
|
|
|
|
@app.route('/genQustion', methods=['GET','POST'])
|
|
def get_task():
|
|
getJson = request.json
|
|
messages = []
|
|
severities = []
|
|
questions = []
|
|
ids = []
|
|
|
|
project_id = getJson["project_id"]
|
|
blob_linenum = getJson["blob_linenum"]
|
|
blob_id = getJson["blob_id"]
|
|
issues = getJson["issues"]
|
|
function_id = getJson["function_id"]
|
|
function_linenum = getJson["function_linenum"]
|
|
for issue in issues:
|
|
messages.append(issue["message"])
|
|
severities.append(issue["severity"])
|
|
ids.append(issue["id"])
|
|
questions.append(genSevrityQuestion(project_id,blob_id,blob_linenum,issue["severity"],issue["id"],issue["message"],function_id,function_linenum))
|
|
questions.append(genLocationQuestion(project_id,blob_id,blob_linenum,issue["id"],issue["message"],function_id,function_linenum))
|
|
questions.append(genErrorChoice(project_id,blob_id,blob_linenum,messages,ids,function_id,function_linenum))
|
|
|
|
# taskIdList = getTaskId(messages)
|
|
# task = filter(lambda t: t['id'] in taskIdList, tasks)
|
|
if len(questions) == 0:
|
|
abort(404)
|
|
return jsonify({'task': questions})
|
|
|
|
@app.errorhandler(404)
|
|
def not_found(error):
|
|
return make_response(jsonify({'error': 'Not found'}), 404)
|
|
|
|
if __name__ == '__main__':
|
|
app.run(debug=True) |