debian-mirror-gitlab/app/models/hooks/web_hook.rb

74 lines
2.8 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
# == Schema Information
#
# Table name: web_hooks
#
2015-11-26 14:37:03 +05:30
# id :integer not null, primary key
# url :string(255)
# project_id :integer
# created_at :datetime
# updated_at :datetime
# type :string(255) default("ProjectHook")
# service_id :integer
# push_events :boolean default(TRUE), not null
# issues_events :boolean default(FALSE), not null
# merge_requests_events :boolean default(FALSE), not null
# tag_push_events :boolean default(FALSE)
# note_events :boolean default(FALSE), not null
# enable_ssl_verification :boolean default(TRUE)
# build_events :boolean default(FALSE), not null
2014-09-02 18:07:02 +05:30
#
class WebHook < ActiveRecord::Base
2015-04-26 12:48:37 +05:30
include Sortable
2014-09-02 18:07:02 +05:30
include HTTParty
default_value_for :push_events, true
default_value_for :issues_events, false
2015-09-11 14:41:01 +05:30
default_value_for :note_events, false
2014-09-02 18:07:02 +05:30
default_value_for :merge_requests_events, false
2015-04-26 12:48:37 +05:30
default_value_for :tag_push_events, false
2015-12-23 02:04:40 +05:30
default_value_for :build_events, false
2015-09-25 12:07:36 +05:30
default_value_for :enable_ssl_verification, true
2014-09-02 18:07:02 +05:30
# HTTParty timeout
2015-04-26 12:48:37 +05:30
default_timeout Gitlab.config.gitlab.webhook_timeout
2014-09-02 18:07:02 +05:30
2015-12-23 02:04:40 +05:30
validates :url, presence: true, url: true
2014-09-02 18:07:02 +05:30
2015-09-11 14:41:01 +05:30
def execute(data, hook_name)
2014-09-02 18:07:02 +05:30
parsed_url = URI.parse(url)
if parsed_url.userinfo.blank?
2015-12-23 02:04:40 +05:30
response = WebHook.post(url,
body: data.to_json,
headers: {
"Content-Type" => "application/json",
"X-Gitlab-Event" => hook_name.singularize.titleize
},
verify: enable_ssl_verification)
2014-09-02 18:07:02 +05:30
else
post_url = url.gsub("#{parsed_url.userinfo}@", "")
auth = {
username: URI.decode(parsed_url.user),
password: URI.decode(parsed_url.password),
}
2015-12-23 02:04:40 +05:30
response = WebHook.post(post_url,
body: data.to_json,
headers: {
"Content-Type" => "application/json",
"X-Gitlab-Event" => hook_name.singularize.titleize
},
verify: enable_ssl_verification,
basic_auth: auth)
2014-09-02 18:07:02 +05:30
end
2015-12-23 02:04:40 +05:30
[(response.code >= 200 && response.code < 300), ActionView::Base.full_sanitizer.sanitize(response.to_s)]
2015-12-23 02:04:40 +05:30
rescue SocketError, OpenSSL::SSL::SSLError, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::OpenTimeout => e
2015-04-26 12:48:37 +05:30
logger.error("WebHook Error => #{e}")
2015-12-23 02:04:40 +05:30
[false, e.to_s]
2014-09-02 18:07:02 +05:30
end
2015-09-11 14:41:01 +05:30
def async_execute(data, hook_name)
Sidekiq::Client.enqueue(ProjectWebHookWorker, id, data, hook_name)
2014-09-02 18:07:02 +05:30
end
end