2012-02-06 09:31:22 +00:00
|
|
|
class GitHook
|
2014-03-11 07:39:25 +00:00
|
|
|
include Feed::Git
|
|
|
|
include Resque::Plugins::Status
|
|
|
|
|
2013-03-07 08:06:34 +00:00
|
|
|
ZERO = '0000000000000000000000000000000000000000'
|
|
|
|
@queue = :hook
|
|
|
|
|
2012-02-06 09:31:22 +00:00
|
|
|
attr_reader :repo, :newrev, :oldrev, :newrev_type, :oldrev_type, :refname,
|
2013-02-25 18:55:45 +00:00
|
|
|
:change_type, :rev, :rev_type, :refname_type, :owner, :project, :user, :message
|
2012-02-06 09:31:22 +00:00
|
|
|
|
2013-03-07 08:06:34 +00:00
|
|
|
def self.perform(*options)
|
|
|
|
self.process(*options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(owner_uname, repo, newrev, oldrev, ref, newrev_type, user = nil, message = nil)
|
|
|
|
@repo, @newrev, @oldrev, @refname, @newrev_type, @user, @message = repo, newrev, oldrev, ref, newrev_type, user, message
|
2014-01-21 04:51:49 +00:00
|
|
|
if @owner = User.where(uname: owner_uname).first || Group.where(uname: owner_uname).first!
|
|
|
|
@project = @owner.own_projects.where(name: repo).first!
|
2012-03-06 18:20:14 +00:00
|
|
|
end
|
2013-03-07 08:06:34 +00:00
|
|
|
@change_type, @user = git_change_type, find_user(user)
|
2012-02-06 09:31:22 +00:00
|
|
|
git_revision_types
|
|
|
|
commit_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def git_change_type
|
2013-03-07 08:06:34 +00:00
|
|
|
if oldrev == ZERO
|
2012-02-06 09:31:22 +00:00
|
|
|
return 'create'
|
2013-03-07 08:06:34 +00:00
|
|
|
elsif newrev == ZERO
|
2012-02-06 09:31:22 +00:00
|
|
|
return 'delete'
|
|
|
|
else
|
|
|
|
return 'update'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def git_revision_types
|
2013-03-07 08:06:34 +00:00
|
|
|
case change_type
|
2012-02-06 09:31:22 +00:00
|
|
|
when 'create', 'update'
|
2013-03-07 08:06:34 +00:00
|
|
|
@rev = newrev
|
2012-02-06 09:31:22 +00:00
|
|
|
when 'delete'
|
2013-03-07 08:06:34 +00:00
|
|
|
@rev = oldrev
|
|
|
|
end
|
|
|
|
@rev_type = newrev_type
|
2012-02-06 09:31:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def commit_type
|
2013-03-07 08:06:34 +00:00
|
|
|
if refname =~ /refs\/tags\/*/ && rev_type == 'commit'
|
2012-02-06 09:31:22 +00:00
|
|
|
# un-annotated tag
|
|
|
|
@refname_type= 'tag'
|
|
|
|
#~ short_refname=refname + '##refs/tags/'
|
2013-03-07 08:06:34 +00:00
|
|
|
elsif refname =~ /refs\/tags\/*/ && rev_type == 'tag'
|
2012-02-06 09:31:22 +00:00
|
|
|
# annotated tag
|
|
|
|
@refname_type="annotated tag"
|
|
|
|
#~ short_refname= refname + '##refs/tags/'
|
2013-03-07 08:06:34 +00:00
|
|
|
elsif refname =~ /refs\/heads\/*/ && rev_type == 'commit'
|
2012-02-06 09:31:22 +00:00
|
|
|
# branch
|
|
|
|
@refname_type= 'branch'
|
2013-03-07 08:06:34 +00:00
|
|
|
elsif refname =~ /refs\/remotes\/*'/ && rev_type == 'commit'
|
2012-02-06 09:31:22 +00:00
|
|
|
# tracking branch
|
|
|
|
@refname_type="tracking branch"
|
2013-03-07 08:06:34 +00:00
|
|
|
@short_refname= refname + '##refs/remotes/'
|
2012-02-06 09:31:22 +00:00
|
|
|
else
|
|
|
|
# Anything else (is there anything else?)
|
|
|
|
@refname_type= "*** Unknown type of update to $refname (#{rev_type})"
|
|
|
|
end
|
|
|
|
end
|
2012-06-16 19:27:46 +01:00
|
|
|
|
|
|
|
def self.process(*args)
|
2014-03-18 09:39:58 +00:00
|
|
|
Feed::Git.create_notifications(args.size > 1 ? GitHook.new(*args) : args.first)
|
2012-06-16 19:27:46 +01:00
|
|
|
end
|
2013-03-07 08:06:34 +00:00
|
|
|
|
|
|
|
def find_user(user)
|
|
|
|
if user.blank?
|
|
|
|
# Local push
|
|
|
|
User.find_by_email(project.repo.commit(newrev).author.email) rescue nil
|
|
|
|
elsif user =~ /\Auser-\d+\Z/
|
|
|
|
# git push over http
|
|
|
|
User.find(user.gsub('user-', ''))
|
|
|
|
elsif user =~ /\Akey-\d+\Z/
|
|
|
|
# git push over ssh
|
|
|
|
SshKey.find_by_id(user.gsub('key-', '')).try(:user)
|
|
|
|
end
|
|
|
|
end
|
2012-02-29 01:30:22 +00:00
|
|
|
end
|