From f50c4a879b1e1d56df4110201f8b38f69a78088b Mon Sep 17 00:00:00 2001 From: huang Date: Tue, 5 Jul 2016 17:35:56 +0800 Subject: [PATCH 1/7] modified total commits --- app/controllers/repositories_controller.rb | 13 +++++++------ lib/gitlab-cli/lib/gitlab/client/repositories.rb | 11 +++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index d0904ddf5..ca5d7040e 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -38,7 +38,7 @@ class RepositoriesController < ApplicationController before_filter :find_changeset, :only => [:revision, :add_related_issue, :remove_related_issue] before_filter :authorize , :except => [:newrepo,:newcreate,:fork, :to_gitlab, :forked, :commit_diff, :project_archive, :quality_analysis] # 链接gitlab - before_filter :connect_gitlab, :only => [:quality_analysis] + before_filter :connect_gitlab, :only => [:quality_analysis, :show] accept_rss_auth :revisions # hidden repositories filter // 隐藏代码过滤器 before_filter :check_hidden_repo, :only => [:show, :stats, :revisions, :revision, :diff ] @@ -370,11 +370,11 @@ update if request.xhr? @entries ? render(:partial => 'dir_list_content') : render(:nothing => true) else - g = Gitlab.client - @changesets = g.commits(@project.gpid, :ref_name => @rev) - g_project = g.project(@project.gpid) + @changesets = @g.commits(@project.gpid, :ref_name => @rev) + g_project = @g.project(@project.gpid) # 总的提交数 - @changesets_all_count = @project.gpid.nil? ? 0 : commit_count(@project, @rev) + @changesets_all_count = @g.user_static(@project.gpid, :rev => @rev).count + # @changesets_all_count = @project.gpid.nil? ? 0 : commit_count(@project, @rev) @g_default_branch = g_project.default_branch.nil? ? "master" : g_project.default_branch # 访问该页面的是会后则刷新 if @project.project_score.nil? @@ -384,9 +384,10 @@ update if @changesets_all_count != @project.project_score.changeset_num && @changesets_all_count != 0 update_commits_count(@project, @changesets_all_count) end + # 最近一次提交 @changesets_latest_coimmit = @changesets[0] - unless @changesets[0].blank? + unless @changesets_latest_coimmit.blank? update_commits_date(@project, @changesets_latest_coimmit) end @creator = User.where("id =?", @project.user_id).first.try(:login) diff --git a/lib/gitlab-cli/lib/gitlab/client/repositories.rb b/lib/gitlab-cli/lib/gitlab/client/repositories.rb index 34915967c..e998ab56b 100644 --- a/lib/gitlab-cli/lib/gitlab/client/repositories.rb +++ b/lib/gitlab-cli/lib/gitlab/client/repositories.rb @@ -75,6 +75,17 @@ class Gitlab::Client end alias_method :repo_commits, :commits_total_count + # Gets total project commits. + # + # @example + # @param [Integer] project The ID of a project. + # @param [Hash] options A customizable set of options. + # @option options [String] :rev The branch or tag name of a project repository. + # @return [Hash] + def user_static(project, options={}) + get("/projects/#{project}/repository/user_static", :query => options) + end + # Gets a specific commit identified by the commit hash or name of a branch or tag. # # @example From 2f8079e85c80b82e79be024ac5a9b7e67a04197d Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 6 Jul 2016 16:07:17 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E7=89=88=E6=9C=AC=20=E5=BA=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- app/controllers/repositories_controller.rb | 38 ++++++++++----------- app/views/repositories/_navigation.html.erb | 6 ++-- app/views/repositories/show.html.erb | 24 +++++-------- 4 files changed, 32 insertions(+), 38 deletions(-) diff --git a/Gemfile b/Gemfile index bb94100a5..250176066 100644 --- a/Gemfile +++ b/Gemfile @@ -57,7 +57,7 @@ group :development do gem 'grape-swagger' gem 'better_errors', '~> 1.1.0' # gem "query_reviewer" - # gem 'rack-mini-profiler', '~> 0.9.3' + gem 'rack-mini-profiler', '~> 0.9.3' if RUBY_PLATFORM =~ /w32/ gem 'win32console' end diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index ca5d7040e..12e41a83a 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -365,41 +365,41 @@ update render_403 return end + # 获取版本库目录结构 @entries = @repository.entries(@path, @rev) - @changeset = @repository.find_changeset_by_name(@rev) if request.xhr? @entries ? render(:partial => 'dir_list_content') : render(:nothing => true) else @changesets = @g.commits(@project.gpid, :ref_name => @rev) + # 最近一次提交 + @changesets_latest_coimmit = @changesets[0] g_project = @g.project(@project.gpid) # 总的提交数 @changesets_all_count = @g.user_static(@project.gpid, :rev => @rev).count - # @changesets_all_count = @project.gpid.nil? ? 0 : commit_count(@project, @rev) + + # 获取默认分支 @g_default_branch = g_project.default_branch.nil? ? "master" : g_project.default_branch - # 访问该页面的是会后则刷新 - if @project.project_score.nil? + + # 访问版本庫后更新project_score表数据;changeset_num为提交总数 + project_score = @project.project_score + if project_score.nil? ProjectScore.create(:project_id => @project.id, :score => false) - end - # 刷新改页面的时候,更新统计数 - if @changesets_all_count != @project.project_score.changeset_num && @changesets_all_count != 0 - update_commits_count(@project, @changesets_all_count) + else + project_score.update_column(:changeset_num, @changesets_all_count) end - # 最近一次提交 - @changesets_latest_coimmit = @changesets[0] - unless @changesets_latest_coimmit.blank? - update_commits_date(@project, @changesets_latest_coimmit) - end - @creator = User.where("id =?", @project.user_id).first.try(:login) - # @properties = @repository.properties(@path, @rev) - # @repositories = @project.repositories - # project_path_cut = RepositoriesHelper::PROJECT_PATH_CUT - # ip = RepositoriesHelper::REPO_IP_ADDRESS + # unless @changesets_latest_coimmit.blank? + # update_commits_date(@project, @changesets_latest_coimmit) + # end + @creator = @project.owner.to_s gitlab_address = Redmine::Configuration['gitlab_address'] + # REDO:需优化,仅测试用 @zip_path = Gitlab.endpoint.to_s + "/projects/" + @project.gpid.to_s + "/repository/archive?&private_token=" + Gitlab.private_token + + # 获取版本库路径,主要分为两种,一种随Gitlab类型,另一种为Git类型,Git类型为无用数据,最终需要删掉这种类型。 if @repository.type.to_s == "Repository::Gitlab" - @repos_url = gitlab_address.to_s+"/"+@project.owner.to_s+"/"+@repository.identifier+"."+"git" + @repos_url = gitlab_address.to_s+"/" + @creator + "/" + @repository.identifier+"."+"git" else @repos_url = "http://"+@repository.login.to_s+"_"+@repository.identifier.to_s+"@"+ip.to_s + @repository.url.slice(project_path_cut, @repository.url.length).to_s end diff --git a/app/views/repositories/_navigation.html.erb b/app/views/repositories/_navigation.html.erb index d99df5ed1..bf7c19879 100644 --- a/app/views/repositories/_navigation.html.erb +++ b/app/views/repositories/_navigation.html.erb @@ -21,9 +21,9 @@ <%= select_tag :branch, options_for_select(@repository.branches, @rev), :id => 'branch' %> <% end -%> - <% if !@repository.tags.nil? && @repository.tags.length > 0 -%> - <%= select_tag :tag, options_for_select([''] + @repository.tags, @rev), :id => 'tag', :style=>" display:none" %> - <% end -%> + <%# if !@repository.tags.nil? && @repository.tags.length > 0 -%> + <%#= select_tag :tag, options_for_select([''] + @repository.tags, @rev), :id => 'tag', :style=>" display:none" %> + <%# end -%> <% if @repository.supports_all_revisions? %> <%= hidden_field_tag 'rev', @rev, :size => 8 %> diff --git a/app/views/repositories/show.html.erb b/app/views/repositories/show.html.erb index 05f25822b..f4a50dfb9 100644 --- a/app/views/repositories/show.html.erb +++ b/app/views/repositories/show.html.erb @@ -1,17 +1,11 @@ -<%= call_hook(:view_repositories_show_contextual, {:repository => @repository, :project => @project}) %> +<%#= call_hook(:view_repositories_show_contextual, {:repository => @repository, :project => @project}) %>

