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

69 lines
2.3 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)
2015-09-11 14:41:01 +05:30
# note_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
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
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-04-26 12:48:37 +05:30
WebHook.post(url,
body: data.to_json,
2015-09-11 14:41:01 +05:30
headers: {
"Content-Type" => "application/json",
"X-Gitlab-Event" => hook_name.singularize.titleize
},
2015-04-26 12:48:37 +05:30
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-09-11 14:41:01 +05:30
headers: {
"Content-Type" => "application/json",
"X-Gitlab-Event" => hook_name.singularize.titleize
},
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
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