2013-04-12 14:26:18 +01:00
|
|
|
class Hook < ActiveRecord::Base
|
2014-03-11 08:58:36 +00:00
|
|
|
include WebHooks
|
|
|
|
include UrlHelper
|
2013-05-16 19:50:18 +01:00
|
|
|
include Rails.application.routes.url_helpers
|
2014-03-11 12:39:57 +00:00
|
|
|
|
2013-04-12 14:26:18 +01:00
|
|
|
belongs_to :project
|
|
|
|
|
2013-04-15 11:04:47 +01:00
|
|
|
before_validation :cleanup_data
|
2014-01-21 04:51:49 +00:00
|
|
|
validates :project_id, :data, presence: true
|
|
|
|
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
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
scope :for_name, ->(name) { where(name: name) if name.present? }
|
2013-04-14 16:46:56 +01:00
|
|
|
|
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 = {
|
2014-01-21 04:51:49 +00:00
|
|
|
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', {
|
2014-01-21 04:51:49 +00:00
|
|
|
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}"},
|
|
|
|
html_url: project_pull_request_url(pull.to_project, pull)
|
2013-04-15 21:10:54 +01:00
|
|
|
)
|
|
|
|
).to_json
|
|
|
|
}
|
|
|
|
else
|
|
|
|
post 'issues', {
|
2014-01-21 04:51:49 +00:00
|
|
|
payload: payload.merge(
|
|
|
|
action: (issue.closed? ? 'closed' : 'opened'),
|
|
|
|
issue: base_params.merge(
|
|
|
|
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
|
2014-01-21 04:51:49 +00: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)
|
2014-01-21 04:51:49 +00:00
|
|
|
|
2013-04-17 11:58:09 +01:00
|
|
|
commits = []
|
2014-01-21 04:51:49 +00:00
|
|
|
payload.merge!(before: oldrev, after: newrev)
|
2013-04-25 16:27:48 +01:00
|
|
|
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', {
|
2014-01-21 04:51:49 +00:00
|
|
|
payload: payload.merge(
|
|
|
|
ref: git_hook['refname'],
|
|
|
|
commits: commits.map{ |commit|
|
2013-05-16 16:41:59 +01:00
|
|
|
files = changed_files commit
|
2013-04-17 11:58:09 +01:00
|
|
|
{
|
2014-01-21 04:51:49 +00:00
|
|
|
id: commit.id,
|
|
|
|
message: commit.message,
|
|
|
|
distinct: true,
|
|
|
|
url: commit_url(project, commit),
|
|
|
|
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
|
2014-01-21 04:51:49 +00: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}"
|
2014-01-21 04:51:49 +00:00
|
|
|
Net::HTTP.post_form uri, params.merge(data: data.to_json)
|
2013-04-15 21:10:54 +01:00
|
|
|
rescue # Dont care about it
|
|
|
|
end
|
|
|
|
|
2013-04-15 18:51:19 +01:00
|
|
|
def meta(project, user)
|
|
|
|
{
|
2014-01-21 04:51:49 +00:00
|
|
|
repository: {
|
|
|
|
name: project.name,
|
|
|
|
url: project_url(project),
|
|
|
|
owner: { login: project.owner.uname }
|
2013-04-15 18:51:19 +01:00
|
|
|
},
|
2014-01-21 04:51:49 +00: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 = {}
|
2014-03-11 08:58:36 +00: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
|
2014-03-11 08:58:36 +00:00
|
|
|
{ removed: removed, added: added, modified: modified }
|
2013-05-14 12:59:39 +01:00
|
|
|
end
|
|
|
|
|
2013-04-12 14:26:18 +01:00
|
|
|
end
|