modified main method

This commit is contained in:
Your Name 2016-06-27 18:13:46 +08:00
parent 69f7dab04d
commit e5b93ac7ed
7 changed files with 93 additions and 95 deletions

View File

@ -1,6 +1,7 @@
class QualityAnalysisController < ApplicationController
before_filter :find_project_by_project_id#, :except => [:getattachtype]
before_filter :authorize
before_filter :connect_jenkins, :only => [:create]
layout "base_projects"
include ApplicationHelper
require 'jenkins_api_client'
@ -13,74 +14,61 @@ class QualityAnalysisController < ApplicationController
end
def create
gitlab_address = Redmine::Configuration['gitlab_address']
jenkins_address = Redmine::Configuration['jenkins_address']
@client = JenkinsApi::Client.new(:server_url => jenkins_address,
:username => "temp",
:password => '123123')
#@client.exists?(job_name)
@g = Gitlab.client
user_name = User.find(params[:user_id]).try(:login)
branch = params[:branch].blank? ? "master" : params[:branch]
language = params[:language]
path = params[:path]
identifier = params[:identifier]
qa = QualityAnalysis.where(:project_id => @project.id).first
version = qa.nil? ? 1 : qa.sonar_version + 1
properties = "sonar.projectKey=#{user_name}:#{identifier}
job_name = "#{user_name}:#{identifier}"
# Checks if the given job exists in Jenkins.
unless @client.job.exists?(job_name)
@g = Gitlab.client
branch = params[:branch].blank? ? "master" : params[:branch]
language = params[:language]
path = params[:path]
qa = QualityAnalysis.where(:project_id => @project.id).first
version = qa.nil? ? 1 : qa.sonar_version + 1
properties = "sonar.projectKey=#{user_name}:#{identifier}
sonar.projectName=#{user_name}:#{identifier}
sonar.projectVersion=#{version}
sonar.sources=#{path}
sonar.language=#{language.downcase}
sonar.sourceEncoding=utf-8"
git_url = gitlab_address.to_s+"/"+@project.owner.to_s+"/"+ identifier + "."+"git"
# # # modify config
@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.BranchSpec/name").content = "*/#{branch}"
@doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties #sonar-properties
#
# replace config.xml of jenkins
@client.job.create("#{user_name}_#{identifier}", @doc.to_xml)
# relace gitlab hook
# genkins address
@g.add_project_hook(@project.gpid, jenkins_address + "/project/#{user_name}_#{identifier}")
if qa.blank?
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier, :sonar_version => version, :path => path, :branch => branch, :language => language)
else
qa.update_attribute(:sonar_version, version)
git_url = @gitlab_address.to_s+"/"+@project.owner.to_s+"/"+ identifier + "."+"git"
# modify config.yml
@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.BranchSpec/name").content = "*/#{branch}"
@doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties # sonar-properties
@client.job.create("#{user_name}_#{identifier}", @doc.to_xml)
# relace gitlab hook
@g.add_project_hook(@project.gpid, @jenkins_address + "/project/#{user_name}_#{identifier}")
# build job
opts = {'build_start_timeout' => 30, 'cancel_on_build_start_timeout' => true}
code = @client.job.build("#{user_name}_#{identifier}", opts)
# sucessed will return "201"
raise "Unable to build job: #{user_name}_#{identifier}" unless code == '201'
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)
else
qa.update_attribute(:sonar_version, version)
end
end
# scan
@client.job.build("#{user_name}_#{identifier}")
end
def index
@sonar_address = Redmine::Configuration['sonar_address']
# if params[:resource_id].nil?
# @name_flag = true
# @quality_analyses = QualityAnalysis.where(:project_id => @project.id)
# # @quality_analyses.map {|qa| qa.}
# # if @quality_analyses.count > 0
# # @quality_analyses.each do |qa|
# # ["Hjqreturn:cc_rep", "Hjqreturn:putong", "Hjqreturn:sonar_rep2", "shitou:sonar_rep"]
# #
# # end
# # end
# # projects_date = open(@sonar_address + "/api/projects/index").read
# # arr = JSON.parse(projects_date).map {|m| m["nm"]}
# # arr.map
# 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
# end
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
end
end
# Find project of id params[:project_id]
@ -99,4 +87,13 @@ class QualityAnalysisController < ApplicationController
end
end
def connect_jenkins
@gitlab_address = Redmine::Configuration['gitlab_address']
@jenkins_address = Redmine::Configuration['jenkins_address']
# connect jenkins
@client = JenkinsApi::Client.new(:server_url => @jenkins_address, :username => "temp", :password => '123123')
rescue
logger.error("failed to connect Jenkins")
end
end

