Skip to content

Commit a4b69db

Browse files
committed
fix: correctly handle template parameters in URL when rendering webhook resource
1 parent 076afe6 commit a4b69db

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

lib/pact_broker/api/contracts/webhook_contract.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'reform'
22
require 'reform/form'
33
require 'pact_broker/webhooks/check_host_whitelist'
4+
require 'pact_broker/webhooks/render'
45

56
module PactBroker
67
module Api
@@ -106,7 +107,7 @@ def host_whitelist
106107
end
107108

108109
def parse_uri(uri_string, placeholder = 'placeholder')
109-
URI(uri_string.gsub(/\$\{pactbroker\.[^\}]+\}/, placeholder))
110+
URI(uri_string.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, placeholder))
110111
end
111112
end
112113

lib/pact_broker/domain/webhook_request.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def initialize attributes = {}
4545
end
4646

4747
def description
48-
"#{method.upcase} #{URI(url).host}"
48+
"#{method.upcase} #{URI(url.gsub(PactBroker::Webhooks::Render::TEMPLATE_PARAMETER_REGEXP, 'placeholder')).host}"
4949
end
5050

5151
def display_password

lib/pact_broker/webhooks/render.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
module PactBroker
22
module Webhooks
33
class Render
4+
5+
TEMPLATE_PARAMETER_REGEXP = /\$\{pactbroker\.[^\}]+\}/
6+
47
def self.call(template, pact, verification = nil, &escaper)
58
base_url = PactBroker.configuration.base_url
69
params = {

spec/lib/pact_broker/domain/webhook_request_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ module Domain
4343
it "returns a brief description of the HTTP request" do
4444
expect(subject.description).to eq 'POST example.org'
4545
end
46+
47+
context "when the URL has a template parameter in it" do
48+
let(:url) { "http://foo/commits/${pactbroker.consumerVersionNumber}" }
49+
50+
it "doesn't explode" do
51+
expect(subject.description).to eq 'POST foo'
52+
end
53+
end
4654
end
4755

4856
describe "display_password" do

spec/lib/pact_broker/webhooks/render_spec.rb

+24-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,25 @@ module Webhooks
1010
end
1111

1212
let(:pact) do
13-
instance_double("pact", consumer_version_number: "1.2.3+foo", consumer_name: "Foo", provider_name: "Bar")
13+
instance_double("pact", consumer_version_number: "1.2.3+foo", consumer_name: "Foo", provider_name: "Bar", latest_verification: nil)
14+
end
15+
16+
let(:pact_with_no_verification) { pact }
17+
18+
let(:pact_with_successful_verification) do
19+
instance_double("pact",
20+
consumer_version_number: "1.2.3+foo",
21+
consumer_name: "Foo",
22+
provider_name: "Bar",
23+
latest_verification: verification)
24+
end
25+
26+
let(:pact_with_failed_verification) do
27+
instance_double("pact",
28+
consumer_version_number: "1.2.3+foo",
29+
consumer_name: "Foo",
30+
provider_name: "Bar",
31+
latest_verification: failed_verification)
1432
end
1533

1634
let(:verification) do
@@ -21,6 +39,7 @@ module Webhooks
2139
instance_double("verification", provider_version_number: "3", success: false)
2240
end
2341

42+
let(:nil_pact) { nil }
2443
let(:nil_verification) { nil }
2544

2645
subject { Render.call(template, pact, verification) }
@@ -34,7 +53,10 @@ module Webhooks
3453
["${pactbroker.providerName}", "Bar", :pact, :verification],
3554
["${pactbroker.githubVerificationStatus}", "success", :pact, :verification],
3655
["${pactbroker.githubVerificationStatus}", "failure", :pact, :failed_verification],
37-
["${pactbroker.githubVerificationStatus}", "", :pact, :nil_verification]
56+
["${pactbroker.githubVerificationStatus}", "", :nil_pact, :nil_verification],
57+
["${pactbroker.githubVerificationStatus}", "pending", :pact_with_no_verification, :nil_verification],
58+
["${pactbroker.githubVerificationStatus}", "success", :pact_with_successful_verification, :nil_verification],
59+
["${pactbroker.githubVerificationStatus}", "failure", :pact_with_failed_verification, :nil_verification]
3860
]
3961

4062
TEST_CASES.each do | (template, expected_output, pact_var_name, verification_var_name) |

0 commit comments

Comments
 (0)