2013-04-12 14:26:18 +01:00
|
|
|
class Hook < ActiveRecord::Base
|
2013-04-15 14:34:11 +01:00
|
|
|
include Modules::Models::WebHooks
|
2013-05-16 19:50:18 +01:00
|
|
|
include Modules::Models::UrlHelper
|
|
|
|
include Rails.application.routes.url_helpers
|
2013-04-12 14:26:18 +01:00
|
|
|
belongs_to :project
|
|
|
|
|
2013-04-15 11:04:47 +01:00
|
|
|
before_validation :cleanup_data
|
2013-04-15 14:34:11 +01:00
|
|
|
validates :project_id, :data, :presence => true
|
2013-04-14 16:46:56 +01:00
|
|
|
validates :name, :presence => true, :inclusion => {:in => NAMES}
|
2013-04-12 14:26:18 +01:00
|
|
|
|
2013-04-14 16:46:56 +01:00
|
|
|
attr_accessible :data, :name
|
2013-04-12 14:26:18 +01:00
|
|
|
|
|
|
|
serialize :data, Hash
|
2013-04-14 16:46:56 +01:00
|
|
|
|
|
|
|
scope :for_name, lambda {|name| where(:name => name) if name.present? }
|
|
|
|
|
2013-04-17 11:58:09 +01:00
|
|
|
def receive_issues(issue, action)
|
2013-04-15 21:10:54 +01:00
|
|
|
pull = issue.pull_request
|
|
|
|
return if action.to_sym == :create && pull
|
2013-05-16 19:50:18 +01:00
|
|
|
default_url_options
|
2013-04-15 21:10:54 +01:00
|
|
|
|
|
|
|
payload = meta(issue.project, issue.user)
|
|
|
|
base_params = {
|
|
|
|
:number => issue.serial_id,
|
|
|
|
:state => issue.status,
|
|
|
|
:title => issue.title,
|
|
|
|
:body => issue.body,
|
|
|
|
:user => {:login => issue.user.uname},
|
2013-04-15 18:51:19 +01:00
|
|
|
}
|
2013-04-15 21:10:54 +01:00
|
|
|
if pull
|
|
|
|
total_commits = pull.repo.commits_between(pull.to_commit, pull.from_commit).count
|
|
|
|
repo_owner = pull.to_project.owner.uname
|
|
|
|
post 'pull_request', {
|
|
|
|
:payload => payload.merge(
|
|
|
|
:action => (pull.ready? ? 'opened' : pull.status),
|
|
|
|
:pull_request => base_params.merge(
|
|
|
|
:commits => total_commits,
|
|
|
|
:head => {:label => "#{pull.from_project.owner.uname}:#{pull.from_ref}"},
|
|
|
|
:base => {:label => "#{repo_owner}:#{pull.to_ref}"},
|
2013-05-16 19:50:18 +01:00
|
|
|
:html_url => project_pull_request_url(pull.to_project, pull)
|
2013-04-15 21:10:54 +01:00
|
|
|
)
|
|
|
|
).to_json
|
|
|
|
}
|
|
|
|
else
|
|
|
|
post 'issues', {
|
|
|
|
:payload => payload.merge(
|
|
|
|
:action => (issue.closed? ? 'closed' : 'opened'),
|
|
|
|
:issue => base_params.merge(
|
2013-05-16 19:50:18 +01:00
|
|
|
:html_url => project_issue_url(issue.project, issue)
|
2013-04-15 21:10:54 +01:00
|
|
|
)
|
|
|
|
).to_json
|
|
|
|
}
|
2013-04-15 18:51:19 +01:00
|
|
|
end
|
|
|
|
end
|
2013-05-16 13:42:28 +01:00
|
|
|
later :receive_issues, :queue => :notification
|
2013-04-17 11:58:09 +01:00
|
|
|
|
|
|
|
def receive_push(git_hook)
|
2013-05-16 19:50:18 +01:00
|
|
|
default_url_options
|
2013-04-25 16:27:48 +01:00
|
|
|
project = Project.find(git_hook['project']['project']['id'])
|
|
|
|
user = User.find(git_hook['user']['user']['id'])
|
|
|
|
payload = meta(project, user)
|
2013-05-16 16:41:59 +01:00
|
|
|
oldrev, newrev, change_type = git_hook.values_at *%w(oldrev newrev change_type)
|
2013-04-25 16:27:48 +01:00
|
|
|
|
2013-04-17 11:58:09 +01:00
|
|
|
commits = []
|
2013-04-25 16:27:48 +01:00
|
|
|
payload.merge!(:before => oldrev, :after => newrev)
|
|
|
|
if %w(delete create).exclude? change_type
|
2013-04-17 11:58:09 +01:00
|
|
|
payload.merge!(
|
2013-05-16 19:50:18 +01:00
|
|
|
:compare => diff_url(project, "#{oldrev[0..6]}...#{newrev[0..6]}")
|
2013-04-17 11:58:09 +01:00
|
|
|
)
|
2013-04-25 16:27:48 +01:00
|
|
|
if oldrev == newrev
|
2013-05-14 12:27:15 +01:00
|
|
|
commits = [project.repo.commit(newrev)]
|
|
|
|
modified = commits.first.stats.files.map{|f| f[0]}
|
2013-04-17 11:58:09 +01:00
|
|
|
else
|
2013-04-25 16:27:48 +01:00
|
|
|
commits = project.repo.commits_between(oldrev, newrev)
|
2013-04-17 11:58:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
post 'push', {
|
|
|
|
:payload => payload.merge(
|
2013-04-25 16:27:48 +01:00
|
|
|
:ref => git_hook['refname'],
|
2013-05-16 16:41:59 +01:00
|
|
|
:commits => commits.map{ |commit|
|
|
|
|
files = changed_files commit
|
2013-04-17 11:58:09 +01:00
|
|
|
{
|
2013-05-16 16:41:59 +01:00
|
|
|
:id => commit.id,
|
|
|
|
:message => commit.message,
|
2013-05-14 12:59:39 +01:00
|
|
|
:distinct => true,
|
2013-05-16 19:50:18 +01:00
|
|
|
:url => commit_url(project, commit),
|
2013-05-16 16:41:59 +01:00
|
|
|
:removed => files[:removed],
|
|
|
|
:added => files[:added],
|
|
|
|
:modified => files[:modified],
|
|
|
|
:timestamp => commit.committed_date,
|
|
|
|
:author => {:name => commit.committer.name, :email => commit.committer.email}
|
2013-04-17 11:58:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
).to_json
|
|
|
|
}
|
|
|
|
end
|
2013-05-16 13:42:28 +01:00
|
|
|
later :receive_push, :queue => :notification
|
2013-04-15 18:51:19 +01:00
|
|
|
|
2013-04-15 11:04:47 +01:00
|
|
|
protected
|
|
|
|
|
2013-04-15 21:10:54 +01:00
|
|
|
def post(action, params)
|
2013-04-17 12:55:22 +01:00
|
|
|
github_services = APP_CONFIG['github_services']
|
|
|
|
uri = URI "http://#{github_services['ip']}:#{github_services['port']}/#{name}/#{action}"
|
2013-04-15 21:10:54 +01:00
|
|
|
Net::HTTP.post_form uri, params.merge(:data => data.to_json)
|
|
|
|
rescue # Dont care about it
|
|
|
|
end
|
|
|
|
|
2013-04-15 18:51:19 +01:00
|
|
|
def meta(project, user)
|
|
|
|
{
|
|
|
|
:repository => {
|
|
|
|
:name => project.name,
|
2013-05-16 19:50:18 +01:00
|
|
|
:url => project_url(project),
|
2013-04-15 18:51:19 +01:00
|
|
|
:owner => { :login => project.owner.uname }
|
|
|
|
},
|
2013-04-17 11:58:09 +01:00
|
|
|
:sender => {:login => user.uname},
|
|
|
|
:pusher => {:name => user.uname}
|
2013-04-15 18:51:19 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-04-15 11:04:47 +01:00
|
|
|
def cleanup_data
|
2013-04-15 14:34:11 +01:00
|
|
|
if self.name.present? && fields = SCHEMA[self.name.to_sym]
|
2013-04-15 11:04:47 +01:00
|
|
|
new_data = {}
|
2013-04-15 14:34:11 +01:00
|
|
|
fields.each{ |type, field| new_data[field] = self.data[field] }
|
2013-04-15 11:04:47 +01:00
|
|
|
self.data = new_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-14 12:59:39 +01:00
|
|
|
def changed_files(commit)
|
|
|
|
removed, added, modified = [], [], []
|
|
|
|
commit.show.each do |diff|
|
|
|
|
if diff.renamed_file
|
|
|
|
added << diff.b_path
|
|
|
|
removed << diff.a_path
|
|
|
|
elsif diff.new_file
|
|
|
|
added << diff.b_path
|
|
|
|
elsif diff.deleted_file
|
|
|
|
removed << diff.a_path
|
|
|
|
else
|
|
|
|
modified << diff.a_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
{:removed => removed, :added => added, :modified => modified}
|
|
|
|
end
|
|
|
|
|
2013-04-12 14:26:18 +01:00
|
|
|
end
|