<%= render :partial => 'breadcrumbs', :locals => {:path => @path, :kind => 'dir', :revision => @rev} %>

<% unless @entries.nil? %> ZIP下载 - <%# if is_project_manager?(User.current, @project.id) && QualityAnalysis.where(:project_id => @project.id).first.nil? %> - <%# if User.current.member_of?(@project) %> <% if quality_analysis(User.current.try(:login), @repository.id).nil? && User.current.member_of?(@project) && @project.is_public? %> <%= link_to "质量分析", quality_analysis_path(:id => @project.id, :repository_id => @repository.identifier, :rev => @rev, :default_branch => @g_default_branch ), :remote => true, :class => "btn_zipdown fr" %> <% end %> - <%# end %> - <%# else %> - <%#= link_to "质量分析", project_quality_analysis_path(:project_id => @project.id, :resource_id => @proje), :class => "btn_zipdown fr" %> - <%# end %> <% end %>
@@ -23,29 +17,29 @@ 该版本库还没有上传代码!
<% end %> - <% if @repository.type.to_s=="Repository::Gitlab" %> + <% if @repository.type.to_s == "Repository::Gitlab" %> 版本库地址:<%= @repos_url %> <% else %> 版本库地址:<%= h @repository.url %> <% end %> -
<% else %> <%= render :partial => 'navigation' %>
克隆网址:
- +
<%=link_to "代码统计", stats_repository_project_path(:id => @project.id, :repository_id => @repository.identifier, :rev => @rev, :creator => @creator, :default_branch => @g_default_branch ), :class => "fl vl_zip" %>
- <%# 针对公开项目:用户必须创建了项目,否则用户无法同步 %> + <% if User.current.id != @project.user_id %>
<%= link_to "".html_safe+"Fork", {:controller => 'repositories', :action => 'forked'}, :class=>"vl_btn", :target => "_blank", :confirm=>"平台将为您创建一个新的同名项目和版本库,请问是否继续?" %> <%= @project.forked_count.to_i %>
<% end %>
+
<% if @changesets && !@changesets.empty? %> <% if !user_commit_rep(@changesets_latest_coimmit.author_email).nil? %> @@ -77,7 +71,7 @@ <%# 数据统计 %> <%#= render :partial => 'summary' %> <%# end %> - <%= render :partial => 'dir_list' %> + <%#= render :partial => 'dir_list' %> <% end %> <%#= render_properties(@properties) %> @@ -87,8 +81,8 @@ 如何提交代码
-<% content_for :header_tags do %> - <%= stylesheet_link_tag "scm" %> -<% end %> +<%# content_for :header_tags do %> + <%#= stylesheet_link_tag "scm" %> +<%# end %> <% html_title(l(:label_repository)) -%> From 88f87524905ab99af441bce28b9c912025d52510 Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 6 Jul 2016 16:37:19 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/repositories_controller.rb | 24 +++++++++++++------ .../repositories/_dir_list_content.html.erb | 17 ++++--------- app/views/repositories/show.html.erb | 12 +--------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 12e41a83a..4764d1cd7 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -39,6 +39,8 @@ class RepositoriesController < ApplicationController before_filter :authorize , :except => [:newrepo,:newcreate,:fork, :to_gitlab, :forked, :commit_diff, :project_archive, :quality_analysis] # 链接gitlab before_filter :connect_gitlab, :only => [:quality_analysis, :show] + # 版本库新增权限 + before_filter :show_rep, :only => [:show] accept_rss_auth :revisions # hidden repositories filter // 隐藏代码过滤器 before_filter :check_hidden_repo, :only => [:show, :stats, :revisions, :revision, :diff ] @@ -361,10 +363,7 @@ update def show ## TODO: the below will move to filter, done. - if !User.current.member_of?(@project) && @project.hidden_repo - render_403 - return - end + # 获取版本库目录结构 @entries = @repository.entries(@path, @rev) if request.xhr? @@ -654,9 +653,20 @@ update # 链接gitlab def connect_gitlab - @g = Gitlab.client - unless @project.gpid.nil? - @g_project = @g.project(@project.gpid) + begin + @g = Gitlab.client + unless @project.gpid.nil? + @g_project = @g.project(@project.gpid) + end + rescue => e + logger.error("failed to connect gitlab ==> #{e}") + end + end + + def show_rep + if !User.current.member_of?(@project) && @project.hidden_repo + render_403 + return end end diff --git a/app/views/repositories/_dir_list_content.html.erb b/app/views/repositories/_dir_list_content.html.erb index 4dee108ee..7a378dcb2 100644 --- a/app/views/repositories/_dir_list_content.html.erb +++ b/app/views/repositories/_dir_list_content.html.erb @@ -9,7 +9,7 @@ <% if entry.is_dir? %> -<%# 展开文件目录 %> + <%# 展开文件目录 %>   <% end %> - - - - <%#= h(ent_name), :class => "(entry.is_dir? ? 'icon icon-folder' : "icon icon-file #{Redmine::MimeType.css_class_of(ent_name)}")%> -<%= link_to h(ent_name), - {:action => (entry.is_dir? ? 'show' : 'entry'), :id => @project, :repository_id => @repository.identifier_param, :path => to_path_param(ent_path), :rev => @rev}, - :class => (entry.is_dir? ? 'icon icon-folder' : "icon icon-file #{Redmine::MimeType.css_class_of(ent_name)}")%> + <%= link_to h(ent_name), + {:action => (entry.is_dir? ? 'show' : 'entry'), :id => @project, :repository_id => @repository.identifier_param, :path => to_path_param(ent_path), :rev => @rev}, + :class => (entry.is_dir? ? 'icon icon-folder' : "icon icon-file #{Redmine::MimeType.css_class_of(ent_name)}")%> - - -<%# if @repository.report_last_commit %>
-<%# end %> <% end %> diff --git a/app/views/repositories/show.html.erb b/app/views/repositories/show.html.erb index f4a50dfb9..823d0c1ad 100644 --- a/app/views/repositories/show.html.erb +++ b/app/views/repositories/show.html.erb @@ -65,19 +65,9 @@
<% end %> - - <% if !@entries.nil? && authorize_for('repositories', 'browse') %> - <%# 数据统计 %> - <%#= render :partial => 'summary' %> - <%# end %> - <%#= render :partial => 'dir_list' %> + <%= render :partial => 'dir_list' %> <% end %> -<%#= render_properties(@properties) %> - - -<%#= render_properties(@properties) %> - 如何提交代码 From 92b6171c36d7fd931bd1210d23621018c6be7757 Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 7 Jul 2016 11:08:29 +0800 Subject: [PATCH 4/7] sonar error puts --- .../quality_analysis_controller.rb | 25 ++++++++----------- app/views/quality_analysis/create.html.erb | 20 ++++++++------- .../repositories/_quality_analysis.html.erb | 2 +- public/stylesheets/public.css | 4 +-- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/app/controllers/quality_analysis_controller.rb b/app/controllers/quality_analysis_controller.rb index bec66aeff..4c67ef621 100644 --- a/app/controllers/quality_analysis_controller.rb +++ b/app/controllers/quality_analysis_controller.rb @@ -61,7 +61,7 @@ class QualityAnalysisController < ApplicationController # 判断调用sonar分析是否成功 # 等待启动时间处理, 最长时间为30分钟 for i in 0..60 do - sleep(60) + sleep(30) @current_build_status = @client.job.get_current_build_status("#{job_name}") if (@current_build_status != "not_run" || @current_build_status != "running") break @@ -74,23 +74,27 @@ class QualityAnalysisController < ApplicationController @console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text') logger.info("@current_build_status is ==> #{@current_build_status}") - logger.info("@console_build is ==> #{@console_build}") - d = @client.job.delete("#{job_name}") if jenkins_job == '200' && code != '201' logger.error("delete result ==> #{code}") + if @current_build_status == "success" + SonarError.create() + end if qa.blank? && @current_build_status == "success" QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier, :sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => "#{user_name}:#{rep_id}") else - qa.update_attribute(:sonar_version, version) + qa.update_attribute(:sonar_version, version) unless qa.blank? end end rescue => e puts e end respond_to do |format| - format.html{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch, :current_build_status => @current_build_status, :job_name => job_name)} - # format.js{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch)} + if @current_build_status == "success" + format.html{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch, :current_build_status => @current_build_status, :job_name => job_name)} + elsif @current_build_status == "failure" + format.html + end end end @@ -145,7 +149,7 @@ class QualityAnalysisController < ApplicationController get_current_build_status = @client.job.get_current_build_status("Hjqreturn-1280") logger.error("Failed to update job: ==> #{jenkins_job}") unless jenkins_job == '200' - # 数据更新到Trustie数据库 + # 数据更新到Trustie数据 if jenkins_job == '200' logger.info("quality_ananlysis will be updated: ==> #{jenkins_job}") @quality_analysis.path = path @@ -168,18 +172,12 @@ class QualityAnalysisController < ApplicationController @branch = params[:branch] @resource_id = params[:resource_id] @sonar_address = Redmine::Configuration['sonar_address'] - @jenkins_address = Redmine::Configuration['jenkins_address'] if params[:resource_id].nil? @name_flag = true projects_date = open(@sonar_address + "/api/projects/index").read arr = JSON.parse(projects_date).map {|m| m["nm"]} # eg: ["Hjqreturn:cc_rep", "Hjqreturn:putong", "Hjqreturn:sonar_rep2", "shitou:sonar_rep"] @quality_analyses = QualityAnalysis.where(:project_id => @project.id).select{|qa| arr.include?(qa.sonar_name)} - else - if params[:current_build_status] == "failure" - job_name = params[:job_name] - @console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text')["output"] - end complexity_date = open(@sonar_address + "/api/resources/index?resource=#{@resource_id}&depth=0&metrics=sqale_rating,function_complexity,duplicated_lines_density,comment_lines_density,sqale_index,lines,file_line,files,functions,classes,directories").read @complexity =JSON.parse(complexity_date).first issue_date = open(@sonar_address + "/api/resources/index?resource=#{@resource_id}&depth=0&metrics=blocker_violations,critical_violations,major_violations,minor_violations,info_violations,violations").read @@ -188,7 +186,6 @@ class QualityAnalysisController < ApplicationController rescue => e puts e end - end # Find project of id params[:project_id] diff --git a/app/views/quality_analysis/create.html.erb b/app/views/quality_analysis/create.html.erb index e7c81f662..a0a9d7254 100644 --- a/app/views/quality_analysis/create.html.erb +++ b/app/views/quality_analysis/create.html.erb @@ -1,9 +1,11 @@ -<% if @current_build_status == "success" %> - <%= render :partial => "show", :locals => {:branch => params[:branch]} %> -<% else %> - <% if @build_console_result %> - 运行结果超时 - <% else %> - <%= render :partial => "console_output" %> - <% end %> -<% end %> \ No newline at end of file +
+

