|
|
|
@ -1,7 +1,8 @@
|
|
|
|
|
class QualityAnalysisController < ApplicationController
|
|
|
|
|
before_filter :find_project_by_project_id#, :except => [:getattachtype]
|
|
|
|
|
before_filter :find_quality_analysis, :only => [:edit, :update_jenkins_job]
|
|
|
|
|
before_filter :authorize
|
|
|
|
|
before_filter :connect_jenkins, :only => [:create]
|
|
|
|
|
before_filter :connect_jenkins, :only => [:create, :edit, :update_jenkins_job]
|
|
|
|
|
layout "base_projects"
|
|
|
|
|
include ApplicationHelper
|
|
|
|
|
require 'jenkins_api_client'
|
|
|
|
@ -54,7 +55,7 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
# 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'
|
|
|
|
|
d = @client.job.delete("#{job_name}") if jenkins_job == '200' && code != '201'
|
|
|
|
|
logger.error("delete result ==> #{code}")
|
|
|
|
|
if qa.blank? && code == '201'
|
|
|
|
|
QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier,
|
|
|
|
@ -86,11 +87,54 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def edit
|
|
|
|
|
@quality_analysis = QualityAnalysis.where(:id => params[:id])
|
|
|
|
|
@g = Gitlab.client
|
|
|
|
|
gitlab_branches = @g.branches(@project.gpid)
|
|
|
|
|
@branch_names = gitlab_branches.map{|b| b.name}
|
|
|
|
|
@gitlab_default_branch = @g.project(@project.gpid).default_branch
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
|
|
|
|
@quality_analysis = QualityAnalysis.where(:id => params[:id])
|
|
|
|
|
def update_jenkins_job
|
|
|
|
|
begin
|
|
|
|
|
rep_id = Repository.where(:project_id => @project.id).first.try(:id)
|
|
|
|
|
logger.error("#############################===>666")
|
|
|
|
|
sonar_name = @quality_analysis.sonar_name
|
|
|
|
|
job_name = "#{@quality_analysis.author_login}-#{rep_id}"
|
|
|
|
|
version = @quality_analysis.sonar_version
|
|
|
|
|
path = params[:path].blank? ? "./" : params[:path]
|
|
|
|
|
language = swith_language_type(params[:language])
|
|
|
|
|
branch = params[:branch]
|
|
|
|
|
identifier = @quality_analysis.rep_identifier
|
|
|
|
|
properties = "sonar.projectKey=#{sonar_name}
|
|
|
|
|
sonar.projectName=#{sonar_name}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
# return '200' if successed
|
|
|
|
|
jenkins_job = @client.job.update("#{job_name}", @doc.to_xml)
|
|
|
|
|
logger.error("Failed to update job: ==> #{jenkins_job}") unless jenkins_job == '200'
|
|
|
|
|
if jenkins_job == '200'
|
|
|
|
|
logger.info("quality_ananlysis will be updated: ==> #{jenkins_job}")
|
|
|
|
|
@quality_analysis.path = path
|
|
|
|
|
@quality_analysis.language = language
|
|
|
|
|
@quality_analysis.branch = branch
|
|
|
|
|
@quality_analysis.save
|
|
|
|
|
end
|
|
|
|
|
rescue Exception => e
|
|
|
|
|
logger.error("Update jenkins job: #{e}")
|
|
|
|
|
end
|
|
|
|
|
respond_to do |format|
|
|
|
|
|
format.html{redirect_to project_quality_analysis_path(:project_id => @project.id)}
|
|
|
|
|
format.js
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# resource_id: login + @repository.id
|
|
|
|
@ -123,6 +167,14 @@ class QualityAnalysisController < ApplicationController
|
|
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_quality_analysis
|
|
|
|
|
begin
|
|
|
|
|
@quality_analysis = QualityAnalysis.find(params[:id])
|
|
|
|
|
rescue
|
|
|
|
|
render_404
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Authorize the user for the requested action
|
|
|
|
|
def authorize(ctrl = params[:controller], action = params[:action], global = false)
|
|
|
|
|
unless @project.archived? && @project.gpid.nil?
|
|
|
|
|