|
|
|
@ -14,9 +14,12 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
|
|
|
|
begin
|
|
|
|
|
user_name = User.find(params[:user_id]).try(:login)
|
|
|
|
|
identifier = params[:identifier]
|
|
|
|
|
# REDO
|
|
|
|
|
job_name = "#{user_name}:#{identifier}"
|
|
|
|
|
|
|
|
|
|
# Checks if the given job exists in Jenkins.
|
|
|
|
|
unless @client.job.exists?(job_name)
|
|
|
|
|
@g = Gitlab.client
|
|
|
|
@ -32,26 +35,33 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
sonar.language=#{language.downcase}
|
|
|
|
|
sonar.sourceEncoding=utf-8"
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# return '200' if successed
|
|
|
|
|
jenkins_job = @client.job.create("#{user_name}_#{identifier}", @doc.to_xml)
|
|
|
|
|
|
|
|
|
|
# replace 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'
|
|
|
|
|
logger.error("Test#{jenkins_job}")
|
|
|
|
|
|
|
|
|
|
# return '201' if successed
|
|
|
|
|
code = @client.job.build("#{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, :sonar_name => job_name)
|
|
|
|
|
else
|
|
|
|
|
qa.update_attribute(:sonar_version, version)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
rescue => e
|
|
|
|
|
puts e
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def index
|
|
|
|
@ -90,10 +100,11 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
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")
|
|
|
|
|
rescue => e
|
|
|
|
|
logger.error("failed to connect Jenkins ==> #{e}")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|