Skip to content

Commit

Permalink
feat(webhooks): allow the http codes to be considered as "successful"…
Browse files Browse the repository at this point in the history
… to be configured.

* feat/239: Add new configuration 'webhook_http_code_whitelist' to allow webhook to be successful on defined HTTP codes. Default are: 200 (OK), 201 (CREATED) and 202 (ACCEPTED)

* feat: 239 Pull Request proposals:
* rename "webhook_http_code_whitelist" to "webhook_http_code_success"
* Rollback test case descriptions
* Add other 2xx status (203 - 206)
  • Loading branch information
pitschr authored Feb 11, 2021
1 parent 5ca9de6 commit a84989b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ app = PactBroker::App.new do | config |
config.webhook_host_whitelist = [/.*/, "10.0.0.0/8"]
config.webhook_scheme_whitelist = ['http', 'https']
config.webhook_http_method_whitelist = ['GET', 'POST']
config.webhook_http_code_success = [200, 201, 202, 203, 204, 205, 206]
#config.base_url = ENV['PACT_BROKER_BASE_URL']

database_logger = PactBroker::DB::LogQuietener.new(config.logger)
Expand Down
25 changes: 25 additions & 0 deletions lib/pact_broker/config/space_delimited_integer_list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module PactBroker
module Config
class SpaceDelimitedIntegerList < Array
def initialize list
super(list)
end

def self.integer?(string)
(Integer(string) rescue nil) != nil
end

def self.parse(string)
array = (string || '')
.split(' ')
.select { |word| integer?(word) }
.collect(&:to_i)
SpaceDelimitedIntegerList.new(array)
end

def to_s
collect(&:to_s).join(' ')
end
end
end
end
18 changes: 17 additions & 1 deletion lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'pact_broker/version'
require 'pact_broker/error'
require 'pact_broker/config/space_delimited_string_list'
require 'pact_broker/config/space_delimited_integer_list'
require 'semantic_logger'

module PactBroker
Expand Down Expand Up @@ -49,7 +50,7 @@ class Configuration
attr_accessor :check_for_potential_duplicate_pacticipant_names
attr_accessor :webhook_retry_schedule
attr_accessor :user_agent
attr_reader :webhook_http_method_whitelist, :webhook_scheme_whitelist, :webhook_host_whitelist
attr_reader :webhook_http_method_whitelist, :webhook_scheme_whitelist, :webhook_host_whitelist, :webhook_http_code_success
attr_accessor :semver_formats
attr_accessor :enable_public_badge_access, :shields_io_base_url, :badge_provider_mode
attr_accessor :disable_ssl_verification
Expand Down Expand Up @@ -105,6 +106,7 @@ def self.default_configuration
config.check_for_potential_duplicate_pacticipant_names = true
config.disable_ssl_verification = false
config.webhook_http_method_whitelist = ['POST']
config.webhook_http_code_success = [200, 201, 202, 203, 204, 205, 206]
config.webhook_scheme_whitelist = ['https']
config.webhook_host_whitelist = []
# TODO get rid of unsafe-inline
Expand Down Expand Up @@ -240,6 +242,10 @@ def webhook_http_method_whitelist= webhook_http_method_whitelist
@webhook_http_method_whitelist = parse_space_delimited_string_list_property('webhook_http_method_whitelist', webhook_http_method_whitelist)
end

def webhook_http_code_success= webhook_http_code_success
@webhook_http_code_success = parse_space_delimited_integer_list_property('webhook_http_code_success', webhook_http_code_success)
end

def webhook_scheme_whitelist= webhook_scheme_whitelist
@webhook_scheme_whitelist = parse_space_delimited_string_list_property('webhook_scheme_whitelist', webhook_scheme_whitelist)
end
Expand Down Expand Up @@ -269,5 +275,15 @@ def parse_space_delimited_string_list_property property_name, property_value
raise ConfigurationError.new("Pact Broker configuration property `#{property_name}` must be a space delimited String or an Array")
end
end

def parse_space_delimited_integer_list_property property_name, property_value
case property_value
when String then Config::SpaceDelimitedIntegerList.parse(property_value)
when Array then Config::SpaceDelimitedIntegerList.new(property_value)
else
raise ConfigurationError.new("Pact Broker configuration property `#{property_name}` must be a space delimited String or an Array with Integer values")
end
end

end
end
6 changes: 5 additions & 1 deletion lib/pact_broker/webhooks/webhook_request_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,12 @@ def log_error uuid, e, base_url
end

def success?(response)
!response.nil? && response.code.to_i < 300
unless response.nil?
# Response HTTP Code must be in success list otherwise it is false
PactBroker.configuration.webhook_http_code_success.include? response.code.to_i
end
end

end
end
end
47 changes: 47 additions & 0 deletions spec/lib/pact_broker/config/space_delimited_integer_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'pact_broker/config/space_delimited_integer_list'

module PactBroker
module Config
describe SpaceDelimitedIntegerList do
describe "parse" do
subject { SpaceDelimitedIntegerList.parse(input) }

context "when input is ''" do
let(:input) { "" }

it { is_expected.to eq [] }
it { is_expected.to be_a SpaceDelimitedIntegerList }

its(:to_s) { is_expected.to eq input }
end

context "when input is 'off'" do
let(:input) { "off" }

it { is_expected.to eq [] }
it { is_expected.to be_a SpaceDelimitedIntegerList }

its(:to_s) { is_expected.to eq "" }
end

context "when input is '0 1 1 2 3 5 8 13 21 34'" do
let(:input) { "0 1 1 2 3 5 8 13 21 34" }

it { is_expected.to eq [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] }
it { is_expected.to be_a SpaceDelimitedIntegerList }

its(:to_s) { is_expected.to eq input }
end

context "when input is '13 17 foo 19'" do
let(:input) { "13 17 foo 19" }

it { is_expected.to eq [13, 17, 19] }
it { is_expected.to be_a SpaceDelimitedIntegerList }

its(:to_s) { is_expected.to eq "13 17 19" }
end
end
end
end
end
12 changes: 12 additions & 0 deletions spec/lib/pact_broker/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ module PactBroker
end
end

describe "webhook_http_code_success" do
it "allows setting the 'webhook_http_code_success' by a space-delimited string" do
PactBroker.configuration.webhook_http_code_success = "200 201 202"
expect(PactBroker.configuration.webhook_http_code_success).to be_a Config::SpaceDelimitedIntegerList
end

it "allows setting the 'webhook_http_code_success' by an array" do
PactBroker.configuration.webhook_http_code_success = [200, 201, 202]
expect(PactBroker.configuration.webhook_http_code_success).to be_a Config::SpaceDelimitedIntegerList
end
end

describe "webhook_scheme_whitelist" do
it "allows setting the whitelist by a string" do
PactBroker.configuration.webhook_scheme_whitelist = "foo"
Expand Down
8 changes: 8 additions & 0 deletions spec/lib/pact_broker/webhooks/webhook_request_logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@ module Webhooks
end
end

context "when the response code '100' is not in 'webhook_http_code_success'" do
let(:status) { 100 }

it "not successful, code '100' not in 'webhook_http_code_success'" do
expect(logs).to include "oops"
end
end

context "when the response code is not successful" do
let(:status) { 400 }

Expand Down

0 comments on commit a84989b

Please sign in to comment.