质量分析

+
+
+
本次分析失败,原因如下:
+ <% if @current_build_status == "failure" %> + <%= h @console_build.to_json %> + <% elsif @build_console_result == false %> + 分析超时 + <% end %> +
\ No newline at end of file diff --git a/app/views/repositories/_quality_analysis.html.erb b/app/views/repositories/_quality_analysis.html.erb index 0053849a3..936e513f5 100644 --- a/app/views/repositories/_quality_analysis.html.erb +++ b/app/views/repositories/_quality_analysis.html.erb @@ -15,7 +15,7 @@ <%= select_tag :language, options_for_select(["java","python","ruby","c++","c#","c"]), :id => 'branch', :class => "analysis-option-box" %> - +
diff --git a/public/stylesheets/public.css b/public/stylesheets/public.css index 7cfb7f9e0..9662181df 100644 --- a/public/stylesheets/public.css +++ b/public/stylesheets/public.css @@ -246,7 +246,7 @@ a.c_green{ color:#28be6c;} .b_blue{background:#64bdd9;} .b_green{background:#28be6c;} .b_slow_yellow{background:#adde18;} -.b_yellow{background:#DDDF0D;} +.b_yellow{background:#e4de4b;} .b_slow_red{background:#df8538;} .b_green2 {background:#63c360;} .b_red {background:#d60308;} @@ -1184,4 +1184,4 @@ a.shadowbox_news_all{ display:block; width:305px; height:40px; line-height:40px; .reply_iconup{ position:absolute; top:21px; left:13px; color:#d4d4d4; font-size:16px; background:#f1f1f1; line-height:13px;} /*20160622代码分析弹窗*/ -.analysis-option-box {width:100%; border:1px solid #ccc; padding:3px 5px;} +.analysis-option-box {width:100%; border:1px solid #ccc; padding:3px 5px;} From b90c90bc60c923d8225d6cf469adcc5d5aada794 Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 7 Jul 2016 14:22:48 +0800 Subject: [PATCH 5/7] add model error_list --- .../quality_analysis_controller.rb | 39 +++++++++++++----- app/models/sonar_error.rb | 3 ++ app/views/quality_analysis/create.html.erb | 6 +-- .../quality_analysis/error_list.html.erb | 11 +++++ config/routes.rb | 1 + .../20160707031248_create_sonar_errors.rb | 11 +++++ db/schema.rb | 41 ++++++++++++++++--- spec/factories/sonar_errors.rb | 7 ++++ spec/models/sonar_error_spec.rb | 5 +++ 9 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 app/models/sonar_error.rb create mode 100644 app/views/quality_analysis/error_list.html.erb create mode 100644 db/migrate/20160707031248_create_sonar_errors.rb create mode 100644 spec/factories/sonar_errors.rb create mode 100644 spec/models/sonar_error_spec.rb diff --git a/app/controllers/quality_analysis_controller.rb b/app/controllers/quality_analysis_controller.rb index 4c67ef621..f18cc7d46 100644 --- a/app/controllers/quality_analysis_controller.rb +++ b/app/controllers/quality_analysis_controller.rb @@ -72,18 +72,28 @@ class QualityAnalysisController < ApplicationController end end - @console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text') + # 获取sonar output结果 + console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text') logger.info("@current_build_status is ==> #{@current_build_status}") - d = @client.job.delete("#{job_name}") if jenkins_job == '200' && code != '201' - logger.error("delete result ==> #{code}") - if @current_build_status == "success" - SonarError.create() - end - if qa.blank? && @current_build_status == "success" - QualityAnalysis.create(:project_id => @project.id, :author_login => user_name, :rep_identifier => identifier, - :sonar_version => version, :path => path, :branch => branch, :language => language, :sonar_name => "#{user_name}:#{rep_id}") + + # 两种情况需要删除job: + # 1/创建成功但是build失败则删除job + # 2/creat和build成功,调用sonar启动失败则删除job + # 错误信息存储需存到Trustie数据库,否则一旦job删除则无法获取这些信息 + if jenkins_job == '200' && code != '201' + @client.job.delete("#{job_name}") else - qa.update_attribute(:sonar_version, version) unless qa.blank? + if @current_build_status == "failure" + @sonar_error = SonarError.create(:project_id => @project.id, :jenkins_job_name => job_name, :output => console_build) + @client.job.delete("#{job_name}") + elsif @current_build_status == "success" + 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 => "#{user_name}:#{rep_id}") + else + qa.update_attribute(:sonar_version, version) + end + end end end rescue => e @@ -93,11 +103,18 @@ class QualityAnalysisController < ApplicationController if @current_build_status == "success" format.html{redirect_to project_quality_analysis_path(:project_id => @project.id, :resource_id => sonar_name, :branch => branch, :current_build_status => @current_build_status, :job_name => job_name)} elsif @current_build_status == "failure" - format.html + format.html{redirect_to error_list_project_quality_analysi_path(:project_id => @project.id, :job_name => job_name)} end end end + def error_list + @error_list = SonarError.where(:jenkins_job_name => params[:job_name]).first + respond_to do |format| + format.html + end + end + # get language type def swith_language_type language if language == "c#" diff --git a/app/models/sonar_error.rb b/app/models/sonar_error.rb new file mode 100644 index 000000000..05fcfc5b9 --- /dev/null +++ b/app/models/sonar_error.rb @@ -0,0 +1,3 @@ +class SonarError < ActiveRecord::Base + attr_accessible :jenkins_job_name, :output, :project_id +end diff --git a/app/views/quality_analysis/create.html.erb b/app/views/quality_analysis/create.html.erb index a0a9d7254..4f01fa2c8 100644 --- a/app/views/quality_analysis/create.html.erb +++ b/app/views/quality_analysis/create.html.erb @@ -3,9 +3,9 @@
本次分析失败,原因如下:
- <% if @current_build_status == "failure" %> - <%= h @console_build.to_json %> - <% elsif @build_console_result == false %> + <% if @build_console_result == false %> 分析超时 + <% else %> + <%= h @sonar_error.to_json %> <% end %>
\ No newline at end of file diff --git a/app/views/quality_analysis/error_list.html.erb b/app/views/quality_analysis/error_list.html.erb new file mode 100644 index 000000000..217990b13 --- /dev/null +++ b/app/views/quality_analysis/error_list.html.erb @@ -0,0 +1,11 @@ +
+

质量分析

+
+
+
本次分析失败,原因如下:
+ <% if @build_console_result == false %> + 分析超时 + <% else %> + <%= h @error_list.to_json %> + <% end %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index bc9abeb35..0ef4c9fcb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -797,6 +797,7 @@ RedmineApp::Application.routes.draw do match 'update_jenkins_job' match 'edit' match 'create' + get 'error_list' end end # resources :files, :only => [:index, :new, :create] do diff --git a/db/migrate/20160707031248_create_sonar_errors.rb b/db/migrate/20160707031248_create_sonar_errors.rb new file mode 100644 index 000000000..c55caccce --- /dev/null +++ b/db/migrate/20160707031248_create_sonar_errors.rb @@ -0,0 +1,11 @@ +class CreateSonarErrors < ActiveRecord::Migration + def change + create_table :sonar_errors do |t| + t.integer :project_id + t.string :jenkins_job_name + t.text :output + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 3258f89c1..93dbba8a4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20160627090316) do +ActiveRecord::Schema.define(:version => 20160707031248) do create_table "activities", :force => true do |t| t.integer "act_id", :null => false @@ -57,6 +57,18 @@ ActiveRecord::Schema.define(:version => 20160627090316) do t.integer "user_id", :null => false end + create_table "apply_add_schools", :force => true do |t| + t.string "name" + t.string "province" + t.string "city" + t.string "address" + t.string "remarks" + t.integer "school_id" + t.integer "status", :default => 0 + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "apply_homeworks", :force => true do |t| t.integer "status" t.integer "user_id" @@ -1633,10 +1645,11 @@ ActiveRecord::Schema.define(:version => 20160627090316) do create_table "schools", :force => true do |t| t.string "name" t.string "province" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "logo_link" t.string "pinyin" + t.integer "school_type", :default => 0 end create_table "secdomains", :force => true do |t| @@ -1725,6 +1738,14 @@ ActiveRecord::Schema.define(:version => 20160627090316) do t.integer "project_id" end + create_table "sonar_errors", :force => true do |t| + t.integer "project_id" + t.string "jenkins_job_name" + t.text "output" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + create_table "ssos", :force => true do |t| t.integer "user_id" t.string "openid" @@ -1849,8 +1870,18 @@ ActiveRecord::Schema.define(:version => 20160627090316) do t.string "title" t.text "description" t.integer "user_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + t.string "eng_name" + t.integer "syllabus_type" + t.integer "credit" + t.integer "hours" + t.integer "theory_hours" + t.integer "practice_hours" + t.string "applicable_major" + t.string "pre_course" + t.integer "visits", :default => 0 + t.integer "des_status", :default => 0 end add_index "syllabuses", ["user_id"], :name => "index_syllabuses_on_user_id" diff --git a/spec/factories/sonar_errors.rb b/spec/factories/sonar_errors.rb new file mode 100644 index 000000000..a5ed5485f --- /dev/null +++ b/spec/factories/sonar_errors.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :sonar_error do + project_id 1 + jenkins_job_name "MyString" + output "MyText" + end +end diff --git a/spec/models/sonar_error_spec.rb b/spec/models/sonar_error_spec.rb new file mode 100644 index 000000000..9aae8db29 --- /dev/null +++ b/spec/models/sonar_error_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SonarError, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end From ec3220480906158c5f1f715ed3e0c145c25069de Mon Sep 17 00:00:00 2001 From: huang Date: Thu, 7 Jul 2016 15:53:07 +0800 Subject: [PATCH 6/7] sql_index --to chinese --- app/views/quality_analysis/_show.html.erb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/views/quality_analysis/_show.html.erb b/app/views/quality_analysis/_show.html.erb index 1a7869fd0..0b16a1adb 100644 --- a/app/views/quality_analysis/_show.html.erb +++ b/app/views/quality_analysis/_show.html.erb @@ -43,7 +43,15 @@
质量等级<%=@complexity["msr"][9].nil? ? 0 : score_sqale_rating(@complexity["msr"][9]["val"].to_i) %>/5分 可定性评价为:质量<%=@complexity["msr"][9].nil? ? "很好" : sqale_rating_status(@complexity["msr"][9]["val"])[0] %>
-
技术债务<%=@complexity["msr"][8].nil? ? 0 : @complexity["msr"][8]["frmt_val"] %> +
技术债务 + + <% if @complexity["msr"][8].nil? %> + 0 + <% else %> + <%= /[0-9]*/.match(@complexity["msr"][8]["frmt_val"])[0] %>天 + <%= / [0-9]*/.match(@complexity["msr"][8]["frmt_val"]) %>小时 + <% end %> + 查看详情
From 406d636aacb5e9db9d8a167b4822d2374d8e4d07 Mon Sep 17 00:00:00 2001 From: huang Date: Fri, 8 Jul 2016 11:27:03 +0800 Subject: [PATCH 7/7] add error system --- .../quality_analysis_controller.rb | 24 +++++++++++++++---- .../quality_analysis/error_list.html.erb | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/controllers/quality_analysis_controller.rb b/app/controllers/quality_analysis_controller.rb index f18cc7d46..c53e1939a 100644 --- a/app/controllers/quality_analysis_controller.rb +++ b/app/controllers/quality_analysis_controller.rb @@ -26,14 +26,25 @@ class QualityAnalysisController < ApplicationController job_name = "#{user_name}-#{rep_id}" sonar_name = "#{user_name}:#{rep_id}" + # 考虑到历史数据:有些用户创建类job但是build失败,即sonar没有结果,这个时候需要把job删除,并且删掉quality_analyses表数据 + # 如果不要这句则需要迁移数据 + @sonar_address = Redmine::Configuration['sonar_address'] + projects_date = open(@sonar_address + "/api/projects/index").read + arr = JSON.parse(projects_date).map {|m| m["nm"]} # eg: ["Hjqreturn:cc_rep", "Hjqreturn:putong", "Hjqreturn:sonar_rep2", "shitou:sonar_rep"] + quality_an = QualityAnalysis.where(:sonar_name => sonar_name).first + if @client.job.exists?(job_name) && QualityAnalysis.where(:sonar_name => sonar_name).select{|qa| arr.include?(qa.sonar_name)}.blank? + logger.info("88888888888888888888") + aa = @client.job.delete("#{job_name}") + quality_an.delete unless quality_an.blank? + end # Checks if the given job exists in Jenkins. unless @client.job.exists?(job_name) @g = Gitlab.client branch = params[:branch] language = swith_language_type(params[:language]) path = params[:path].blank? ? "./" : params[:path] - qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first - version = qa.nil? ? 1 : qa.sonar_version + 1 + # qa = QualityAnalysis.where(:project_id => @project.id, :author_login => user_name).first + version = quality_an.nil? ? 1 : quality_an.sonar_version + 1 properties = "sonar.projectKey=#{sonar_name} sonar.projectName=#{sonar_name} sonar.projectVersion=#{version} @@ -73,7 +84,7 @@ class QualityAnalysisController < ApplicationController end # 获取sonar output结果 - console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text') + console_build = @client.job.get_console_output("#{job_name}", build_num = 0, start = 0, mode = 'text')["output"] logger.info("@current_build_status is ==> #{@current_build_status}") # 两种情况需要删除job: @@ -84,10 +95,13 @@ class QualityAnalysisController < ApplicationController @client.job.delete("#{job_name}") else if @current_build_status == "failure" - @sonar_error = SonarError.create(:project_id => @project.id, :jenkins_job_name => job_name, :output => console_build) + reg_console = /Exception:.*?\r/.match(console_build) + output = reg_console[0].gsub("\r", "") unless reg_console.nil? + se = SonarError.where(:jenkins_job_name => job_name).first + se.nil? ? SonarError.create(:project_id => @project.id, :jenkins_job_name => job_name, :output => output) : se.update_column(:output, output) @client.job.delete("#{job_name}") elsif @current_build_status == "success" - if qa.blank? + if quality_an.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 => "#{user_name}:#{rep_id}") else diff --git a/app/views/quality_analysis/error_list.html.erb b/app/views/quality_analysis/error_list.html.erb index 217990b13..84f2777fa 100644 --- a/app/views/quality_analysis/error_list.html.erb +++ b/app/views/quality_analysis/error_list.html.erb @@ -6,6 +6,6 @@ <% if @build_console_result == false %> 分析超时 <% else %> - <%= h @error_list.to_json %> + <%= h @error_list.output %> <% end %>
\ No newline at end of file