From bb164ebf1bd672527a76a6699427cbec728d638b Mon Sep 17 00:00:00 2001 From: Dmitriy Zaporozhets Date: Wed, 29 Feb 2012 22:38:24 +0200 Subject: [PATCH] Send author to post hook. Display push activity to dashboard --- Gemfile | 1 + Gemfile.lock | 2 ++ app/assets/stylesheets/common.scss | 6 ++++ app/controllers/dashboard_controller.rb | 2 ++ app/controllers/issues_controller.rb | 5 +++- app/models/event.rb | 34 ++++++++++++++++++++++ app/models/key.rb | 1 + app/models/project.rb | 13 +++++---- app/models/users_project.rb | 2 +- app/views/dashboard/_events_feed.html.haml | 19 ++++++++++++ app/views/dashboard/index.html.haml | 9 ++++++ app/workers/post_receive.rb | 6 ++-- lib/post-receive-hook | 2 +- lib/tasks/update_hooks.rake | 15 ++++++++++ resque_dev.sh | 1 + 15 files changed, 107 insertions(+), 11 deletions(-) create mode 100644 app/views/dashboard/_events_feed.html.haml create mode 100644 lib/tasks/update_hooks.rake create mode 100755 resque_dev.sh diff --git a/Gemfile b/Gemfile index 60bc6346..5c0b660d 100644 --- a/Gemfile +++ b/Gemfile @@ -30,6 +30,7 @@ gem "charlock_holmes" gem "foreman" gem "omniauth-ldap" gem 'bootstrap-sass', "1.4.4" +gem "colored" group :assets do gem "sass-rails", "3.2.3" diff --git a/Gemfile.lock b/Gemfile.lock index 251e8011..09457d5b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -88,6 +88,7 @@ GEM coffee-script-source execjs coffee-script-source (1.2.0) + colored (1.2) crack (0.3.1) daemons (1.1.8) database_cleaner (0.7.1) @@ -296,6 +297,7 @@ DEPENDENCIES carrierwave charlock_holmes coffee-rails (= 3.2.1) + colored database_cleaner devise drapper diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss index e1acae42..5670f1e0 100644 --- a/app/assets/stylesheets/common.scss +++ b/app/assets/stylesheets/common.scss @@ -626,3 +626,9 @@ p.time { } } } + +.event_feed { + ul { + margin-left:50px; + } +} diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index ade8beea..fe9604ec 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -11,6 +11,8 @@ class DashboardController < ApplicationController @user = current_user @issues = current_user.assigned_issues.opened.order("created_at DESC").limit(10) @issues = @issues.includes(:author, :project) + + @events = Event.where(:project_id => @projects.map(&:id)).recent.limit(20) end # Get authored or assigned open merge requests diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 02e738d5..4e3be259 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -69,7 +69,10 @@ class IssuesController < ApplicationController @issue.author = current_user @issue.save - respond_with(@issue) + respond_to do |format| + format.html { redirect_to project_issue_path(@project, @issue) } + format.js + end end def update diff --git a/app/models/event.rb b/app/models/event.rb index 5cb75247..e1732f00 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -11,6 +11,8 @@ class Event < ActiveRecord::Base serialize :data + scope :recent, order("created_at DESC") + def self.determine_action(record) if [Issue, MergeRequest].include? record.class Event::Created @@ -18,6 +20,38 @@ class Event < ActiveRecord::Base Event::Commented end end + + def push? + action == self.class::Pushed + end + + def new_branch? + data[:before] =~ /^00000/ + end + + def commit_from + data[:before] + end + + def commit_to + data[:after] + end + + def branch_name + @branch_name ||= data[:ref].gsub("refs/heads/", "") + end + + def pusher + User.find_by_id(data[:user_id]) + end + + def commits + @commits ||= data[:commits].map do |commit| + project.commit(commit[:id]) + end + end + + delegate :id, :name, :email, :to => :pusher, :prefix => true, :allow_nil => true end # == Schema Information # diff --git a/app/models/key.rb b/app/models/key.rb index fb59d759..6597d7e5 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -14,6 +14,7 @@ class Key < ActiveRecord::Base before_save :set_identifier after_save :update_repository after_destroy :repository_delete_key + delegate :id, :name, :email, :to => :user, :prefix => true def set_identifier if is_deploy_key diff --git a/app/models/project.rb b/app/models/project.rb index 4d1d4e79..c1662918 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -90,8 +90,8 @@ class Project < ActiveRecord::Base [GIT_HOST['host'], code].join("/") end - def observe_push(oldrev, newrev, ref) - data = web_hook_data(oldrev, newrev, ref) + def observe_push(oldrev, newrev, ref, author_key_id) + data = web_hook_data(oldrev, newrev, ref, author_key_id) Event.create( :project => self, @@ -100,22 +100,25 @@ class Project < ActiveRecord::Base ) end - def execute_web_hooks(oldrev, newrev, ref) + def execute_web_hooks(oldrev, newrev, ref, author_key_id) ref_parts = ref.split('/') # Return if this is not a push to a branch (e.g. new commits) return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000" - data = web_hook_data(oldrev, newrev, ref) + data = web_hook_data(oldrev, newrev, ref, author_key_id) web_hooks.each { |web_hook| web_hook.execute(data) } end - def web_hook_data(oldrev, newrev, ref) + def web_hook_data(oldrev, newrev, ref, author_key_id) + key = Key.find_by_identifier(author_key_id) data = { before: oldrev, after: newrev, ref: ref, + user_id: key.user_id, + user_name: key.user_name, repository: { name: name, url: web_url, diff --git a/app/models/users_project.rb b/app/models/users_project.rb index 726a85ae..ebbbf866 100644 --- a/app/models/users_project.rb +++ b/app/models/users_project.rb @@ -16,7 +16,7 @@ class UsersProject < ActiveRecord::Base validates_presence_of :user_id validates_presence_of :project_id - delegate :name, :email, :to => :user, :prefix => true + delegate :id, :name, :email, :to => :user, :prefix => true def self.bulk_import(project, user_ids, project_access, repo_access) UsersProject.transaction do diff --git a/app/views/dashboard/_events_feed.html.haml b/app/views/dashboard/_events_feed.html.haml new file mode 100644 index 00000000..ae52fb80 --- /dev/null +++ b/app/views/dashboard/_events_feed.html.haml @@ -0,0 +1,19 @@ +- @events.each do |event| + .wll.event_feed + - if event.push? + - if event.new_branch? + User pushed new branch + - else + = image_tag gravatar_icon(event.pusher_email), :class => "avatar" + #{event.pusher_name} pushed to + = link_to project_commits_path(event.project, :ref => event.branch_name) do + %strong= event.branch_name + %span.cgray + = time_ago_in_words(event.created_at) + ago. + - if event.commits.count > 1 + = link_to compare_project_commits_path(event.project, :from => event.commits.last, :to => event.commits.first) do + Compare #{event.commits.last.id[0..8]}...#{event.commits.first.id[0..8]} + - @project = event.project + %ul.unstyled + = render event.commits diff --git a/app/views/dashboard/index.html.haml b/app/views/dashboard/index.html.haml index 9d7cf6c9..27490e40 100644 --- a/app/views/dashboard/index.html.haml +++ b/app/views/dashboard/index.html.haml @@ -54,3 +54,12 @@ %hr .row .dashboard_block= render "dashboard/issues_feed" + +- unless @events.blank? + %div.dashboard_category + %h3 + Activities + + %hr + .row + .dashboard_block= render "dashboard/events_feed" diff --git a/app/workers/post_receive.rb b/app/workers/post_receive.rb index 81654dfa..9da1cde1 100644 --- a/app/workers/post_receive.rb +++ b/app/workers/post_receive.rb @@ -1,11 +1,11 @@ class PostReceive @queue = :post_receive - def self.perform(reponame, oldrev, newrev, ref) + def self.perform(reponame, oldrev, newrev, ref, author_key_id) project = Project.find_by_path(reponame) return false if project.nil? - project.observe_push(oldrev, newrev, ref) - project.execute_web_hooks(oldrev, newrev, ref) + project.observe_push(oldrev, newrev, ref, author_key_id) + project.execute_web_hooks(oldrev, newrev, ref, author_key_id) end end diff --git a/lib/post-receive-hook b/lib/post-receive-hook index d7354d65..93eb96c2 100755 --- a/lib/post-receive-hook +++ b/lib/post-receive-hook @@ -8,5 +8,5 @@ do # For every branch or tag that was pushed, create a Resque job in redis. pwd=`pwd` reponame=`basename "$pwd" | cut -d. -f1` - env -i redis-cli rpush "resque:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\"]}" > /dev/null 2>&1 + env -i redis-cli rpush "resque:queue:post_receive" "{\"class\":\"PostReceive\",\"args\":[\"$reponame\",\"$oldrev\",\"$newrev\",\"$ref\",\"$GL_USER\"]}" > /dev/null 2>&1 done diff --git a/lib/tasks/update_hooks.rake b/lib/tasks/update_hooks.rake new file mode 100644 index 00000000..472023c8 --- /dev/null +++ b/lib/tasks/update_hooks.rake @@ -0,0 +1,15 @@ +desc "Rewrite hooks for repos" +task :update_hooks => :environment do + puts "Starting Projects" + Project.find_each(:batch_size => 100) do |project| + begin + if project.commit + project.repository.write_hooks + print ".".green + end + rescue Exception => e + print e.message.red + end + end + puts "\nDone with projects" +end diff --git a/resque_dev.sh b/resque_dev.sh new file mode 100755 index 00000000..9df4dc1d --- /dev/null +++ b/resque_dev.sh @@ -0,0 +1 @@ +bundle exec rake environment resque:work QUEUE=* VVERBOSE=1