View File

@ -3,19 +3,19 @@ module QualityAnalysisHelper
def sqale_rating_status val
arr = []
if val.to_i > 0 && val.to_i < 5
if val > 0 && val < 5
arr << "很好"
arr << "b_green2"
elsif val.to_i > 5 && val.to_i < 10
elsif val. > 5 && val < 10
arr << "较好"
arr << "b_slow_yellow"
elsif val.to_i > 10 && val.to_i < 20
elsif val > 10 && val < 20
arr << "中等"
arr << "b_yellow"
elsif val.to_i > 20 && val.to_i < 50
elsif val > 20 && val < 50
arr << "较差"
arr << "b_slow_red"
elsif val.to_i > 20
elsif val > 20
arr << "很差"
arr << "b_red"
end
@ -23,13 +23,13 @@ module QualityAnalysisHelper
def complexity_status val
arr = []
if val.to_i < 10
if val < 10
arr << "良好"
arr << "b_green2"
elsif val.to_i > 10 && val.to_i < 15
elsif val > 10 && val < 15
arr << "较高"
arr << "b_yellow"
elsif val.to_i > 15
elsif val > 15
arr << "很高"
arr << "b_red"
end
@ -37,13 +37,13 @@ module QualityAnalysisHelper
def duplicated_lines_density_status val
arr = []
if val.to_i < 30
if val < 30
arr << "良好"
arr << "b_green2"
elsif val.to_i > 30 && val.to_i < 50
elsif val > 30 && val < 50
arr << "较高"
arr << "b_yellow"
elsif val.to_i > 50
elsif val > 50
arr << "很高"
arr << "b_red"
end
@ -51,28 +51,28 @@ module QualityAnalysisHelper
def comment_lines_density_status val
arr = []
if val.to_i < 20
if val < 20
arr << "较低"
arr << "b_yellow"
elsif val.to_i > 20 && val.to_i < 50
elsif val > 20 && val < 50
arr << "正常"
arr << "b_green2"
elsif val.to_i > 50
elsif val > 50
arr << "较高"
arr << "b_red"
end
end
def score_sqale_rating val
if val.to_i > 0 && val.to_i < 5
if val > 0 && val < 5
"5"
elsif val.to_i > 5 && val.to_i < 10
elsif val > 5 && val < 10
"4"
elsif val.to_i > 10 && val.to_i < 20
elsif val > 10 && val < 20
"3"
elsif val.to_i > 20 && val.to_i < 50
elsif val > 20 && val < 50
"2"
elsif val.to_i > 20
elsif val > 20
"1"
end
end

View File

