2012-11-19 13:24:05 -05:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: web_hooks
|
|
|
|
#
|
2013-12-03 12:11:10 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# url :string(255)
|
|
|
|
# project_id :integer
|
2014-04-09 08:05:03 -04:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-12-03 12:11:10 -05:00
|
|
|
# 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
|
2014-04-09 08:05:03 -04:00
|
|
|
# tag_push_events :boolean default(FALSE)
|
2012-11-19 13:24:05 -05:00
|
|
|
#
|
|
|
|
|
2011-12-14 11:38:52 -05:00
|
|
|
class WebHook < ActiveRecord::Base
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2011-12-14 11:38:52 -05:00
|
|
|
include HTTParty
|
|
|
|
|
2014-02-27 08:56:35 -05:00
|
|
|
default_value_for :push_events, true
|
|
|
|
default_value_for :issues_events, false
|
|
|
|
default_value_for :merge_requests_events, false
|
2014-09-19 04:23:18 -04:00
|
|
|
default_value_for :tag_push_events, false
|
2014-02-27 08:56:35 -05:00
|
|
|
|
2011-12-14 11:38:52 -05:00
|
|
|
# HTTParty timeout
|
2014-09-12 11:38:14 -04:00
|
|
|
default_timeout Gitlab.config.gitlab.webhook_timeout
|
2011-12-14 11:38:52 -05:00
|
|
|
|
2012-09-27 02:20:36 -04:00
|
|
|
validates :url, presence: true,
|
2015-04-11 05:38:14 -04:00
|
|
|
format: { with: /\A#{URI.regexp(%w(http https))}\z/, message: "should be a valid url" }
|
2011-12-14 11:38:52 -05:00
|
|
|
|
|
|
|
def execute(data)
|
2012-08-01 22:48:46 -04:00
|
|
|
parsed_url = URI.parse(url)
|
|
|
|
if parsed_url.userinfo.blank?
|
2014-12-04 08:07:01 -05:00
|
|
|
WebHook.post(url,
|
|
|
|
body: data.to_json,
|
|
|
|
headers: { "Content-Type" => "application/json" },
|
|
|
|
verify: false)
|
2012-08-01 22:48:46 -04:00
|
|
|
else
|
2012-09-27 02:20:36 -04:00
|
|
|
post_url = url.gsub("#{parsed_url.userinfo}@", "")
|
2013-02-22 11:03:59 -05:00
|
|
|
auth = {
|
|
|
|
username: URI.decode(parsed_url.user),
|
|
|
|
password: URI.decode(parsed_url.password),
|
|
|
|
}
|
2012-08-01 22:48:46 -04:00
|
|
|
WebHook.post(post_url,
|
|
|
|
body: data.to_json,
|
2015-02-02 23:36:54 -05:00
|
|
|
headers: { "Content-Type" => "application/json" },
|
2014-01-15 09:16:19 -05:00
|
|
|
verify: false,
|
2013-02-22 11:03:59 -05:00
|
|
|
basic_auth: auth)
|
2012-08-01 22:48:46 -04:00
|
|
|
end
|
2015-02-03 16:57:28 -05:00
|
|
|
rescue SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::OpenTimeout => e
|
2014-12-04 08:07:01 -05:00
|
|
|
logger.error("WebHook Error => #{e}")
|
|
|
|
false
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|
2013-01-24 15:15:24 -05:00
|
|
|
|
|
|
|
def async_execute(data)
|
|
|
|
Sidekiq::Client.enqueue(ProjectWebHookWorker, id, data)
|
|
|
|
end
|
2011-12-14 11:38:52 -05:00
|
|
|
end
|