|
|
|
@ -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
|
|
|
|
|