From dd6b19a8b543234d1f9d2fa879a583bf3d1f4cb7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 28 Jun 2016 14:48:09 +0800 Subject: [PATCH] some status of sonar --- .../quality_analysis_controller.rb | 66 +++++++++++-------- app/models/quality_analysis.rb | 2 +- .../quality_analysis/_result_list.html.erb | 6 +- .../repositories/_quality_analysis.html.erb | 2 +- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/app/controllers/quality_analysis_controller.rb b/app/controllers/quality_analysis_controller.rb index bff369eaf..7de4bedab 100644 --- a/app/controllers/quality_analysis_controller.rb +++ b/app/controllers/quality_analysis_controller.rb @@ -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] diff --git a/app/models/quality_analysis.rb b/app/models/quality_analysis.rb index 229be9826..8b8034c65 100644 --- a/app/models/quality_analysis.rb +++ b/app/models/quality_analysis.rb @@ -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 diff --git a/app/views/quality_analysis/_result_list.html.erb b/app/views/quality_analysis/_result_list.html.erb index 507ded0fe..845049cea 100644 --- a/app/views/quality_analysis/_result_list.html.erb +++ b/app/views/quality_analysis/_result_list.html.erb @@ -6,17 +6,17 @@
  • 版本
  • 分支
  • 语言
  • -
  • 时间
  • +
  • lujing
  • <% if @quality_analyses.count >0 %> <% @quality_analyses.each do |qa| %> <% end %> diff --git a/app/views/repositories/_quality_analysis.html.erb b/app/views/repositories/_quality_analysis.html.erb index f9726fb9b..0b3f38ee3 100644 --- a/app/views/repositories/_quality_analysis.html.erb +++ b/app/views/repositories/_quality_analysis.html.erb @@ -1,6 +1,6 @@
    代码质量分析
    - <%= 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 %>