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,43 +14,53 @@ class QualityAnalysisController < ApplicationController
end
def create
user_name = User.find(params[:user_id]).try(:login)
identifier = params[: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}
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
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.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)
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
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
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
@ -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