diff --git a/Gemfile b/Gemfile index 03dbb5785..a94f1b33b 100644 --- a/Gemfile +++ b/Gemfile @@ -51,7 +51,7 @@ gem 'elasticsearch-rails' ### profile -gem 'oneapm_rpm' +# gem 'oneapm_rpm' group :development do gem 'grape-swagger' diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index cb86f1165..d8c174bc3 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -300,15 +300,13 @@ class ProjectsController < ApplicationController return end - logger.debug "111111111"*100 - # over @author = params[:user_id].blank? ? nil : User.active.find(params[:user_id]) @page = params[:page] ? params[:page].to_i + 1 : 0 # 根据私密性,取出符合条件的所有数据 if User.current.member_of?(@project) || User.current.admin? case params[:type] when nil - @events_pages = ForgeActivity.where("project_id = ? and forge_act_type in ('Issue', 'Message','News', 'Project', 'Attachment')", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) + @events_pages = ForgeActivity.where("project_id = ? and forge_act_type in ('Issue', 'Message','News', 'Project', 'Attachment','Commit')", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) when 'issue' @events_pages = ForgeActivity.where("project_id = ? and forge_act_type = 'Issue'", @project).includes(:forge_act).order("updated_at desc").limit(10).offset(@page * 10) when 'news' @@ -321,8 +319,8 @@ class ProjectsController < ApplicationController else @events_pages = ForgeActivity.includes(:project).where("forge_activities.project_id = ? and projects.is_public = ? and forge_act_type != ? ",@project,1, "Document").order("created_at desc").page(params['page'|| 1]).per(10); end - logger.debug "2"*100 - # g = Gitlab.client + + # 版本库统计图 unless @project.gpid.nil? || @project.project_score.changeset_num == 0 # rep_statics_commit = @project.rep_statics.order("commits_num desc") rep_statics_commit = RepStatics.find_by_sql("SELECT * FROM `rep_statics` where project_id = #{@project.id} order by commits_num desc limit 10") @@ -335,20 +333,14 @@ class ProjectsController < ApplicationController @a_commits_del = rep_statics_code.map {|s| s.del.to_i } @a_commits_changeset = rep_statics_code.map {|s| s.changeset.to_i } g = Gitlab.client - logger.debug "3"*100 begin gid = @project.gpid - logger.debug "31"*100 - g_branch = g.project(gid) - logger.debug "4"*100 - g_branch = g_branch.default_branch.to_s - logger.debug "5"*100 + g_project = g.project(gid) + g_branch = g_project.default_branch.to_s rescue =>e - logger.error("get gitlab project failed: " + e) + logger.error("get default branch failed: " + e) end - logger.debug "6"*100 @rev = g_branch.nil? ? "master" : g_branch - logger.debug "7"*100 end # 根据对应的请求,返回对应的数据 respond_to do |format| diff --git a/app/controllers/quality_analysis_controller.rb b/app/controllers/quality_analysis_controller.rb index 6b50c1788..80c5a2468 100644 --- a/app/controllers/quality_analysis_controller.rb +++ b/app/controllers/quality_analysis_controller.rb @@ -165,7 +165,7 @@ class QualityAnalysisController < ApplicationController logger.info("result: delete job ###################==>#{d_job}") qa.delete respond_to do |format| - format.html{redirect_to project_quality_analysis_path(:project_id => @project.id)} + format.html{redirect_to :controller => 'repositories', :action => 'show', :id => @project, :repository_id => gitlab_repository(@project).identifier} end rescue Exception => e puts e @@ -199,9 +199,6 @@ class QualityAnalysisController < ApplicationController # update成功则返回 ‘200’ jenkins_job = @client.job.update("#{job_name}", @doc.to_xml) - 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数据 if jenkins_job == '200' logger.info("quality_ananlysis will be updated: ==> #{jenkins_job}") diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 6b41e39fc..65b4a15d6 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -48,7 +48,7 @@ class RepositoriesController < ApplicationController include RepositoriesHelper helper :project_score #@root_path = RepositoriesHelper::ROOT_PATH - require 'net/ssh' + # require 'net/ssh' rescue_from Redmine::Scm::Adapters::CommandFailed, :with => :show_error_command_failed def new diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 603084cd1..8a871e2ea 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -38,6 +38,17 @@ module ApplicationHelper # super # end + # 通过系统外部邮箱查找用户,如果用户不存在则用邮箱替换 + def get_user_by_mail mail + user = User.find_by_mail(mail) + user.nil? ? User.find(2) : user + end + + # 获取用户单位 + # 优先获取高校信息,如果改信息不存在则获取occupation + def get_occupation_from_user user + School.where("id=?",user.user_extensions.school_id).first.try(:name).nil? ? user.user_extensions.occupation : School.where("id=?",user.user_extensions.school_id).first.try(:name) + end def update_visiti_count container container.update_column(:visits, container.visits + 1) diff --git a/app/models/changeset.rb b/app/models/changeset.rb index f72342257..149f4b163 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -22,9 +22,7 @@ class Changeset < ActiveRecord::Base #after_save :be_user_score # user_score has_many :filechanges, :class_name => 'Change', :dependent => :delete_all - # fq - has_many :acts, :class_name => 'Activity', :as => :act, :dependent => :destroy - # end + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy #Added by nie has_one :project_status, :dependent => :destroy @@ -59,7 +57,6 @@ class Changeset < ActiveRecord::Base validates :committed_on, presence: true validates :commit_date, presence: true validates :scmid, uniqueness: {scope: :repository_id, allow_nil: true} - scope :visible, lambda {|*args| includes(:repository => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_changesets, *args)) } @@ -68,7 +65,7 @@ class Changeset < ActiveRecord::Base # after_update :be_user_score after_destroy :down_user_score # before_create :before_create_cs - after_create :act_as_forge_activity + # after_create :act_as_forge_activity def revision=(r) @@ -118,7 +115,7 @@ class Changeset < ActiveRecord::Base # 项目中提交动态,类型Changeset # type:0 为老版本即Trsutie数据 1为gitlab中获取的动态 def act_as_forge_activity - self.acts << ForgeActivity.new(:user_id => self.user_id, :project_id => self.project_id, :type => true) + self.forge_acts << ForgeActivity.new(:user_id => self.user_id, :project_id => self.project_id, :type => true) end def scan_for_issues diff --git a/app/models/commit.rb b/app/models/commit.rb new file mode 100644 index 000000000..a6633206a --- /dev/null +++ b/app/models/commit.rb @@ -0,0 +1,13 @@ +class Commit < ActiveRecord::Base + attr_accessible :comments, :committed_on, :committer, :project_id, :repository_id, :version + validates :repository_id, presence: true + validates :version, presence: true, uniqueness: {scope: :repository_id} + validates :committed_on, presence: true + has_many :forge_acts, :class_name => 'ForgeActivity',:as =>:forge_act ,:dependent => :destroy + after_create :act_as_forge_activity + + # 项目中提交动态 + def act_as_forge_activity + self.forge_acts << ForgeActivity.new(:user_id => 2, :project_id => self.project_id) + end +end diff --git a/app/models/forge_activity.rb b/app/models/forge_activity.rb index 47ebf4d0f..5fe953f66 100644 --- a/app/models/forge_activity.rb +++ b/app/models/forge_activity.rb @@ -25,8 +25,11 @@ class ForgeActivity < ActiveRecord::Base # 在个人动态里面增加当前动态 # 版本库提交动态不显示在用户动态中 + # Commit为版本库提交类型,因为是从gitlab获取,不能实时更新 def add_user_activity - if self.forge_act_type != "Changeset" + if self.forge_act_type == "Commit" + self.update_column(:updated_at, self.forge_act.committed_on) + else user_activity = UserActivity.where("act_type = '#{self.forge_act_type.to_s}' and act_id = '#{self.forge_act_id}'").first if user_activity user_activity.save @@ -50,7 +53,9 @@ class ForgeActivity < ActiveRecord::Base # 项目提交动态不显示在组织动态中 def add_org_activity - if self.forge_act_type != "Changeset" + if self.forge_act_type == "Commit" + self.update_column(:updated_at, self.forge_act.committed_on) + else org_activity = OrgActivity.where("org_act_type = '#{self.forge_act_type.to_s}' and org_act_id = #{self.forge_act_id}").first if org_activity org_activity.updated_at = self.updated_at diff --git a/app/views/admin/users.html.erb b/app/views/admin/users.html.erb index 2a757fdee..9215782bb 100644 --- a/app/views/admin/users.html.erb +++ b/app/views/admin/users.html.erb @@ -56,8 +56,7 @@ <%= checked_image user.admin? %> <%= format_time(user.created_on) %> <%= format_time(user.last_login_on) unless user.last_login_on.nil? %> - <% occupation = user.user_extensions.identity == 0 ? School.where("id=?",user.user_extensions.school_id).first.try(:name) : user.user_extensions.occupation %> - <%= truncate( occupation, :length => 12 ) %> + <%= truncate(get_occupation_from_user(user), :length => 12 ) %> <%= change_status_link(user) %> <%= delete_link user_path(user, :back_url => admin_users_path(params)) unless User.current == user %> diff --git a/app/views/projects/_project_activities.html.erb b/app/views/projects/_project_activities.html.erb index 6322a532a..658f9f4f0 100644 --- a/app/views/projects/_project_activities.html.erb +++ b/app/views/projects/_project_activities.html.erb @@ -72,23 +72,8 @@ <% when "Attachment" %> <%= render :partial => 'users/project_attachment', :locals => {:activity => activity.forge_act, :user_activity_id => activity.id } %> - - - - - - - - - <%#= link_to format_activity_title("#{l(:label_attachment)}: #{act.filename}"), {:controller => 'attachments', :action => 'show', :id => act.id}, :class => "problem_tit fl fb" %> - - - - - - - - + <% when "Commit" %> + <%= render :partial => 'projects/project_commit', :locals => {:activity => activity.forge_act, :user_activity_id => activity.id } %> <% end %> <% end %> <% end %> diff --git a/app/views/projects/_project_commit.html.erb b/app/views/projects/_project_commit.html.erb new file mode 100644 index 000000000..99998e6e1 --- /dev/null +++ b/app/views/projects/_project_commit.html.erb @@ -0,0 +1,28 @@ +<% project = Project.find(activity.project_id) %> +<% user = get_user_by_mail(activity.committer) %> +
+
+
+ <%= link_to image_tag(url_to_avatar(user), :width => "50", :height => "50"), user_path(user), :alt => "用户头像" %> + <%= render :partial => 'users/show_detail_info', :locals => {:user => user} %> +
+
+
+ <% if user.try(:realname) == ' ' %> + <%= link_to user, user_path(user), :class => "newsBlue mr15" %> + <% else %> + <%= link_to user.try(:realname), user_path(user), :class => "newsBlue mr15" %> + <% end %> + TO + <%= link_to project.to_s+" | 项目代码提交", project_path(project.id,:host=>Setting.host_course), :class => "newsBlue ml15" %> +
+
+ <%= link_to activity.comments, {:controller => 'repositories', :action => 'commit_diff', :id => project.id, :changeset => activity.version}, :class => "postGrey" %> +
+
+ 提交时间:<%= format_time(activity.committed_on) %> +
+
+
+
+
diff --git a/app/views/quality_analysis/_show.html.erb b/app/views/quality_analysis/_show.html.erb index b364f5f89..7901c6be0 100644 --- a/app/views/quality_analysis/_show.html.erb +++ b/app/views/quality_analysis/_show.html.erb @@ -34,7 +34,7 @@

