class QualityAnalysisController < ApplicationController before_filter :find_project_by_project_id#, :except => [:getattachtype] before_filter :authorize layout "base_projects" include ApplicationHelper require 'jenkins_api_client' require 'nokogiri' require 'json' require 'open-uri' def show end def create @client = JenkinsApi::Client.new(:server_url => 'http://123.59.135.93:8890', :username => "temp", :password => '123123') #@client.exists?(job_name) @g = Gitlab.client gitlab_address = Redmine::Configuration['gitlab_address'] user_name = User.find(params[:user_id]).try(:login) branch = params[:branch].nil? ? "master" : params[:branch] language = params[:language] path = params[:path] identifier = params[:identifier] qa = QualityAnalysis.where(:project_id => @project.id).first version = 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 @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 # # replace config.xml of jenkins @client = @client.job.create("#{user_name}_#{identifier}", @doc.to_xml) # relace gitlab hook # genkins address @g.add_project_hook(@project.gpid,"http://123.59.135.93:8890/project/#{user_name}_#{identifier}") if qa.nil? QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier, :version => version) else qa.update_attribute(:version, version) end end def index if params[:name].nil? @name_flag = true projects_date = open("http://123.59.135.93:8891/api/projects/index").read @results = JSON.parse(projects_date) else @name_flag = false project_key = params[:name] data = open("http://123.59.135.93:8891/api/resources/index?resource=#{project_key}&depth=-1&metrics=complexity,class_complexity,lines,comment_lines,blocker_violations").read @cc =JSON.parse(data) end 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 # 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 end