-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16468 from opf/fix/webhooks-error
Also log errors on errors without response
- Loading branch information
Showing
5 changed files
with
127 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
modules/webhooks/app/services/webhooks/outgoing/request_webhook_service.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module Webhooks | ||
module Outgoing | ||
class RequestWebhookService | ||
include ::OpenProjectErrorHelper | ||
|
||
attr_reader :current_user, :event_name, :webhook | ||
|
||
def initialize(webhook, event_name:, current_user:) | ||
@current_user = current_user | ||
@webhook = webhook | ||
@event_name = event_name | ||
end | ||
|
||
def call!(body:, headers:) | ||
begin | ||
response = Faraday.post( | ||
webhook.url, | ||
body, | ||
headers | ||
) | ||
rescue Faraday::Error => e | ||
response = e.response | ||
exception = e | ||
rescue StandardError => e | ||
op_handle_error(e.message, reference: :webhook_job) | ||
exception = e | ||
end | ||
|
||
log!(body:, headers:, response:, exception:) | ||
|
||
# We want to re-raise timeout exceptions | ||
# but log the request beforehand | ||
raise exception if exception.is_a?(Faraday::TimeoutError) | ||
end | ||
|
||
def log!(body:, headers:, response:, exception:) | ||
log = ::Webhooks::Log.new( | ||
webhook:, | ||
event_name:, | ||
url: webhook.url, | ||
request_headers: headers, | ||
request_body: body, | ||
**response_attributes(response:, exception:) | ||
) | ||
|
||
unless log.save | ||
OpenProject.logger.error("Failed to save webhook log: #{log.errors.full_messages.join('. ')}") | ||
end | ||
end | ||
|
||
def response_attributes(response:, exception:) | ||
{ | ||
response_code: response&.status || -1, | ||
response_headers: response&.headers&.to_h&.transform_keys { |k| k.underscore.to_sym }, | ||
response_body: response&.body || exception&.message | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
modules/webhooks/spec/services/webhooks/outgoing/request_webhook_service_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#-- copyright | ||
# OpenProject is an open source project management software. | ||
# Copyright (C) the OpenProject GmbH | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License version 3. | ||
# | ||
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: | ||
# Copyright (C) 2006-2013 Jean-Philippe Lang | ||
# Copyright (C) 2010-2013 the ChiliProject Team | ||
# | ||
# This program is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU General Public License | ||
# as published by the Free Software Foundation; either version 2 | ||
# of the License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
# | ||
# See COPYRIGHT and LICENSE files for more details. | ||
#++ | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe Webhooks::Outgoing::RequestWebhookService, type: :model do | ||
let(:user) { build_stubbed(:user) } | ||
let(:instance) { described_class.new(webhook, event_name: :created, current_user: user) } | ||
|
||
shared_let(:webhook) { create(:webhook, all_projects: true, url: "https://example.net/test/42", secret: nil) } | ||
|
||
subject { instance.call!(body: "body", headers: {}) } | ||
|
||
describe "#call!" do | ||
context "when request_url fails with SSL errors" do | ||
it "still logs the exception" do | ||
allow(Faraday) | ||
.to receive(:post) | ||
.with(webhook.url, *any_args) | ||
.and_raise(Faraday::SSLError, "SSL error") | ||
|
||
expect { subject } | ||
.to change(Webhooks::Log, :count).by(1) | ||
end | ||
end | ||
end | ||
end |