代码重复度

<%= @ha["duplicated_lines_density"].to_i == 0 ? 0 : @ha["duplicated_lines_density"] %> borderRadius"> - <%= @ha["duplicated_lines_density"].nil? ? 0 : duplicated_lines_density_status(@ha["duplicated_lines_density"].to_i)[0] %> + <%= @ha["duplicated_lines_density"].nil? ? "良好" : duplicated_lines_density_status(@ha["duplicated_lines_density"].to_i)[0] %>

@@ -43,7 +43,7 @@

注释率

<%= @ha["comment_lines_density"].to_i == 0 ? 0 : @ha["comment_lines_density"] %> borderRadius"> - <%= @ha["comment_lines_density"].nil? ? 0 : comment_lines_density_status(@ha["comment_lines_density"].to_i)[0] %> + <%= @ha["comment_lines_density"].nil? ? "较低" : comment_lines_density_status(@ha["comment_lines_density"].to_i)[0] %>

diff --git a/db/migrate/20160718064146_add_type_to_changeset.rb b/db/migrate/20160718064146_add_type_to_changeset.rb deleted file mode 100644 index 4d51883a1..000000000 --- a/db/migrate/20160718064146_add_type_to_changeset.rb +++ /dev/null @@ -1,6 +0,0 @@ -class AddTypeToChangeset < ActiveRecord::Migration - def change - add_column :changesets, :project_id, :integer - add_column :changesets, :type, :integer, :default => false - end -end diff --git a/db/migrate/20160721075236_create_commits.rb b/db/migrate/20160721075236_create_commits.rb new file mode 100644 index 000000000..555bf72f1 --- /dev/null +++ b/db/migrate/20160721075236_create_commits.rb @@ -0,0 +1,14 @@ +class CreateCommits < ActiveRecord::Migration + def change + create_table :commits do |t| + t.integer :repository_id + t.string :version + t.string :committer + t.text :comments + t.datetime :committed_on + t.integer :project_id + + t.timestamps + end + end +end diff --git a/lib/tasks/gitlab_act_project.rake b/lib/tasks/gitlab_act_project.rake index ee9ca1a9a..c4854db58 100644 --- a/lib/tasks/gitlab_act_project.rake +++ b/lib/tasks/gitlab_act_project.rake @@ -1,25 +1,32 @@ namespace :gitlab do desc "sync gitlab's commit acts to trustie" task :forge_acts => :environment do - begin - g = Gitlab.client - projects = Project.find_by_sql("select * from projects where gpid is not null") - projects.each do |project| + g = Gitlab.client + projects = Project.find_by_sql("select * from projects where gpid is not null and id not in (2,847,931,942)") + projects.each do |project| + begin g_project = g.project(project.gpid) # 获取默认分支 g_default_branch = g_project.default_branch.nil? ? "master" : g_project.default_branch # 总的提交次数 commit_count = g.user_static(project.gpid, :rev => g_default_branch).count pages = commit_count / 20 + 1 - pages.each do |page| - commits = g.commits(g_project.gpid, :ref_name => g_default_branch, :page => page) + puts "#{pages}" + puts "project id is #{project.id}" + # api获取每次只能获取20次提交,所以需要通过取得page值来获取每页的提交动态 + (0..pages).each do |page| + commits = g.commits(project.gpid, :ref_name => g_default_branch, :page => page) commits.each do |commit| - Changeset.create(:project_id => project.id, :repository_id => project.gpid, :revision => commit.id, :committer => commit.author_email, :comments => Redmine::CodesetUtil.to_utf8(commit.title, 'UTF-8'), :committed_on => commit.created_at) + Commit.create(:project_id => project.id, :repository_id => project.gpid, :version => commit.id, :committer => commit.author_email, :comments => Redmine::CodesetUtil.to_utf8(commit.title, 'UTF-8'), :committed_on => commit.created_at) end end + rescue Exception => e + # puts "Some wrong with project #{project.id}" + # Project.where(:id => project.id).first.update_column(:gpid, nil) + # Repository.where(:project_id => project.id).first.destroy + # try + puts e end - rescue Exception => e - puts e end end end diff --git a/lib/tasks/gitlab_forge_acts_update.rake b/lib/tasks/gitlab_forge_acts_update.rake new file mode 100644 index 000000000..4d8e9b2fb --- /dev/null +++ b/lib/tasks/gitlab_forge_acts_update.rake @@ -0,0 +1,32 @@ +namespace :gitlab do + desc "sync gitlab's commit acts to trustie" + task :forge_acts_update => :environment do + g = Gitlab.client + ids = [2,847,931,942] + projects = Project.find(ids) + projects.each do |project| + # c = Commit.where(:project_id => project.id) + # if c.blank? + begin + g_project = g.project(project.gpid) + # 获取默认分支 + g_default_branch = g_project.default_branch.nil? ? "master" : g_project.default_branch + # 总的提交次数 + commit_count = g.user_static(project.gpid, :rev => g_default_branch).count + pages = commit_count / 20 + 1 + puts "#{pages}" + puts "project id is #{project.id}" + # api获取每次只能获取20次提交,所以需要通过取得page值来获取每页的提交动态 + (0..pages).each do |page| + commits = g.commits(project.gpid, :ref_name => g_default_branch, :page => page) + commits.each do |commit| + Commit.create(:project_id => project.id, :repository_id => project.gpid, :version => commit.id, :committer => commit.author_email, :comments => Redmine::CodesetUtil.to_utf8(commit.title, 'UTF-8'), :committed_on => commit.created_at) + end + end + rescue Exception => e + puts e + end + # end + end + end +end diff --git a/lib/tasks/gitlab_trustie_acts.rake b/lib/tasks/gitlab_trustie_acts.rake new file mode 100644 index 000000000..8c3568397 --- /dev/null +++ b/lib/tasks/gitlab_trustie_acts.rake @@ -0,0 +1,41 @@ +namespace :gitlab do + desc "sync gitlab's commit acts to trustie" + task :acts_to_trustie => :environment do + begin + projects = Project.where(:status => 1) + projects.each do |project| + c = Commit.find_by_sql("SELECT * FROM `commits` where project_id = #{project.id} order by committed_on limit 1;") + g_project = g.project(project.gpid) + end + rescue Exception => e + puts e + end + g = Gitlab.client + ids = [2,847,931,942] + projects = Project.find(ids) + projects.each do |project| + c = Commit.where(:project_id => project.id) + if c.blank? + begin + g_project = g.project(project.gpid) + # 获取默认分支 + g_default_branch = g_project.default_branch.nil? ? "master" : g_project.default_branch + # 总的提交次数 + commit_count = g.user_static(project.gpid, :rev => g_default_branch).count + pages = commit_count / 20 + 1 + puts "#{pages}" + puts "project id is #{project.id}" + # api获取每次只能获取20次提交,所以需要通过取得page值来获取每页的提交动态 + (0..pages).each do |page| + commits = g.commits(project.gpid, :ref_name => g_default_branch, :page => page) + commits.each do |commit| + Commit.create(:project_id => project.id, :repository_id => project.gpid, :version => commit.id, :committer => commit.author_email, :comments => Redmine::CodesetUtil.to_utf8(commit.title, 'UTF-8'), :committed_on => commit.created_at) + end + end + rescue Exception => e + puts e + end + end + end + end +end diff --git a/lib/tasks/gitlab_unused.rake b/lib/tasks/gitlab_unused.rake new file mode 100644 index 000000000..4b81acbff --- /dev/null +++ b/lib/tasks/gitlab_unused.rake @@ -0,0 +1,18 @@ +namespace :gitlab do + desc "sync gitlab's commit acts to trustie" + task :unused => :environment do + begin + Project.where("id in (161,236,266)").update_all(:gpid => nil) + repositories = Repository.find_by_sql("select * from repositories where project_id in (select project_id from repositories group by project_id having count(project_id) > 1);") + repositories.each do |rep| + puts "#{rep.id}" + if rep.type == "Repository::Git" + rep.destroy + end + end + # Repository.where("project_id in (161,236,266)").destroy_all + rescue Exception => e + puts e + end + end +end diff --git a/spec/factories/commits.rb b/spec/factories/commits.rb new file mode 100644 index 000000000..c520e8b64 --- /dev/null +++ b/spec/factories/commits.rb @@ -0,0 +1,11 @@ +FactoryGirl.define do + factory :commit do + repository_id 1 +version "MyString" +committer "MyString" +comments "MyText" +committed_on "2016-07-21" +project_id 1 + end + +end diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb new file mode 100644 index 000000000..546996270 --- /dev/null +++ b/spec/models/commit_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Commit, :type => :model do + pending "add some examples to (or delete) #{__FILE__}" +end