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