@ -1,8 +1,8 @@
<%= javascript_include_tag 'highcharts','highcharts-more' %>
<%= render :partial => "hightchars" %>
<div class="project_r_h">
<h2 class="project_h2" style="width:180px;">质量分析</h2>
</div>
<%= render :partial => "hightchars" %>
<div class="project_r_h">
<h2 class="project_h2" style="width:180px;">质量分析</h2>
</div>
<div class="button-rep">当前分支:<%= params[:branch] %></div>
<div class="cl"></div>
<div class="tac f20 fb mt35 mb30">项目代码质量分析报告</div>
@ -12,28 +12,28 @@
<div class="analysis-genral">
<p id="container_sqale_rating" style="max-width:200px;min-height:200px;width:200px; margin:0 auto;"></p>
<p class="fontGrey3">质量等级</p>
<p class="fontBlue2 pr"><%= @complexity["msr"][9]["frmt_val"] %><span class="f8 c_white analysis-genral-icon <%= sqale_rating_status(@complexity["msr"][9]["val"])[1] %> borderRadius"><%= sqale_rating_status(@complexity["msr"][9]["val"])[0] %></span></p>
<p class="fontBlue2 pr"><%= @complexity["msr"][9]["frmt_val"] %><span class="f8 c_white analysis-genral-icon <%= sqale_rating_status(@complexity["msr"][9]["val"].to_i)[1] %> borderRadius"><%= sqale_rating_status(@complexity["msr"][9]["val"].to_i)[0] %></span></p>
</div>
<div class="analysis-genral" >
<p id="container_function_complexity" style="max-width:200px;min-height:200px;width:200px; margin:0 auto;"></p>
<p class="fontGrey3">复杂度</p>
<p class="fontBlue2 pr"><%= @complexity["msr"][6]["val"] %><span class="f8 c_white analysis-genral-icon <%= complexity_status(@complexity["msr"][6]["val"])[1] %> borderRadius"><%= complexity_status(@complexity["msr"][6]["val"])[0] %></span></p>
<p class="fontBlue2 pr"><%= @complexity["msr"][6]["val"] %><span class="f8 c_white analysis-genral-icon <%= complexity_status(@complexity["msr"][6]["val"].to_i)[1] %> borderRadius"><%= complexity_status(@complexity["msr"][6]["val"].to_i)[0] %></span></p>
</div>
</div>
<div class="flex">
<div class="analysis-genral">
<p id="container_duplicated_lines_density" style="max-width:200px;min-height:200px;width:200px; margin:0 auto;"></p>
<p class="fontGrey3">代码重复度</p>
<p class="fontBlue2 pr"><%= @complexity["msr"][7]["frmt_val"] %><span class="f8 c_white analysis-genral-icon <%= duplicated_lines_density_status(@complexity["msr"][7]["val"])[1] %> borderRadius"><%= duplicated_lines_density_status(@complexity["msr"][7]["val"])[0] %></span></p>
<p class="fontBlue2 pr"><%= @complexity["msr"][7]["frmt_val"] %><span class="f8 c_white analysis-genral-icon <%= duplicated_lines_density_status(@complexity["msr"][7]["val"].to_i)[1] %> borderRadius"><%= duplicated_lines_density_status(@complexity["msr"][7]["val"].to_i)[0] %></span></p>
</div>
<div class="analysis-genral">
<p id="container_comment_lines_density" style="max-width:200px;min-height:200px;width:200px; margin:0 auto;"></p>
<p class="fontGrey3">注释率</p>
<p class="fontBlue2 pr"><%= @complexity["msr"][5]["frmt_val"] %><span class="f8 c_white analysis-genral-icon <%= comment_lines_density_status(@complexity["msr"][5]["val"])[1] %> borderRadius"><%=comment_lines_density_status(@complexity["msr"][5]["val"])[0] %></span></p>
<p class="fontBlue2 pr"><%= @complexity["msr"][5]["frmt_val"] %><span class="f8 c_white analysis-genral-icon <%= comment_lines_density_status(@complexity["msr"][5]["val"].to_i)[1] %> borderRadius"><%=comment_lines_density_status(@complexity["msr"][5]["val"].to_i)[0] %></span></p>
</div>
</div>
</div>
<div class="analysis-tag-wrap f16"> <span class="analysis-tag fl mr15"></span> <span class="fb fl mr10">质量等级</span><span class="mr10 fontGrey2"><span class="c_red f18" style="margin-top:-5px; display:inline-block;"><%= score_sqale_rating(@complexity["msr"][9]["val"]) %></span>/5分</span><span class="fontGrey2">可定性评价为:<span class="c_red">质量<%= sqale_rating_status(@complexity["msr"][9]["val"])[0] %></span></span></div>
<div class="analysis-tag-wrap f16"> <span class="analysis-tag fl mr15"></span> <span class="fb fl mr10">质量等级</span><span class="mr10 fontGrey2"><span class="c_red f18" style="margin-top:-5px; display:inline-block;"><%= score_sqale_rating(@complexity["msr"][9]["val"].to_i) %></span>/5分</span><span class="fontGrey2">可定性评价为:<span class="c_red">质量<%= sqale_rating_status(@complexity["msr"][9]["val"])[0] %></span></span></div>
<div class="analysis-block mt10 mb40 f14">
<div><span class="fontGrey3 mr30">技术债务</span><span class="fontBlue2 w70 pInline"><%= @complexity["msr"][8]["frmt_val"] %></span><span class="fontGrey2"><a target="_blank" href="<%= @sonar_address %>/drilldown/measures/<%= @resource_id %>?metric=sqale_index">查看详情</a></span></div>
<div><span class="fontGrey3 mr30">质量问题</span>
@ -46,7 +46,7 @@
<div><span class="analysis-info-icon mr5"></span><span class="fontGrey3 mr45">信息</span><span class="fontBlue2 w70 pInline"><%= @sonar_issues["msr"][5]["frmt_val"] %></span><span class="quality-percentage"><span class="quality-percentage-rate" style="width:<%= statistics_result_percentage(@sonar_issues["msr"][5]["frmt_val"].to_i, 200) %>%;"></span></span></div>
</div>
</div>
<div class="analysis-tag-wrap f16"> <span class="analysis-tag fl mr15"></span> <span class="fb fl mr10">代码规模</span><span class="fontGrey2">可定性评价为:<span class="c_red"><%= lines_scale(@complexity["msr"][0]["frmt_val"]) %></span></span></div>
<div class="analysis-tag-wrap f16"> <span class="analysis-tag fl mr15"></span> <span class="fb fl mr10">代码规模</span><span class="fontGrey2">可定性评价为:<span class="c_red"><%= lines_scale(@complexity["msr"][0]["frmt_val"].to_i) %></span></span></div>
<div class="analysis-block mt10 mb40 flex f14">
<div class="analysis-genral">
<p class="fontGrey3">代码行数</p>

