modified create

This commit is contained in:
Your Name 2016-06-28 09:45:45 +08:00
parent e5b93ac7ed
commit f930bbddad
1 changed files with 44 additions and 33 deletions

View File

@ -14,9 +14,12 @@ class QualityAnalysisController < ApplicationController
end end
def create def create
begin
user_name = User.find(params[:user_id]).try(:login) user_name = User.find(params[:user_id]).try(:login)
identifier = params[:identifier] identifier = params[:identifier]
# REDO
job_name = "#{user_name}:#{identifier}" job_name = "#{user_name}:#{identifier}"
# Checks if the given job exists in Jenkins. # Checks if the given job exists in Jenkins.
unless @client.job.exists?(job_name) unless @client.job.exists?(job_name)
@g = Gitlab.client @g = Gitlab.client
@ -32,26 +35,33 @@ class QualityAnalysisController < ApplicationController
sonar.language=#{language.downcase} sonar.language=#{language.downcase}
sonar.sourceEncoding=utf-8" sonar.sourceEncoding=utf-8"
git_url = @gitlab_address.to_s+"/"+@project.owner.to_s+"/"+ identifier + "."+"git" git_url = @gitlab_address.to_s+"/"+@project.owner.to_s+"/"+ identifier + "."+"git"
# modify config.yml # modify config.yml
@doc = Nokogiri::XML(File.open(File.join(Rails.root, 'tmp', 'config.xml'))) @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.UserRemoteConfig/url").content = git_url
@doc.at_xpath("//hudson.plugins.git.BranchSpec/name").content = "*/#{branch}" @doc.at_xpath("//hudson.plugins.git.BranchSpec/name").content = "*/#{branch}"
@doc.at_xpath("//hudson.plugins.sonar.SonarRunnerBuilder/properties").content = properties # sonar-properties @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}") @g.add_project_hook(@project.gpid, @jenkins_address + "/project/#{user_name}_#{identifier}")
# build job # build job
opts = {'build_start_timeout' => 30, 'cancel_on_build_start_timeout' => true} logger.error("Test#{jenkins_job}")
code = @client.job.build("#{user_name}_#{identifier}", opts)
# sucessed will return "201" # return '201' if successed
raise "Unable to build job: #{user_name}_#{identifier}" unless code == '201' code = @client.job.build("#{user_name}_#{identifier}")
if qa.blank? && code == '201' if qa.blank?
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier, 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 => job_name)
else else
qa.update_attribute(:sonar_version, version) qa.update_attribute(:sonar_version, version)
end end
end end
rescue => e
puts e
end
end end
def index def index
@ -90,10 +100,11 @@ class QualityAnalysisController < ApplicationController
def connect_jenkins def connect_jenkins
@gitlab_address = Redmine::Configuration['gitlab_address'] @gitlab_address = Redmine::Configuration['gitlab_address']
@jenkins_address = Redmine::Configuration['jenkins_address'] @jenkins_address = Redmine::Configuration['jenkins_address']
# connect jenkins # connect jenkins
@client = JenkinsApi::Client.new(:server_url => @jenkins_address, :username => "temp", :password => '123123') @client = JenkinsApi::Client.new(:server_url => @jenkins_address, :username => "temp", :password => '123123')
rescue rescue => e
logger.error("failed to connect Jenkins") logger.error("failed to connect Jenkins ==> #{e}")
end end
end end