Event entity created

Observe issue, merge request, note creation - create event
register push event
This commit is contained in:
Dmitriy Zaporozhets 2012-02-28 20:09:23 +02:00
parent 85954621a3
commit 0a0710a548
16 changed files with 310 additions and 104 deletions

View file

@ -0,0 +1,12 @@
class ActivityObserver < ActiveRecord::Observer
observe :issue, :merge_request, :note
def after_create(record)
Event.create(
:project => record.project,
:target_id => record.id,
:target_type => record.class.name,
:action => Event.determine_action(record)
)
end
end

36
app/models/event.rb Normal file
View file

@ -0,0 +1,36 @@
class Event < ActiveRecord::Base
Created = 1
Updated = 2
Closed = 3
Reopened = 4
Pushed = 5
Commented = 6
belongs_to :project
belongs_to :target, :polymorphic => true
serialize :data
def self.determine_action(record)
if [Issue, MergeRequest].include? record.class
Event::Created
elsif record.kind_of? Note
Event::Commented
end
end
end
# == Schema Information
#
# Table name: events
#
# id :integer not null, primary key
# target_type :string(255)
# target_id :integer
# title :string(255)
# data :text
# project_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# action :integer
#

View file

@ -3,6 +3,7 @@ require "grit"
class Project < ActiveRecord::Base
belongs_to :owner, :class_name => "User"
has_many :events, :dependent => :destroy
has_many :merge_requests, :dependent => :destroy
has_many :issues, :dependent => :destroy, :order => "position"
has_many :users_projects, :dependent => :destroy
@ -89,6 +90,16 @@ class Project < ActiveRecord::Base
[GIT_HOST['host'], code].join("/")
end
def observe_push(oldrev, newrev, ref)
data = web_hook_data(oldrev, newrev, ref)
Event.create(
:project => self,
:action => Event::Pushed,
:data => data
)
end
def execute_web_hooks(oldrev, newrev, ref)
ref_parts = ref.split('/')
@ -96,6 +107,7 @@ class Project < ActiveRecord::Base
return if ref_parts[1] !~ /heads/ || oldrev == "00000000000000000000000000000000"
data = web_hook_data(oldrev, newrev, ref)
web_hooks.each { |web_hook| web_hook.execute(data) }
end
@ -364,5 +376,6 @@ end
# issues_enabled :boolean default(TRUE), not null
# wall_enabled :boolean default(TRUE), not null
# merge_requests_enabled :boolean default(TRUE), not null
# wiki_enabled :boolean default(TRUE), not null
#

View file

@ -80,7 +80,6 @@ end
# project_id :integer not null
# created_at :datetime
# updated_at :datetime
# repo_access :integer default(0), not null
# project_access :integer default(0), not null
#

View file

@ -31,3 +31,17 @@ class Wiki < ActiveRecord::Base
end
end
# == Schema Information
#
# Table name: wikis
#
# id :integer not null, primary key
# title :string(255)
# content :text
# project_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# slug :string(255)
# user_id :integer
#

View file

@ -5,6 +5,7 @@ class PostReceive
project = Project.find_by_path(reponame)
return false if project.nil?
project.observe_push(oldrev, newrev, ref)
project.execute_web_hooks(oldrev, newrev, ref)
end
end