test jenkins

dev_blankdatabase
huang 9 years ago
parent 1588f5cfca
commit e2557aede8

@ -32,12 +32,17 @@ class QualityAnalysisController < ApplicationController
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).first
if @client.job.exists?(job_name) && QualityAnalysis.where(:sonar_name => sonar_name).select{|qa| arr.include?(qa.sonar_name)}.blank? if @client_jenkins.jobdd.exists?(job_name) && QualityAnalysis.where(:sonar_name => sonar_name).select{|qa| arr.include?(qa.sonar_name)}.blank?
aa = @client.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
if @client_jenkins
return connect_jenkins
end
# Checks if the given job exists in Jenkins. # Checks if the given job exists in Jenkins.
# unless @client.job.exists?(job_name) 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])
@ -59,20 +64,20 @@ class QualityAnalysisController < ApplicationController
@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.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}") 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.job.build("#{job_name}") code = @client_jenkins.job.build("#{job_name}")
logger.error("build result ==> #{code}") logger.error("build result ==> #{code}")
# 判断调用sonar分析是否成功 # 判断调用sonar分析是否成功
# 等待启动时间处理, 最长时间为30分钟 # 等待启动时间处理, 最长时间为30分钟
for i in 0..360 do for i in 0..360 do
sleep(5) sleep(5)
@current_build_status = @client.job.get_current_build_status("#{job_name}") @current_build_status = @client_jenkins.job.get_current_build_status("#{job_name}")
if (@current_build_status == "success" || @current_build_status == "failure") if (@current_build_status == "success" || @current_build_status == "failure")
break break
if i == 360 if i == 360
@ -86,7 +91,7 @@ class QualityAnalysisController < ApplicationController
sleep(10) sleep(10)
# 获取sonar output结果 # 获取sonar output结果
console_build = @client.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
@ -94,14 +99,14 @@ class QualityAnalysisController < ApplicationController
# 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.job.delete("#{job_name}") @client_jenkins.job.delete("#{job_name}")
else else
if @current_build_status == "failure" if @current_build_status == "failure"
reg_console = /Exception:.*?\r/.match(console_build) reg_console = /Exception:.*?\r/.match(console_build)
output = reg_console[0].gsub("\r", "") unless reg_console.nil? output = reg_console[0].gsub("\r", "") unless reg_console.nil?
se = SonarError.where(:jenkins_job_name => job_name).first 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) 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}") @client_jenkins.job.delete("#{job_name}")
elsif @current_build_status == "success" elsif @current_build_status == "success"
if quality_an.blank? if quality_an.blank?
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier, QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier,
@ -111,17 +116,24 @@ class QualityAnalysisController < ApplicationController
end end
end end
end end
# end
respond_to do |format|
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
rescue => e rescue => e
@message = e.message @message = e.message
end logger.error("######################====>#{e.message}")
respond_to do |format| respond_to do |format|
if @current_build_status == "success" format.html{redirect_to error_list_project_quality_analysi_path(:project_id => @project.id, :job_name => job_name, :message => @message)}
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 :controller => 'repositories', :action => 'show', :id => @project, :repository_id => identifier}
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 end
end end
def error_list def error_list
@ -159,9 +171,9 @@ class QualityAnalysisController < ApplicationController
rep_id = Repository.where(:project_id => @project.id, :identifier => qa.rep_identifier).first.try(:id) rep_id = Repository.where(:project_id => @project.id, :identifier => qa.rep_identifier).first.try(:id)
job_name = "#{qa.author_login}-#{rep_id}" job_name = "#{qa.author_login}-#{rep_id}"
logger.info("result: job_name ###################==>#{job_name}") logger.info("result: job_name ###################==>#{job_name}")
logger.info("result: @client.job ###################==>#{@client.job}") logger.info("result: @client_jenkins.job ###################==>#{@client_jenkins.job}")
d_job = @client.job.delete(job_name) d_job = @client_jenkins.job.delete(job_name)
logger.info("result: delete job ###################==>#{d_job}") logger.info("result: delete job ###################==>#{d_job}")
qa.delete qa.delete
respond_to do |format| respond_to do |format|
@ -198,7 +210,7 @@ class QualityAnalysisController < ApplicationController
@doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties # sonar-properties @doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties # sonar-properties
# update成功则返回 200 # update成功则返回 200
jenkins_job = @client.job.update("#{job_name}", @doc.to_xml) jenkins_job = @client_jenkins.job.update("#{job_name}", @doc.to_xml)
# 数据更新到Trustie数据 # 数据更新到Trustie数据
if jenkins_job == '200' if jenkins_job == '200'
logger.info("quality_ananlysis will be updated: ==> #{jenkins_job}") logger.info("quality_ananlysis will be updated: ==> #{jenkins_job}")
@ -280,7 +292,7 @@ class QualityAnalysisController < ApplicationController
jenkins_password = Redmine::Configuration['jenkins_password'] jenkins_password = Redmine::Configuration['jenkins_password']
# connect jenkins # connect jenkins
@client = 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)
rescue => e rescue => e
logger.error("failed to connect Jenkins ==> #{e}") logger.error("failed to connect Jenkins ==> #{e}")
end end

@ -5,6 +5,8 @@
<div class="c_red">本次分析失败,原因如下:</div> <div class="c_red">本次分析失败,原因如下:</div>
<% if @build_console_result == false %> <% if @build_console_result == false %>
分析超时 分析超时
<% elsif params[:message] %>
<%= params[:message] %>
<% else %> <% else %>
<%= h @error_list.try(:output).html_safe %> <%= h @error_list.try(:output).html_safe %>
<% end %> <% end %>

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save