[refs #134] add post receive hook to repos

This commit is contained in:
Alexander Machehin 2012-02-06 15:31:22 +06:00
parent edd2ae2d86
commit fde67a2c22
4 changed files with 96 additions and 0 deletions

56
app/models/git_hook.rb Normal file
View File

@ -0,0 +1,56 @@
class GitHook
attr_reader :repo, :newrev, :oldrev, :newrev_type, :oldrev_type, :refname,
:change_type, :rev, :rev_type, :refname_type, :owner, :project
def initialize(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type)
@repo, @newrev, @oldrev, @refname, @newrev_type, @oldrev_type = repo, newrev, oldrev, ref, newrev_type, oldrev_type
@owner = User.find_by_uname owner_uname
@project = @owner.projects.where(:name => repo).first
@change_type = git_change_type
git_revision_types
commit_type
end
def git_change_type
if @oldrev =~ /0+$/
return 'create'
elsif @newrev =~ /0+$/
return 'delete'
else
return 'update'
end
end
def git_revision_types
case @change_type
when 'create', 'update'
@rev = @newrev
@rev_type = @newrev_type
when 'delete'
@rev = @oldrev
@rev_type = @oldrev_type
end
end
def commit_type
if @refname =~ /refs\/tags\/*/ && @rev_type == 'commit'
# un-annotated tag
@refname_type= 'tag'
#~ short_refname=refname + '##refs/tags/'
elsif @refname =~ /refs\/tags\/*/ && @rev_type == 'tag'
# annotated tag
@refname_type="annotated tag"
#~ short_refname= refname + '##refs/tags/'
elsif @refname =~ /refs\/heads\/*/ && @rev_type == 'commit'
# branch
@refname_type= 'branch'
elsif @refname =~ /refs\/remotes\/*'/ && @rev_type == 'commit'
# tracking branch
@refname_type="tracking branch"
@short_refname= @refname + '##refs/remotes/'
else
# Anything else (is there anything else?)
@refname_type= "*** Unknown type of update to $refname (#{rev_type})"
end
end
end

View File

@ -162,6 +162,11 @@ class Project < ActiveRecord::Base
owner == user owner == user
end end
def self.process_hook(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type)
rec = GitHook.new(owner_uname, repo, newrev, oldrev, ref, newrev_type, oldrev_type)
#ActivityFeedObserver.instance.after_create rec # for example
end
protected protected
def build_path(dir) def build_path(dir)
@ -178,6 +183,7 @@ class Project < ActiveRecord::Base
def create_git_repo def create_git_repo
is_root? ? Grit::Repo.init_bare(path) : parent.git_repository.repo.delay.fork_bare(path) is_root? ? Grit::Repo.init_bare(path) : parent.git_repository.repo.delay.fork_bare(path)
write_hook.delay
end end
def destroy_git_repo def destroy_git_repo
@ -205,4 +211,10 @@ class Project < ActiveRecord::Base
FileUtils.rm_rf wiki_path FileUtils.rm_rf wiki_path
end end
def write_hook
hook_file = File.join(path, 'hooks', 'post-receive')
FileUtils.cp(File.join(::Rails.root.to_s, 'lib', 'post-receive-hook'), hook_file)
#File.chmod(0775, hook_file) # need?
rescue Exception # FIXME
end
end end

View File

@ -0,0 +1,13 @@
class WriteGitHookToProjects < ActiveRecord::Migration
def self.up
origin_hook = File.join(::Rails.root.to_s, 'lib', 'post-receive-hook')
Project.all.each do |project|
hook_file = File.join(project.path, 'hooks', 'post-receive')
FileUtils.cp(origin_hook, hook_file)
end
end
def self.down
Project.all.each { |project| FileUtils.rm_rf File.join(project.path, 'hooks', 'post-receive')}
end
end

15
lib/post-receive-hook Executable file
View File

@ -0,0 +1,15 @@
#!/bin/bash
# This file was placed here by rosa-team. It makes sure that your pushed commits will be processed properly.
pwd=`pwd`
reponame=`basename $pwd .git`
owner=`basename \`dirname $pwd\``
while read oldrev newrev ref
do
newrev_type=$(git cat-file -t $newrev 2> /dev/null)
oldrev_type=$(git cat-file -t "$oldrev" 2> /dev/null)
/bin/bash -l -c "cd /srv/rosa_build/current && bundle exec rails runner 'Project.delay.process_hook(\"$owner\", \"$reponame\", \"$newrev\", \"$oldrev\", \"$ref\", \"$newrev_type\", \"$oldrev_type\")'"
done