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

61 lines
1.9 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
# == Schema Information
#
# Table name: web_hooks
#
# 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)
#
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
default_value_for :merge_requests_events, false
2015-04-26 12:48:37 +05:30
default_value_for :tag_push_events, false
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
validates :url, presence: true,
2015-04-26 12:48:37 +05:30
format: { with: /\A#{URI.regexp(%w(http https))}\z/, message: "should be a valid url" }
2014-09-02 18:07:02 +05:30
def execute(data)
parsed_url = URI.parse(url)
if parsed_url.userinfo.blank?
2015-04-26 12:48:37 +05:30
WebHook.post(url,
body: data.to_json,
headers: { "Content-Type" => "application/json" },
verify: false)
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),
}
WebHook.post(post_url,
body: data.to_json,
2015-04-26 12:48:37 +05:30
headers: { "Content-Type" => "application/json" },
2014-09-02 18:07:02 +05:30
verify: false,
basic_auth: auth)
end
2015-04-26 12:48:37 +05:30
rescue SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::OpenTimeout => e
logger.error("WebHook Error => #{e}")
false
2014-09-02 18:07:02 +05:30
end
def async_execute(data)
Sidekiq::Client.enqueue(ProjectWebHookWorker, id, data)
end
end