| |   |
| 134 | 134 | super({:procs => [info]}.merge(opts)) |
| 135 | 135 | end |
| 136 | 136 | |
| 137 | | def create_event(action_id, target, user, data = nil, body = nil) |
| 138 | | events.create(:action => action_id, :target => target, :user => user, :body => body, :data => data) |
| 137 | def create_event(action_id, target, user, data = nil, body = nil, date = Time.now.utc) |
| 138 | events.create(:action => action_id, :target => target, :user => user, |
| 139 | :body => body, :data => data, :created_at => date) |
| 139 | 140 | end |
| 140 | 141 | |
| 141 | 142 | protected |
| toggle raw diff |
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -134,8 +134,9 @@ class Project < ActiveRecord::Base
super({:procs => [info]}.merge(opts))
end
- def create_event(action_id, target, user, data = nil, body = nil)
- events.create(:action => action_id, :target => target, :user => user, :body => body, :data => data)
+ def create_event(action_id, target, user, data = nil, body = nil, date = Time.now.utc)
+ events.create(:action => action_id, :target => target, :user => user,
+ :body => body, :data => data, :created_at => date)
end
protected |
| |   |
| 7 | 7 | require 'date' |
| 8 | 8 | |
| 9 | 9 | gitdir = File.expand_path(File.join(incpath, "..")) |
| 10 | | repository_name = File.basename(gitdir) |
| 11 | | slug = "" |
| 10 | repository = Repository.find_by_path(gitdir) |
| 12 | 11 | |
| 13 | | if repository_name =~ /\.git$/ |
| 14 | | repository_name.sub!(/\.git$/, "") |
| 15 | | slug = File.basename(File.expand_path(File.join(gitdir, ".."))) |
| 12 | if repository.nil? |
| 13 | $stderr.puts "Unknown repository" |
| 14 | exit 0 |
| 16 | 15 | end |
| 17 | 16 | |
| 17 | project = repository.project |
| 18 | 18 | git = Grit::Git.new(gitdir) |
| 19 | 19 | |
| 20 | 20 | while line = gets |
| 21 | | #puts "@@> #{line.inspect}" |
| 21 | # puts "@@> #{line.inspect}" |
| 22 | 22 | # 3b480c5c30962c9f5b82a9c61a75992dc605de21 ee27ee3a022802c0569f9cdef6e9ad29aea096a8 refs/heads/master\n |
| 23 | 23 | oldrev, newrev, revname = line.split(" ") |
| 24 | 24 | current_rev = newrev |
| … | … | |
| 51 | 51 | # type => heads, tags, remotes |
| 52 | 52 | # name => branch name |
| 53 | 53 | path, type, name = revname.split("/") |
| 54 | revs = [ current_rev ] |
| 55 | emails = [git.show({:pretty => "format:%ce", :s => true}, current_rev)] |
| 54 | 56 | |
| 55 | | info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %cd%nmessage: %s", :s => true}, current_rev ) |
| 56 | | |
| 57 | | hash = {} |
| 58 | | info.each { |line| |
| 59 | | if line =~ /(\w+):\s(.*)$/ |
| 60 | | key = $1.to_sym |
| 61 | | value = $2 |
| 62 | | |
| 63 | | value = Date.parse(value) if key == :date |
| 64 | | |
| 65 | | hash[key] = value |
| 57 | if type == "heads" |
| 58 | if action == :update |
| 59 | revs = git.rev_list({}, "#{oldrev}..#{newrev}").split("\n") |
| 60 | emails = git.log({:pretty => "format:%ce"}, "#{oldrev}..#{newrev}").split("\n") |
| 61 | elsif action == :create && name == "master" |
| 62 | revs = git.rev_list({}, current_rev).split("\n") |
| 63 | emails = git.log({:pretty => "format:%ce"}, current_rev).split("\n") |
| 66 | 64 | end |
| 67 | | } |
| 68 | | |
| 69 | | # action => create, delete, update |
| 70 | | # rev_type => commit, tag |
| 71 | | |
| 72 | | user = User.find_by_email(hash[:email]) |
| 73 | | if user.nil? |
| 74 | | # TODO: no user should be ok, no need to skip |
| 75 | | # $stdout.puts "** The email '#{hash[:email]}' is not registered." |
| 76 | | next |
| 77 | 65 | end |
| 78 | 66 | |
| 79 | | project = Project.find_by_slug(slug) |
| 80 | | repository = nil |
| 67 | users = repository.committers.find(:all, :conditions => ["email in (?)", emails]) |
| 68 | user_map = users.inject({}) { |hash, user| hash[user.email] = user; hash } |
| 81 | 69 | |
| 82 | | project.repositories.each { |repo| |
| 83 | | if repo.name == repository_name |
| 84 | | repository = repo |
| 85 | | break |
| 70 | revs.each do |sha1| |
| 71 | info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %ct%nmessage: %s", :s => true}, sha1) |
| 72 | |
| 73 | hash = {} |
| 74 | info.each { |line| |
| 75 | if line =~ /(\w+):\s(.*)$/ |
| 76 | key = $1.to_sym |
| 77 | value = $2 |
| 78 | |
| 79 | value = Time.at(value.to_i).utc if key == :date |
| 80 | |
| 81 | hash[key] = value |
| 82 | end |
| 83 | } |
| 84 | |
| 85 | # action => create, delete, update |
| 86 | # rev_type => commit, tag |
| 87 | |
| 88 | user = user_map[hash[:email]] |
| 89 | if user.nil? |
| 90 | # TODO: no user should be ok, no need to skip |
| 91 | # $stdout.puts "** The email '#{hash[:email]}' is not registered." |
| 92 | next |
| 86 | 93 | end |
| 87 | | } |
| 88 | | |
| 89 | | action_id = nil |
| 90 | | ref = nil |
| 91 | | if current_rev_type == "commit" |
| 92 | | if type == "heads" |
| 93 | | case action |
| 94 | | when :create |
| 95 | | action_id = Action::CREATE_BRANCH |
| 94 | |
| 95 | action_id = nil |
| 96 | ref = nil |
| 97 | if current_rev_type == "commit" |
| 98 | if type == "heads" |
| 99 | case action |
| 100 | when :create |
| 101 | action_id = Action::CREATE_BRANCH |
| 102 | ref = name |
| 103 | when :update |
| 104 | action_id = Action::COMMIT |
| 105 | ref = sha1 |
| 106 | when :delete |
| 107 | action_id = Action::DELETE_BRANCH |
| 108 | ref = name |
| 109 | end |
| 110 | elsif type == "tags" |
| 111 | if action == :create |
| 112 | action_id = Action::CREATE_TAG |
| 96 | 113 | ref = name |
| 97 | | when :update |
| 98 | | action_id = Action::COMMIT |
| 99 | | ref = current_rev |
| 100 | | when :delete |
| 101 | | action_id = Action::DELETE_BRANCH |
| 114 | elsif action == :delete |
| 115 | action_id = Action::DELETE_TAG |
| 102 | 116 | ref = name |
| 117 | end |
| 103 | 118 | end |
| 104 | | elsif type == "tags" |
| 105 | | if action == :create |
| 106 | | action_id = Action::CREATE_TAG |
| 107 | | ref = name |
| 108 | | elsif action == :delete |
| 109 | | action_id = Action::DELETE_TAG |
| 110 | | ref = name |
| 111 | | end |
| 112 | | end |
| 113 | | elsif current_rev_type == "tag" |
| 114 | | if type == "tags" |
| 115 | | if action == :create |
| 116 | | action_id = Action::CREATE_TAG |
| 117 | | ref = name |
| 118 | | elsif action == :delete |
| 119 | | action_id = Action::DELETE_TAG |
| 120 | | ref = name |
| 119 | elsif current_rev_type == "tag" |
| 120 | if type == "tags" |
| 121 | if action == :create |
| 122 | action_id = Action::CREATE_TAG |
| 123 | ref = name |
| 124 | elsif action == :delete |
| 125 | action_id = Action::DELETE_TAG |
| 126 | ref = name |
| 127 | end |
| 121 | 128 | end |
| 122 | 129 | end |
| 130 | |
| 131 | next unless action_id |
| 132 | |
| 133 | project.create_event(action_id, repository, user, ref, hash[:message], hash[:date]) |
| 134 | action = :update |
| 123 | 135 | end |
| 124 | | |
| 125 | | next unless action_id |
| 126 | | |
| 127 | | # puts "#{hash[:author]}: #{Action.name(action_id)} #{ref} on #{slug} [#{hash[:date]}]" |
| 128 | | # puts " #{hash[:message]}" |
| 129 | | |
| 130 | | project.create_event(action_id, repository, user, ref, hash[:message]) |
| 131 | 136 | end |
| 132 | 137 | |
| 133 | 138 | puts "[OK]" |
| toggle raw diff |
--- a/data/hooks/post-receive
+++ b/data/hooks/post-receive
@@ -7,18 +7,18 @@ require 'rails_env'
require 'date'
gitdir = File.expand_path(File.join(incpath, ".."))
-repository_name = File.basename(gitdir)
-slug = ""
+repository = Repository.find_by_path(gitdir)
-if repository_name =~ /\.git$/
- repository_name.sub!(/\.git$/, "")
- slug = File.basename(File.expand_path(File.join(gitdir, "..")))
+if repository.nil?
+ $stderr.puts "Unknown repository"
+ exit 0
end
+project = repository.project
git = Grit::Git.new(gitdir)
while line = gets
- #puts "@@> #{line.inspect}"
+# puts "@@> #{line.inspect}"
# 3b480c5c30962c9f5b82a9c61a75992dc605de21 ee27ee3a022802c0569f9cdef6e9ad29aea096a8 refs/heads/master\n
oldrev, newrev, revname = line.split(" ")
current_rev = newrev
@@ -51,83 +51,88 @@ while line = gets
# type => heads, tags, remotes
# name => branch name
path, type, name = revname.split("/")
+ revs = [ current_rev ]
+ emails = [git.show({:pretty => "format:%ce", :s => true}, current_rev)]
- info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %cd%nmessage: %s", :s => true}, current_rev )
-
- hash = {}
- info.each { |line|
- if line =~ /(\w+):\s(.*)$/
- key = $1.to_sym
- value = $2
-
- value = Date.parse(value) if key == :date
-
- hash[key] = value
+ if type == "heads"
+ if action == :update
+ revs = git.rev_list({}, "#{oldrev}..#{newrev}").split("\n")
+ emails = git.log({:pretty => "format:%ce"}, "#{oldrev}..#{newrev}").split("\n")
+ elsif action == :create && name == "master"
+ revs = git.rev_list({}, current_rev).split("\n")
+ emails = git.log({:pretty => "format:%ce"}, current_rev).split("\n")
end
- }
-
- # action => create, delete, update
- # rev_type => commit, tag
-
- user = User.find_by_email(hash[:email])
- if user.nil?
- # TODO: no user should be ok, no need to skip
- # $stdout.puts "** The email '#{hash[:email]}' is not registered."
- next
end
- project = Project.find_by_slug(slug)
- repository = nil
+ users = repository.committers.find(:all, :conditions => ["email in (?)", emails])
+ user_map = users.inject({}) { |hash, user| hash[user.email] = user; hash }
- project.repositories.each { |repo|
- if repo.name == repository_name
- repository = repo
- break
+ revs.each do |sha1|
+ info = git.show({ :pretty => "format:author: %cn%nemail: %ce%ndate: %ct%nmessage: %s", :s => true}, sha1)
+
+ hash = {}
+ info.each { |line|
+ if line =~ /(\w+):\s(.*)$/
+ key = $1.to_sym
+ value = $2
+
+ value = Time.at(value.to_i).utc if key == :date
+
+ hash[key] = value
+ end
+ }
+
+ # action => create, delete, update
+ # rev_type => commit, tag
+
+ user = user_map[hash[:email]]
+ if user.nil?
+ # TODO: no user should be ok, no need to skip
+ # $stdout.puts "** The email '#{hash[:email]}' is not registered."
+ next
end
- }
-
- action_id = nil
- ref = nil
- if current_rev_type == "commit"
- if type == "heads"
- case action
- when :create
- action_id = Action::CREATE_BRANCH
+
+ action_id = nil
+ ref = nil
+ if current_rev_type == "commit"
+ if type == "heads"
+ case action
+ when :create
+ action_id = Action::CREATE_BRANCH
+ ref = name
+ when :update
+ action_id = Action::COMMIT
+ ref = sha1
+ when :delete
+ action_id = Action::DELETE_BRANCH
+ ref = name
+ end
+ elsif type == "tags"
+ if action == :create
+ action_id = Action::CREATE_TAG
ref = name
- when :update
- action_id = Action::COMMIT
- ref = current_rev
- when :delete
- action_id = Action::DELETE_BRANCH
+ elsif action == :delete
+ action_id = Action::DELETE_TAG
ref = name
+ end
end
- elsif type == "tags"
- if action == :create
- action_id = Action::CREATE_TAG
- ref = name
- elsif action == :delete
- action_id = Action::DELETE_TAG
- ref = name
- end
- end
- elsif current_rev_type == "tag"
- if type == "tags"
- if action == :create
- action_id = Action::CREATE_TAG
- ref = name
- elsif action == :delete
- action_id = Action::DELETE_TAG
- ref = name
+ elsif current_rev_type == "tag"
+ if type == "tags"
+ if action == :create
+ action_id = Action::CREATE_TAG
+ ref = name
+ elsif action == :delete
+ action_id = Action::DELETE_TAG
+ ref = name
+ end
end
end
+
+ next unless action_id
+
+ project.create_event(action_id, repository, user, ref, hash[:message], hash[:date])
+ action = :update
end
-
- next unless action_id
-
- # puts "#{hash[:author]}: #{Action.name(action_id)} #{ref} on #{slug} [#{hash[:date]}]"
- # puts " #{hash[:message]}"
-
- project.create_event(action_id, repository, user, ref, hash[:message])
end
puts "[OK]" |