some status of sonar

This commit is contained in:
Your Name 2016-06-28 14:48:09 +08:00
parent f930bbddad
commit dd6b19a8b5
4 changed files with 45 additions and 31 deletions

View File

@ -17,19 +17,20 @@ class QualityAnalysisController < ApplicationController
begin
user_name = User.find(params[:user_id]).try(:login)
identifier = params[:identifier]
rep_id = params[:rep_id]
# REDO
job_name = "#{user_name}:#{identifier}"
job_name = "#{user_name}-#{rep_id}"
# Checks if the given job exists in Jenkins.
unless @client.job.exists?(job_name)
@g = Gitlab.client
branch = params[:branch].blank? ? "master" : params[:branch]
branch = params[:branch]
language = params[:language]
path = params[:path]
qa = QualityAnalysis.where(:project_id => @project.id).first
qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first
version = qa.nil? ? 1 : qa.sonar_version + 1
properties = "sonar.projectKey=#{user_name}:#{identifier}
sonar.projectName=#{user_name}:#{identifier}
properties = "sonar.projectKey=#{user_name}:#{rep_id}
sonar.projectName=#{user_name}:#{rep_id}
sonar.projectVersion=#{version}
sonar.sources=#{path}
sonar.language=#{language.downcase}
@ -43,18 +44,20 @@ class QualityAnalysisController < ApplicationController
@doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties # sonar-properties
# return '200' if successed
jenkins_job = @client.job.create("#{user_name}_#{identifier}", @doc.to_xml)
jenkins_job = @client.job.create("#{job_name}", @doc.to_xml)
# replace gitlab hook
@g.add_project_hook(@project.gpid, @jenkins_address + "/project/#{user_name}_#{identifier}")
@g.add_project_hook(@project.gpid, @jenkins_address + "/project/#{job_name}")
# build job
logger.error("Test#{jenkins_job}")
# return '201' if successed
code = @client.job.build("#{user_name}_#{identifier}")
if qa.blank?
logger.error("Jenkins status of create ==> #{jenkins_job}")
# return '201' if build successed
code = @client.job.build("#{job_name}")
logger.error("build result ==> #{code}")
d = @client.delete("#{job_name}") if jenkins_job == '200' && code != '201'
logger.error("delete result ==> #{code}")
if qa.blank? && code == '201'
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier,
:sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => job_name)
:sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => "#{user_name}:#{rep_id}")
else
qa.update_attribute(:sonar_version, version)
end
@ -62,23 +65,34 @@ class QualityAnalysisController < ApplicationController
rescue => e
puts e
end
respond_to do |format|
format.html{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => "#{user_name}:#{rep_id}")}
format.js
end
end
# resource_id: login + @repository.id
def index
@sonar_address = Redmine::Configuration['sonar_address']
projects_date = open(@sonar_address + "/api/projects/index").read
arr = JSON.parse(projects_date).map {|m| m["nm"]} # ["Hjqreturn:cc_rep", "Hjqreturn:putong", "Hjqreturn:sonar_rep2", "shitou:sonar_rep"]
if params[:resource_id].nil?
@quality_analyses = QualityAnalysis.where("sonar_name in #{arr}")
else
qa = QualityAnalysis.where(:project_id => @project.id).first
@resource_id = qa.author_login+":"+qa.rep_identifier
@name_flag = false
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
# @sonar_issues = JSON.parse(issue_date).first
begin
@resource_id = params[:resource_id]
@sonar_address = Redmine::Configuration['sonar_address']
# projects_date = open(@sonar_address + "/api/projects/index").read
# @arr = JSON.parse(projects_date).map {|m| m["nm"]} # ["Hjqreturn:cc_rep", "Hjqreturn:putong", "Hjqreturn:sonar_rep2", "shitou:sonar_rep"]
if params[:resource_id].nil?
@name_flag = true
# @quality_analyses = QualityAnalysis.where("sonar_name in (#{arr.empty? ? '0': arr.join(',')})")
@quality_analyses = QualityAnalysis.where(:project_id => @project.id)
else
qa = QualityAnalysis.where(:project_id => @project.id).first
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
@sonar_issues = JSON.parse(issue_date).first
end
rescue => e
puts e
end
end
# Find project of id params[:project_id]

View File

@ -1,5 +1,5 @@
class QualityAnalysis < ActiveRecord::Base
attr_accessible :author_login, :project_id, :rep_identifier, :sonar_version, :branch, :path, :rep_identifier, :language
attr_accessible :author_login, :project_id, :rep_identifier, :sonar_version, :branch, :path, :rep_identifier, :language, :sonar_name
def user_rep_name
self.author_login+":"+self.rep_identifier

View File

@ -6,17 +6,17 @@
<li class="analysis-result-version fl fontBlue2" >版本</li>
<li class="analysis-result-loc fl fontBlue2" >分支</li>
<li class="analysis-result-debt fl fontBlue2" >语言</li>
<li class="analysis-result-time fl fontBlue2" >时间</li>
<li class="analysis-result-time fl fontBlue2" >lujing</li>
<div class="cl"></div>
</ul>
<% if @quality_analyses.count >0 %>
<% @quality_analyses.each do |qa| %>
<ul class="analysis-result-list">
<li title="Name" title="名称"><%=link_to "#{qa.author_login}:#{qa.rep_identifier}", project_quality_analysis_path(:resource_id => qa.author_login+":"+qa.rep_identifier, :branch => qa.branch.nil? ? "master" : qa.branch), :class => "analysis-result-name fl fontBlue2" %></li>
<li title="Name" title="名称"><%=link_to "#{qa.author_login}:#{qa.rep_identifier}", project_quality_analysis_path(:resource_id => qa.sonar_name, :branch => qa.branch.nil? ? "master" : qa.branch), :class => "analysis-result-name fl fontBlue2" %></li>
<li class="analysis-result-version fl fontBlue2" title="版本">1.0</li>
<li class="analysis-result-loc fl fontBlue2" title="分支名"><%= qa.branch %></li>
<li class="analysis-result-debt fl fontBlue2" title="语言"><%= qa.language %></li>
<li class="analysis-result-time fl fontBlue2" title="时间"><%= format_time(qa.created_at) %></li>
<li class="analysis-result-time fl fontBlue2" title="时间"><%= qa.path %></li>
<div class="cl"></div>
</ul>
<% end %>

View File

@ -1,6 +1,6 @@
<div class="f16 fb fontBlue mb10">代码质量分析</div>
<div>
<%= form_tag( url_for(:controller => 'quality_analysis', :action => 'create', :project_id => @project.id, :user_id => User.current.id, :identifier => @repository.identifier), :remote => true, :id => 'quality_analyses_form') do %>
<%= form_tag( url_for(:controller => 'quality_analysis', :action => 'create', :project_id => @project.id, :user_id => User.current.id, :identifier => @repository.identifier, :rep_id => @repository.id), :remote => true, :id => 'quality_analyses_form') do %>
<div class="ui form">
<div class="mb10" style="margin-right:13px;">
<textarea id="path_description" name="path" rows="8" placeholder="目录相对于根目录,用半角逗号隔开。如:src/main/java,libs,res/script" style="height: 87px; resize:vertical;" class="analysis-option-box"></textarea>