|
|
|
@ -26,14 +26,25 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
job_name = "#{user_name}-#{rep_id}"
|
|
|
|
|
sonar_name = "#{user_name}:#{rep_id}"
|
|
|
|
|
|
|
|
|
|
# 考虑到历史数据:有些用户创建类job但是build失败,即sonar没有结果,这个时候需要把job删除,并且删掉quality_analyses表数据
|
|
|
|
|
# 如果不要这句则需要迁移数据
|
|
|
|
|
@sonar_address = Redmine::Configuration['sonar_address']
|
|
|
|
|
projects_date = open(@sonar_address + "/api/projects/index").read
|
|
|
|
|
arr = JSON.parse(projects_date).map {|m| m["nm"]} # eg: ["Hjqreturn:cc_rep", "Hjqreturn:putong", "Hjqreturn:sonar_rep2", "shitou:sonar_rep"]
|
|
|
|
|
quality_an = QualityAnalysis.where(:sonar_name => sonar_name).first
|
|
|
|
|
if @client.job.exists?(job_name) && QualityAnalysis.where(:sonar_name => sonar_name).select{|qa| arr.include?(qa.sonar_name)}.blank?
|
|
|
|
|
logger.info("88888888888888888888")
|
|
|
|
|
aa = @client.job.delete("#{job_name}")
|
|
|
|
|
quality_an.delete unless quality_an.blank?
|
|
|
|
|
end
|
|
|
|
|
# Checks if the given job exists in Jenkins.
|
|
|
|
|
unless @client.job.exists?(job_name)
|
|
|
|
|
@g = Gitlab.client
|
|
|
|
|
branch = params[:branch]
|
|
|
|
|
language = swith_language_type(params[:language])
|
|
|
|
|
path = params[:path].blank? ? "./" : params[:path]
|
|
|
|
|
qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first
|
|
|
|
|
version = qa.nil? ? 1 : qa.sonar_version + 1
|
|
|
|
|
# qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first
|
|
|
|
|
version = quality_an.nil? ? 1 : quality_an.sonar_version + 1
|
|
|
|
|
properties = "sonar.projectKey=#{sonar_name}
|
|
|
|
|
sonar.projectName=#{sonar_name}
|
|
|
|
|
sonar.projectVersion=#{version}
|
|
|
|
@ -61,7 +72,7 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
# 判断调用sonar分析是否成功
|
|
|
|
|
# 等待启动时间处理, 最长时间为30分钟
|
|
|
|
|
for i in 0..60 do
|
|
|
|
|
sleep(60)
|
|
|
|
|
sleep(30)
|
|
|
|
|
@current_build_status = @client.job.get_current_build_status("#{job_name}")
|
|
|
|
|
if (@current_build_status != "not_run" || @current_build_status != "running")
|
|
|
|
|
break
|
|
|
|
@ -72,25 +83,49 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text')
|
|
|
|
|
# 获取sonar output结果
|
|
|
|
|
console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text')["output"]
|
|
|
|
|
logger.info("@current_build_status is ==> #{@current_build_status}")
|
|
|
|
|
logger.info("@console_build is ==> #{@console_build}")
|
|
|
|
|
|
|
|
|
|
d = @client.job.delete("#{job_name}") if jenkins_job == '200' && code != '201'
|
|
|
|
|
logger.error("delete result ==> #{code}")
|
|
|
|
|
if qa.blank? && @current_build_status == "success"
|
|
|
|
|
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier,
|
|
|
|
|
:sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => "#{user_name}:#{rep_id}")
|
|
|
|
|
# 两种情况需要删除job:
|
|
|
|
|
# 1/创建成功但是build失败则删除job
|
|
|
|
|
# 2/creat和build成功,调用sonar启动失败则删除job
|
|
|
|
|
# 错误信息存储需存到Trustie数据库,否则一旦job删除则无法获取这些信息
|
|
|
|
|
if jenkins_job == '200' && code != '201'
|
|
|
|
|
@client.job.delete("#{job_name}")
|
|
|
|
|
else
|
|
|
|
|
qa.update_attribute(:sonar_version, version)
|
|
|
|
|
if @current_build_status == "failure"
|
|
|
|
|
reg_console = /Exception:.*?\r/.match(console_build)
|
|
|
|
|
output = reg_console[0].gsub("\r", "") unless reg_console.nil?
|
|
|
|
|
se = SonarError.where(:jenkins_job_name => job_name).first
|
|
|
|
|
se.nil? ? SonarError.create(:project_id => @project.id, :jenkins_job_name => job_name, :output => output) : se.update_column(:output, output)
|
|
|
|
|
@client.job.delete("#{job_name}")
|
|
|
|
|
elsif @current_build_status == "success"
|
|
|
|
|
if quality_an.blank?
|
|
|
|
|
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier,
|
|
|
|
|
:sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => "#{user_name}:#{rep_id}")
|
|
|
|
|
else
|
|
|
|
|
qa.update_attribute(:sonar_version, version)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue => e
|
|
|
|
|
puts e
|
|
|
|
|
end
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch, :current_build_status => @current_build_status, :job_name => job_name)}
|
|
|
|
|
# format.js{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch)}
|
|
|
|
|
if @current_build_status == "success"
|
|
|
|
|
format.html{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch, :current_build_status => @current_build_status, :job_name => job_name)}
|
|
|
|
|
elsif @current_build_status == "failure"
|
|
|
|
|
format.html{redirect_to error_list_project_quality_analysi_path(:project_id => @project.id, :job_name => job_name)}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def error_list
|
|
|
|
|
@error_list = SonarError.where(:jenkins_job_name => params[:job_name]).first
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
@ -145,7 +180,7 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
get_current_build_status = @client.job.get_current_build_status("Hjqreturn-1280")
|
|
|
|
|
logger.error("Failed to update job: ==> #{jenkins_job}") unless jenkins_job == '200'
|
|
|
|
|
|
|
|
|
|
# 数据更新到Trustie数据库
|
|
|
|
|
# 数据更新到Trustie数据
|
|
|
|
|
if jenkins_job == '200'
|
|
|
|
|
logger.info("quality_ananlysis will be updated: ==> #{jenkins_job}")
|
|
|
|
|
@quality_analysis.path = path
|
|
|
|
@ -168,18 +203,12 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
@branch = params[:branch]
|
|
|
|
|
@resource_id = params[:resource_id]
|
|
|
|
|
@sonar_address = Redmine::Configuration['sonar_address']
|
|
|
|
|
@jenkins_address = Redmine::Configuration['jenkins_address']
|
|
|
|
|
if params[:resource_id].nil?
|
|
|
|
|
@name_flag = true
|
|
|
|
|
projects_date = open(@sonar_address + "/api/projects/index").read
|
|
|
|
|
arr = JSON.parse(projects_date).map {|m| m["nm"]} # eg: ["Hjqreturn:cc_rep", "Hjqreturn:putong", "Hjqreturn:sonar_rep2", "shitou:sonar_rep"]
|
|
|
|
|
@quality_analyses = QualityAnalysis.where(:project_id => @project.id).select{|qa| arr.include?(qa.sonar_name)}
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
if params[:current_build_status] == "failure"
|
|
|
|
|
job_name = params[:job_name]
|
|
|
|
|
@console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text')["output"]
|
|
|
|
|
end
|
|
|
|
|
complexity_date = open(@sonar_address + "/api/resources/index?resource=#{@resource_id}&depth=0&metrics=sqale_rating,function_complexity,duplicated_lines_density,comment_lines_density,sqale_index,lines,file_line,files,functions,classes,directories").read
|
|
|
|
|
@complexity =JSON.parse(complexity_date).first
|
|
|
|
|
issue_date = open(@sonar_address + "/api/resources/index?resource=#{@resource_id}&depth=0&metrics=blocker_violations,critical_violations,major_violations,minor_violations,info_violations,violations").read
|
|
|
|
@ -188,7 +217,6 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
rescue => e
|
|
|
|
|
puts e
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Find project of id params[:project_id]
|
|
|
|
|