View File

@ -2,13 +2,13 @@
<div class="project_r_h">
<div class="fl"><h2 class="project_h2_repository"><%= render :partial => 'breadcrumbs', :locals => {:path => @path, :kind => 'dir', :revision => @rev} %></h2></div>
<a href="<%= @zip_path %>" class="btn_zipdown fr" onclick="">ZIP下载</a>
<% if is_project_manager?(User.current, @project.id) && QualityAnalysis.where(:project_id => @project.id).first.nil? %>
<%# if is_project_manager?(User.current, @project.id) && QualityAnalysis.where(:project_id => @project.id).first.nil? %>
<%# if User.current.member_of?(@project) %>
<%= link_to "质量分析", quality_analysis_path(:id => @project.id), :remote => true, :class => "btn_zipdown fr" %>
<%# end %>
<% else %>
<%= link_to "质量分析", project_quality_analysis_path(:project_id => @project.id, :resource_id => @proje), :class => "btn_zipdown fr" %>
<% end %>
<%# else %>
<%#= link_to "质量分析", project_quality_analysis_path(:project_id => @project.id, :resource_id => @proje), :class => "btn_zipdown fr" %>
<%# end %>
</div>
<div class="repository_con" style="line-height:1.9;">
<% if @entries.nil? %>

View File

@ -1,5 +0,0 @@
class AddNameToQualityAnalyses < ActiveRecord::Migration
def change
add_column :quality_analyses, :language, :string
end
end

View File

@ -0,0 +1,5 @@
class AddSonarNameToQualityAnalyses < ActiveRecord::Migration
def change
add_column :quality_analyses, :sonar_name, :string
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20160624055127) do
ActiveRecord::Schema.define(:version => 20160627090316) do
create_table "activities", :force => true do |t|
t.integer "act_id", :null => false
@ -1844,6 +1844,7 @@ ActiveRecord::Schema.define(:version => 20160624055127) do
t.string "path"
t.string "branch"
t.string "language"
t.string "sonar_name"
end
create_table "queries", :force => true do |t|