socialforge/app/controllers/quality_analysis_controller.rb

138 lines
5.2 KiB
Ruby
Raw Normal View History

2016-06-21 16:50:45 +08:00
class QualityAnalysisController < ApplicationController
2016-06-17 11:29:49 +08:00
before_filter :find_project_by_project_id#, :except => [:getattachtype]
2016-06-21 16:46:34 +08:00
before_filter :authorize
2016-06-27 18:13:46 +08:00
before_filter :connect_jenkins, :only => [:create]
2016-06-17 11:29:49 +08:00
layout "base_projects"
include ApplicationHelper
require 'jenkins_api_client'
require 'nokogiri'
2016-06-21 16:46:34 +08:00
require 'json'
require 'open-uri'
2016-06-22 17:09:54 +08:00
def show
end
def create
2016-06-28 09:45:45 +08:00
begin
user_name = User.find(params[:user_id]).try(:login)
identifier = params[:identifier]
2016-06-28 14:48:09 +08:00
rep_id = params[:rep_id]
2016-06-28 09:45:45 +08:00
# REDO
2016-06-28 14:48:09 +08:00
job_name = "#{user_name}-#{rep_id}"
2016-06-28 17:18:50 +08:00
sonar_name = "#{user_name}:#{rep_id}"
2016-06-28 09:45:45 +08:00
# Checks if the given job exists in Jenkins.
unless @client.job.exists?(job_name)
@g = Gitlab.client
2016-06-28 14:48:09 +08:00
branch = params[:branch]
2016-06-29 09:59:21 +08:00
language = swith_language_type(params[:language])
2016-06-29 09:10:47 +08:00
path = params[:path].blank? ? "./" : params[:path]
2016-06-28 14:48:09 +08:00
qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first
2016-06-28 09:45:45 +08:00
version = qa.nil? ? 1 : qa.sonar_version + 1
2016-06-28 17:18:50 +08:00
properties = "sonar.projectKey=#{sonar_name}
sonar.projectName=#{sonar_name}
2016-06-22 17:09:54 +08:00
sonar.projectVersion=#{version}
sonar.sources=#{path}
2016-06-21 16:46:34 +08:00
sonar.language=#{language.downcase}
sonar.sourceEncoding=utf-8"
2016-06-28 09:45:45 +08:00
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
# return '200' if successed
2016-06-28 14:48:09 +08:00
jenkins_job = @client.job.create("#{job_name}", @doc.to_xml)
2016-06-28 09:45:45 +08:00
# replace gitlab hook
2016-06-28 14:48:09 +08:00
@g.add_project_hook(@project.gpid, @jenkins_address + "/project/#{job_name}")
2016-06-28 09:45:45 +08:00
# build job
2016-06-28 14:48:09 +08:00
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'
2016-06-28 09:45:45 +08:00
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier,
2016-06-28 14:48:09 +08:00
:sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => "#{user_name}:#{rep_id}")
2016-06-28 09:45:45 +08:00
else
qa.update_attribute(:sonar_version, version)
end
2016-06-27 18:13:46 +08:00
end
2016-06-28 09:45:45 +08:00
rescue => e
puts e
2016-06-22 17:09:54 +08:00
end
2016-06-29 09:38:30 +08:00
# respond_to do |format|
2016-06-29 11:10:19 +08:00
# #format.html{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch)}
2016-06-29 09:38:30 +08:00
# format.js
# end
end
2016-06-17 11:29:49 +08:00
2016-06-29 09:59:21 +08:00
# get language type
def swith_language_type language
2016-06-29 11:10:19 +08:00
if language == "c#"
2016-06-29 09:59:21 +08:00
"cs"
2016-06-29 11:10:19 +08:00
elsif language == "python"
2016-06-29 09:59:21 +08:00
"py"
2016-06-29 11:10:19 +08:00
elsif language == "c"
2016-06-29 09:59:21 +08:00
"c++"
else
language
end
end
2016-06-28 14:48:09 +08:00
# resource_id: login + @repository.id
2016-06-17 11:29:49 +08:00
def index
2016-06-28 14:48:09 +08:00
begin
@resource_id = params[:resource_id]
@sonar_address = Redmine::Configuration['sonar_address']
if params[:resource_id].nil?
@name_flag = true
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"]
2016-06-29 09:38:30 +08:00
@quality_analyses = QualityAnalysis.where(:project_id => @project.id).select{|qa| arr.include?(qa.sonar_name)}
2016-06-28 14:48:09 +08:00
else
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
2016-06-27 18:13:46 +08:00
end
2016-06-28 14:48:09 +08:00
2016-06-17 11:29:49 +08:00
end
# Find project of id params[:project_id]
def find_project_by_project_id
@project = Project.find(params[:project_id])
rescue ActiveRecord::RecordNotFound
render_404
end
2016-06-21 16:46:34 +08:00
# Authorize the user for the requested action
def authorize(ctrl = params[:controller], action = params[:action], global = false)
unless @project.archived? && @project.gpid.nil?
true
else
render_403 :message => :notice_not_authorized_archived_project
end
end
2016-06-27 18:13:46 +08:00
def connect_jenkins
@gitlab_address = Redmine::Configuration['gitlab_address']
@jenkins_address = Redmine::Configuration['jenkins_address']
2016-06-28 09:45:45 +08:00
2016-06-27 18:13:46 +08:00
# connect jenkins
@client = JenkinsApi::Client.new(:server_url => @jenkins_address, :username => "temp", :password => '123123')
2016-06-28 09:45:45 +08:00
rescue => e
logger.error("failed to connect Jenkins ==> #{e}")
2016-06-27 18:13:46 +08:00
end
2016-06-17 11:29:49 +08:00
end