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
|
2016-09-29 09:46:39 +05:30
|
|
|
default_value_for :confidential_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
|
2016-09-13 17:45:13 +05:30
|
|
|
default_value_for :pipeline_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
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
scope :push_hooks, -> { where(push_events: true) }
|
|
|
|
scope :tag_push_hooks, -> { where(tag_push_events: 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,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: build_headers(hook_name),
|
2015-12-23 02:04:40 +05:30
|
|
|
verify: enable_ssl_verification)
|
2014-09-02 18:07:02 +05:30
|
|
|
else
|
2016-06-02 11:05:42 +05:30
|
|
|
post_url = url.gsub("#{parsed_url.userinfo}@", '')
|
2014-09-02 18:07:02 +05:30
|
|
|
auth = {
|
2016-01-19 16:12:03 +05:30
|
|
|
username: CGI.unescape(parsed_url.user),
|
|
|
|
password: CGI.unescape(parsed_url.password),
|
2014-09-02 18:07:02 +05:30
|
|
|
}
|
2015-12-23 02:04:40 +05:30
|
|
|
response = WebHook.post(post_url,
|
|
|
|
body: data.to_json,
|
2016-06-02 11:05:42 +05:30
|
|
|
headers: build_headers(hook_name),
|
2015-12-23 02:04:40 +05:30
|
|
|
verify: enable_ssl_verification,
|
|
|
|
basic_auth: auth)
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
2015-12-23 02:04:40 +05:30
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
[response.code, 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
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_headers(hook_name)
|
|
|
|
headers = {
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'X-Gitlab-Event' => hook_name.singularize.titleize
|
|
|
|
}
|
|
|
|
headers['X-Gitlab-Token'] = token if token.present?
|
|
|
|
headers
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|