|
|
@ -16,8 +16,8 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# params 说明:{identifier:版本库名}
|
|
|
|
# params 说明:{identifier:版本库名}
|
|
|
|
|
|
|
|
# type: 1 新的分析 2 重新分析
|
|
|
|
def create
|
|
|
|
def create
|
|
|
|
logger.info("11111111111111111111111111111")
|
|
|
|
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
user_name = User.find(params[:user_id]).try(:login)
|
|
|
|
user_name = User.find(params[:user_id]).try(:login)
|
|
|
|
identifier = params[:identifier]
|
|
|
|
identifier = params[:identifier]
|
|
|
@ -30,98 +30,98 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
# 考虑到历史数据:有些用户创建类job但是build失败,即sonar没有结果,这个时候需要把job删除,并且删掉quality_analyses表数据
|
|
|
|
# 考虑到历史数据:有些用户创建类job但是build失败,即sonar没有结果,这个时候需要把job删除,并且删掉quality_analyses表数据
|
|
|
|
# 如果不要这句则需要迁移数据
|
|
|
|
# 如果不要这句则需要迁移数据
|
|
|
|
@sonar_address = Redmine::Configuration['sonar_address']
|
|
|
|
@sonar_address = Redmine::Configuration['sonar_address']
|
|
|
|
projects_date = open(@sonar_address + "/api/projects/index").read
|
|
|
|
# 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"]
|
|
|
|
# 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
|
|
|
|
quality_an = QualityAnalysis.where(:sonar_name => sonar_name)
|
|
|
|
if @client_jenkins.job.exists?(job_name) && QualityAnalysis.where(:sonar_name => sonar_name).select{|qa| arr.include?(qa.sonar_name)}.blank?
|
|
|
|
# if @client_jenkins.job.exists?(job_name) && QualityAnalysis.where(:sonar_name => sonar_name).select{|qa| arr.include?(qa.sonar_name)}.blank?
|
|
|
|
aa = @client_jenkins.job.delete("#{job_name}")
|
|
|
|
# aa = @client_jenkins.job.delete("#{job_name}")
|
|
|
|
quality_an.delete unless quality_an.blank?
|
|
|
|
# quality_an.delete unless quality_an.blank?
|
|
|
|
end
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# type 1的时候之所以判断job是否存在,为了防止特殊情况,正常情况是不会出现的
|
|
|
|
|
|
|
|
# 重新分析的时候需要删除以前的分析结果
|
|
|
|
|
|
|
|
@client_jenkins.job.delete("#{job_name}") if @client_jenkins.job.exists?(job_name)
|
|
|
|
|
|
|
|
quality_an.delete_all unless quality_an.blank?
|
|
|
|
|
|
|
|
|
|
|
|
# Checks if the given job exists in Jenkins.
|
|
|
|
# Checks if the given job exists in Jenkins.
|
|
|
|
unless @client_jenkins.job.exists?(job_name)
|
|
|
|
@g = Gitlab.client
|
|
|
|
@g = Gitlab.client
|
|
|
|
branch = params[:branch]
|
|
|
|
branch = params[:branch]
|
|
|
|
language = swith_language_type(params[:language])
|
|
|
|
language = swith_language_type(params[:language])
|
|
|
|
path = params[:path].blank? ? "./" : params[:path]
|
|
|
|
path = params[:path].blank? ? "./" : params[:path]
|
|
|
|
# qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first
|
|
|
|
# qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first
|
|
|
|
version = 1
|
|
|
|
version = quality_an.nil? ? 1 : quality_an.sonar_version + 1
|
|
|
|
properties = "sonar.projectKey=#{sonar_name}
|
|
|
|
properties = "sonar.projectKey=#{sonar_name}
|
|
|
|
|
|
|
|
sonar.projectName=#{sonar_name}
|
|
|
|
sonar.projectName=#{sonar_name}
|
|
|
|
sonar.projectVersion=#{version}
|
|
|
|
sonar.projectVersion=#{version}
|
|
|
|
sonar.sources=#{path}
|
|
|
|
sonar.sources=#{path}
|
|
|
|
sonar.language=#{language.downcase}
|
|
|
|
sonar.language=#{language.downcase}
|
|
|
|
sonar.sourceEncoding=utf-8"
|
|
|
|
sonar.sourceEncoding=utf-8"
|
|
|
|
git_url = @gitlab_address.to_s+"/"+@project.owner.to_s+"/"+ identifier + "."+"git"
|
|
|
|
git_url = @gitlab_address.to_s+"/"+@project.owner.to_s+"/"+ identifier + "."+"git"
|
|
|
|
|
|
|
|
|
|
|
|
# 替换配置文件
|
|
|
|
# 替换配置文件
|
|
|
|
@doc = Nokogiri::XML(File.open(File.join(Rails.root, 'tmp', 'config.xml')))
|
|
|
|
@doc = Nokogiri::XML(File.open(File.join(Rails.root, 'tmp', 'config.xml')))
|
|
|
|
@doc.at_xpath("//hudson.plugins.git.UserRemoteConfig/url").content = git_url
|
|
|
|
@doc.at_xpath("//hudson.plugins.git.UserRemoteConfig/url").content = git_url
|
|
|
|
@doc.at_xpath("//hudson.plugins.git.BranchSpec/name").content = "*/#{branch}"
|
|
|
|
@doc.at_xpath("//hudson.plugins.git.BranchSpec/name").content = "*/#{branch}"
|
|
|
|
@doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties # sonar-properties
|
|
|
|
@doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties # sonar-properties
|
|
|
|
|
|
|
|
|
|
|
|
# jenkins job创建
|
|
|
|
# jenkins job创建
|
|
|
|
jenkins_job = @client_jenkins.job.create("#{job_name}", @doc.to_xml)
|
|
|
|
jenkins_job = @client_jenkins.job.create("#{job_name}", @doc.to_xml)
|
|
|
|
logger.info("Jenkins status of create ==> #{jenkins_job}")
|
|
|
|
|
|
|
|
|
|
|
|
# 将地址作为hook值添加到gitlab
|
|
|
|
# 将地址作为hook值添加到gitlab
|
|
|
|
# @g.add_project_hook(@project.gpid, @jenkins_address + "/project/#{job_name}")
|
|
|
|
@g.add_project_hook(@project.gpid, @jenkins_address + "/project/#{job_name}")
|
|
|
|
# job创建完成后自动运行job,如果运行成功则返回‘200’
|
|
|
|
# job创建完成后自动运行job,如果运行成功则返回‘200’
|
|
|
|
code = @client_jenkins.job.build("#{job_name}")
|
|
|
|
code = @client_jenkins.job.build("#{job_name}")
|
|
|
|
|
|
|
|
logger.error("build result ==> #{code}")
|
|
|
|
# 判断调用sonar分析是否成功
|
|
|
|
|
|
|
|
# 等待启动时间处理, 最长时间为30分钟
|
|
|
|
# 判断调用sonar分析是否成功
|
|
|
|
for i in 0..360 do
|
|
|
|
# 等待启动时间处理, 最长时间为30分钟
|
|
|
|
sleep(5)
|
|
|
|
for i in 0..360 do
|
|
|
|
@current_build_status = @client_jenkins.job.get_current_build_status("#{job_name}")
|
|
|
|
sleep(5)
|
|
|
|
if (@current_build_status == "success" || @current_build_status == "failure")
|
|
|
|
@current_build_status = @client_jenkins.job.get_current_build_status("#{job_name}")
|
|
|
|
break
|
|
|
|
if (@current_build_status == "success" || @current_build_status == "failure")
|
|
|
|
if i == 360
|
|
|
|
|
|
|
|
@build_console_result = false
|
|
|
|
break
|
|
|
|
break
|
|
|
|
if i == 360
|
|
|
|
|
|
|
|
@build_console_result = false
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# sonar 缓冲,sonar生成数据
|
|
|
|
# sonar 缓冲,sonar生成数据
|
|
|
|
sleep(10)
|
|
|
|
sleep(10)
|
|
|
|
|
|
|
|
|
|
|
|
# 获取sonar output结果
|
|
|
|
# 获取sonar output结果
|
|
|
|
console_build = @client_jenkins.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text')["output"]
|
|
|
|
console_build = @client_jenkins.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("@current_build_status is ==> #{@current_build_status}")
|
|
|
|
|
|
|
|
|
|
|
|
# 两种情况需要删除job:
|
|
|
|
# 两种情况需要删除job:
|
|
|
|
# 1/创建成功但是build失败则删除job
|
|
|
|
# 1/创建成功但是build失败则删除job
|
|
|
|
# 2/creat和build成功,调用sonar启动失败则删除job
|
|
|
|
# 2/creat和build成功,调用sonar启动失败则删除job
|
|
|
|
# 错误信息存储需存到Trustie数据库,否则一旦job删除则无法获取这些信息
|
|
|
|
# 错误信息存储需存到Trustie数据库,否则一旦job删除则无法获取这些信息
|
|
|
|
if jenkins_job == '200' && code != '201'
|
|
|
|
if jenkins_job == '200' && code != '201'
|
|
|
|
|
|
|
|
@client_jenkins.job.delete("#{job_name}")
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
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_jenkins.job.delete("#{job_name}")
|
|
|
|
@client_jenkins.job.delete("#{job_name}")
|
|
|
|
else
|
|
|
|
elsif @current_build_status == "success"
|
|
|
|
if @current_build_status == "failure"
|
|
|
|
if quality_an.blank?
|
|
|
|
reg_console = /Exception:.*?\r/.match(console_build)
|
|
|
|
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier,
|
|
|
|
output = reg_console[0].gsub("\r", "") unless reg_console.nil?
|
|
|
|
:sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => "#{user_name}:#{rep_id}")
|
|
|
|
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_jenkins.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
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
respond_to do |format|
|
|
|
|
if @current_build_status == "success"
|
|
|
|
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)}
|
|
|
|
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"
|
|
|
|
elsif @current_build_status == "failure"
|
|
|
|
format.html{redirect_to error_list_project_quality_analysi_path(:project_id => @project.id, :job_name => job_name)}
|
|
|
|
format.html{redirect_to error_list_project_quality_analysi_path(:project_id => @project.id, :job_name => job_name)}
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
rescue => e
|
|
|
|
rescue => e
|
|
|
|
@message = e.message
|
|
|
|
@message = e.message
|
|
|
|
logger.error("######################====>#{e.message}")
|
|
|
|
logger.error("######################====>#{e.message}")
|
|
|
@ -302,10 +302,8 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
@jenkins_address = Redmine::Configuration['jenkins_address']
|
|
|
|
@jenkins_address = Redmine::Configuration['jenkins_address']
|
|
|
|
jenkins_username = Redmine::Configuration['jenkins_username']
|
|
|
|
jenkins_username = Redmine::Configuration['jenkins_username']
|
|
|
|
jenkins_password = Redmine::Configuration['jenkins_password']
|
|
|
|
jenkins_password = Redmine::Configuration['jenkins_password']
|
|
|
|
logger.info("22222222222222222222222222222222")
|
|
|
|
|
|
|
|
# connect jenkins
|
|
|
|
# connect jenkins
|
|
|
|
@client_jenkins = JenkinsApi::Client.new(:server_url => @jenkins_address, :username => jenkins_username, :password => jenkins_password)
|
|
|
|
@client_jenkins = JenkinsApi::Client.new(:server_url => @jenkins_address, :username => jenkins_username, :password => jenkins_password)
|
|
|
|
logger.info("333333333333333333333333333333")
|
|
|
|
|
|
|
|
rescue => e
|
|
|
|
rescue => e
|
|
|
|
logger.error("failed to connect Jenkins ==> #{e}")
|
|
|
|
logger.error("failed to connect Jenkins ==> #{e}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|