Commit f5908cef19a3cd2c44be02b453fecb568b2c6c8e
1 parent
c3857842
Exists in
master
and in
4 other branches
System Hook: implemented
Showing
9 changed files
with
100 additions
and
10 deletions
Show diff stats
app/controllers/admin/hooks_controller.rb
| ... | ... | @@ -11,12 +11,11 @@ class Admin::HooksController < ApplicationController |
| 11 | 11 | def create |
| 12 | 12 | @hook = SystemHook.new(params[:hook]) |
| 13 | 13 | |
| 14 | - respond_to do |format| | |
| 15 | - if @hook.save | |
| 16 | - format.html { redirect_to admin_hooks_path, notice: 'Hook was successfully created.' } | |
| 17 | - else | |
| 18 | - format.html { render :index } | |
| 19 | - end | |
| 14 | + if @hook.save | |
| 15 | + redirect_to admin_hooks_path, notice: 'Hook was successfully created.' | |
| 16 | + else | |
| 17 | + @hooks = SystemHook.all | |
| 18 | + render :index | |
| 20 | 19 | end |
| 21 | 20 | end |
| 22 | 21 | ... | ... |
app/models/project.rb
| ... | ... | @@ -107,6 +107,32 @@ class Project < ActiveRecord::Base |
| 107 | 107 | validate :check_limit |
| 108 | 108 | validate :repo_name |
| 109 | 109 | |
| 110 | + after_create :create_hooks | |
| 111 | + after_destroy :destroy_hooks | |
| 112 | + | |
| 113 | + def create_hooks | |
| 114 | + SystemHook.all_hooks_fire({ | |
| 115 | + event_name: "project_create", | |
| 116 | + name: self.name, | |
| 117 | + path: self.path, | |
| 118 | + project_id: self.id, | |
| 119 | + owner_name: self.owner.name, | |
| 120 | + owner_email: self.owner.email, | |
| 121 | + created_at: self.created_at | |
| 122 | + }) | |
| 123 | + end | |
| 124 | + | |
| 125 | + def destroy_hooks | |
| 126 | + SystemHook.all_hooks_fire({ | |
| 127 | + event_name: "project_destroy", | |
| 128 | + name: self.name, | |
| 129 | + path: self.path, | |
| 130 | + project_id: self.id, | |
| 131 | + owner_name: self.owner.name, | |
| 132 | + owner_email: self.owner.email, | |
| 133 | + }) | |
| 134 | + end | |
| 135 | + | |
| 110 | 136 | def check_limit |
| 111 | 137 | unless owner.can_create_project? |
| 112 | 138 | errors[:base] << ("Your own projects limit is #{owner.projects_limit}! Please contact administrator to increase it") |
| ... | ... | @@ -120,7 +146,7 @@ class Project < ActiveRecord::Base |
| 120 | 146 | errors.add(:path, " like 'gitolite-admin' is not allowed") |
| 121 | 147 | end |
| 122 | 148 | end |
| 123 | - | |
| 149 | + | |
| 124 | 150 | def self.access_options |
| 125 | 151 | UsersProject.access_roles |
| 126 | 152 | end | ... | ... |
app/models/system_hook.rb
app/models/user.rb
| ... | ... | @@ -57,6 +57,25 @@ class User < ActiveRecord::Base |
| 57 | 57 | scope :active, where(:blocked => false) |
| 58 | 58 | |
| 59 | 59 | before_validation :generate_password, :on => :create |
| 60 | + after_create :create_hooks | |
| 61 | + after_destroy :destroy_hooks | |
| 62 | + | |
| 63 | + def create_hooks | |
| 64 | + SystemHook.all_hooks_fire({ | |
| 65 | + event_name: "user_create", | |
| 66 | + name: self.name, | |
| 67 | + email: self.email, | |
| 68 | + created_at: self.created_at | |
| 69 | + }) | |
| 70 | + end | |
| 71 | + | |
| 72 | + def destroy_hooks | |
| 73 | + SystemHook.all_hooks_fire({ | |
| 74 | + event_name: "user_destroy", | |
| 75 | + name: self.name, | |
| 76 | + email: self.email | |
| 77 | + }) | |
| 78 | + end | |
| 60 | 79 | |
| 61 | 80 | def generate_password |
| 62 | 81 | if self.force_random_password | ... | ... |
app/models/users_project.rb
| ... | ... | @@ -11,6 +11,9 @@ class UsersProject < ActiveRecord::Base |
| 11 | 11 | |
| 12 | 12 | after_save :update_repository |
| 13 | 13 | after_destroy :update_repository |
| 14 | + after_create :add_to_team_hooks | |
| 15 | + after_destroy :remove_from_team_hooks | |
| 16 | + | |
| 14 | 17 | |
| 15 | 18 | validates_uniqueness_of :user_id, :scope => [:project_id] |
| 16 | 19 | validates_presence_of :user_id |
| ... | ... | @@ -18,6 +21,31 @@ class UsersProject < ActiveRecord::Base |
| 18 | 21 | |
| 19 | 22 | delegate :name, :email, :to => :user, :prefix => true |
| 20 | 23 | |
| 24 | + def add_to_team_hooks | |
| 25 | + SystemHook.all_hooks_fire({ | |
| 26 | + event_name: "user_add_to_team", | |
| 27 | + project_name: self.project.name, | |
| 28 | + project_path: self.project.path, | |
| 29 | + project_id: self.project_id, | |
| 30 | + user_name: self.user.name, | |
| 31 | + user_email: self.user.email, | |
| 32 | + project_access: self.repo_access_human, | |
| 33 | + created_at: self.created_at | |
| 34 | + }) | |
| 35 | + end | |
| 36 | + | |
| 37 | + def remove_from_team_hooks | |
| 38 | + SystemHook.all_hooks_fire({ | |
| 39 | + event_name: "user_remove_from_team", | |
| 40 | + project_name: self.project.name, | |
| 41 | + project_path: self.project.path, | |
| 42 | + project_id: self.project_id, | |
| 43 | + user_name: self.user.name, | |
| 44 | + user_email: self.user.email, | |
| 45 | + project_access: self.repo_access_human | |
| 46 | + }) | |
| 47 | + end | |
| 48 | + | |
| 21 | 49 | def self.bulk_import(project, user_ids, project_access) |
| 22 | 50 | UsersProject.transaction do |
| 23 | 51 | user_ids.each do |user_id| |
| ... | ... | @@ -68,7 +96,7 @@ class UsersProject < ActiveRecord::Base |
| 68 | 96 | end |
| 69 | 97 | |
| 70 | 98 | def repo_access_human |
| 71 | - "" | |
| 99 | + self.class.access_roles.invert[self.project_access] | |
| 72 | 100 | end |
| 73 | 101 | end |
| 74 | 102 | # == Schema Information | ... | ... |
app/models/web_hook.rb
resque.sh
| 1 | 1 | mkdir -p tmp/pids |
| 2 | -bundle exec rake environment resque:work QUEUE=post_receive,mailer RAILS_ENV=production PIDFILE=tmp/pids/resque_worker.pid BACKGROUND=yes | |
| 2 | +bundle exec rake environment resque:work QUEUE=post_receive,mailer,system_hook RAILS_ENV=production PIDFILE=tmp/pids/resque_worker.pid BACKGROUND=yes | ... | ... |
resque_dev.sh