change sonar method into restart and cancel push events

This commit is contained in:
huang 2016-09-01 15:58:53 +08:00
parent 5527b4e3a9
commit 79e30862e9
3 changed files with 87 additions and 85 deletions

View File

@ -16,8 +16,8 @@ class QualityAnalysisController < ApplicationController
end
# params 说明:{identifier版本库名}
# type: 1 新的分析 2 重新分析
def create
logger.info("11111111111111111111111111111")
begin
user_name = User.find(params[:user_id]).try(:login)
identifier = params[:identifier]
@ -30,22 +30,26 @@ class QualityAnalysisController < ApplicationController
# 考虑到历史数据有些用户创建类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_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}")
quality_an.delete unless quality_an.blank?
end
# 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)
# 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}")
# quality_an.delete unless quality_an.blank?
# 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.
unless @client_jenkins.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 = quality_an.nil? ? 1 : quality_an.sonar_version + 1
version = 1
properties = "sonar.projectKey=#{sonar_name}
sonar.projectName=#{sonar_name}
sonar.projectVersion=#{version}
@ -62,13 +66,11 @@ class QualityAnalysisController < ApplicationController
# jenkins job创建
jenkins_job = @client_jenkins.job.create("#{job_name}", @doc.to_xml)
logger.info("Jenkins status of create ==> #{jenkins_job}")
# 将地址作为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
code = @client_jenkins.job.build("#{job_name}")
logger.error("build result ==> #{code}")
# 判断调用sonar分析是否成功
# 等待启动时间处理, 最长时间为30分钟
@ -108,8 +110,6 @@ class QualityAnalysisController < ApplicationController
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
@ -121,7 +121,7 @@ class QualityAnalysisController < ApplicationController
format.html{redirect_to error_list_project_quality_analysi_path(:project_id => @project.id, :job_name => job_name)}
end
end
end
rescue => e
@message = e.message
logger.error("######################====>#{e.message}")
@ -302,10 +302,8 @@ class QualityAnalysisController < ApplicationController
@jenkins_address = Redmine::Configuration['jenkins_address']
jenkins_username = Redmine::Configuration['jenkins_username']
jenkins_password = Redmine::Configuration['jenkins_password']
logger.info("22222222222222222222222222222222")
# connect jenkins
@client_jenkins = JenkinsApi::Client.new(:server_url => @jenkins_address, :username => jenkins_username, :password => jenkins_password)
logger.info("333333333333333333333333333333")
rescue => e
logger.error("failed to connect Jenkins ==> #{e}")
end

View File

@ -127,7 +127,7 @@
<p class="fontBlue2"><%= @ha["functions"].to_i %></p>
</div>
</div>
<% unless @user_quality_infos.blank? %>
<% if !@user_quality_infos.blank? %>
<div class="analysis-tag-wrap f16"> <span class="analysis-tag fl mr15"></span> <span class="fb fl">贡献统计</span></div>
<div class="analysis-block mt10 f12">
<ul class="contribute-list">
@ -141,7 +141,7 @@
<% @user_quality_infos.each do |author_info| %>
<% user = get_user_by_mail(author_info[:email]) %>
<% unless author_info[:changes] == 0 %>
<%# unless author_info[:changes] == 0 %>
<ul class="contribute-list">
<li class="fl fontGrey2 contribute-list-avatar contribute-list-height">
<div class="mt8">
@ -155,7 +155,7 @@
<li class="fl contribute-list-rate fontBlue2 contribute-list-height contribute-list-line-height"><%= author_info[:ratio] %></li>
<div class="cl"></div>
</ul>
<% end %>
<%# end %>
<% end %>
</div>

View File

@ -3,8 +3,12 @@
<div class="fl"><h2 class="project_h2_repository"><%= render :partial => 'breadcrumbs', :locals => {:path => @path, :kind => 'dir', :revision => @rev} %></h2></div>
<% unless @entries.nil? %>
<a href="<%= @zip_path %>" class="btn_zipdown fr" onclick="">ZIP下载</a>
<% if quality_analysis(User.current.try(:login), @repository.id).nil? && User.current.member_of?(@project) && @project.is_public? %>
<%= link_to "质量分析", quality_analysis_path(:id => @project.id, :repository_id => @repository.identifier, :rev => @rev, :default_branch => @g_default_branch ), :remote => true, :class => "btn_zipdown fr" %>
<% if User.current.member_of?(@project) && @project.is_public? %>
<% if quality_analysis(User.current.try(:login), @repository.id).nil? %>
<%= link_to "质量分析", quality_analysis_path(:id => @project.id, :repository_id => @repository.identifier, :rev => @rev, :default_branch => @g_default_branch, :type => "1"), :remote => true, :class => "btn_zipdown fr" %>
<% else %>
<%= link_to "重新分析", quality_analysis_path(:id => @project.id, :repository_id => @repository.identifier, :rev => @rev, :default_branch => @g_default_branch, :type => "2"), :remote => true, :class => "btn_zipdown fr" %>
<% end %>
<% end %>
<% end %>
<!--quality_analysis-->