rosa-build/app/models/hook.rb

148 lines
4.2 KiB
Ruby
Raw Normal View History

class Hook < ActiveRecord::Base
include WebHooks
include UrlHelper
include Rails.application.routes.url_helpers
belongs_to :project
before_validation :cleanup_data
validates :project, :data, presence: true
2014-01-21 04:51:49 +00:00
validates :name, presence: true, inclusion: {in: NAMES}
attr_accessible :data, :name
serialize :data, Hash
scope :for_name, ->(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
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 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
}
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)
default_url_options
project = Project.find(git_hook['project']['id'])
user = User.find(git_hook['user']['id'])
2013-04-25 16:27:48 +01:00
payload = meta(project, user)
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!(
: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|
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
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
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 }
},
2014-01-21 04:51:49 +00:00
sender: {login: user.uname},
pusher: {name: user.uname}
}
end
def cleanup_data
if self.name.present? && fields = SCHEMA[self.name.to_sym]
new_data = {}
fields.each { |type, field| new_data[field] = self.data[field] }
self.data = new_data
end
end
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
end