4
4
require 'pact_broker/messages'
5
5
require 'net/http'
6
6
require 'pact_broker/webhooks/redact_logs'
7
+ require 'pact_broker/api/pact_broker_urls'
7
8
8
9
module PactBroker
9
10
@@ -51,17 +52,18 @@ def execute pact, options = {}
51
52
logs = StringIO . new
52
53
execution_logger = Logger . new ( logs )
53
54
begin
54
- execute_and_build_result ( options , logs , execution_logger )
55
+ execute_and_build_result ( pact , options , logs , execution_logger )
55
56
rescue StandardError => e
56
57
handle_error_and_build_result ( e , options , logs , execution_logger )
57
58
end
58
59
end
59
60
60
61
private
61
62
62
- def execute_and_build_result options , logs , execution_logger
63
- req = build_request ( execution_logger )
64
- response = do_request ( req )
63
+ def execute_and_build_result pact , options , logs , execution_logger
64
+ uri = build_uri ( pact )
65
+ req = build_request ( uri , pact , execution_logger )
66
+ response = do_request ( uri , req )
65
67
log_response ( response , execution_logger )
66
68
result = WebhookExecutionResult . new ( response , logs . string )
67
69
log_completion_message ( options , execution_logger , result . success? )
@@ -75,9 +77,9 @@ def handle_error_and_build_result e, options, logs, execution_logger
75
77
WebhookExecutionResult . new ( nil , logs . string , e )
76
78
end
77
79
78
- def build_request execution_logger
79
- req = http_request
80
- execution_logger . info "HTTP/1.1 #{ method . upcase } #{ url_with_credentials } "
80
+ def build_request uri , pact , execution_logger
81
+ req = http_request ( uri )
82
+ execution_logger . info "HTTP/1.1 #{ method . upcase } #{ url_with_credentials ( pact ) } "
81
83
82
84
headers . each_pair do | name , value |
83
85
execution_logger . info Webhooks ::RedactLogs . call ( "#{ name } : #{ value } " )
@@ -88,17 +90,17 @@ def build_request execution_logger
88
90
89
91
unless body . nil?
90
92
if String === body
91
- req . body = body
93
+ req . body = gsub_body ( pact , body )
92
94
else
93
- req . body = body . to_json
95
+ req . body = gsub_body ( pact , body . to_json )
94
96
end
95
97
end
96
98
97
99
execution_logger . info req . body
98
100
req
99
101
end
100
102
101
- def do_request req
103
+ def do_request uri , req
102
104
logger . info "Making webhook #{ uuid } request #{ to_s } "
103
105
Net ::HTTP . start ( uri . hostname , uri . port ,
104
106
:use_ssl => uri . scheme == 'https' ) do |http |
@@ -126,19 +128,31 @@ def to_s
126
128
"#{ method . upcase } #{ url } , username=#{ username } , password=#{ display_password } , headers=#{ headers } , body=#{ body } "
127
129
end
128
130
129
- def http_request
130
- Net ::HTTP . const_get ( method . capitalize ) . new ( url )
131
+ def http_request ( uri )
132
+ Net ::HTTP . const_get ( method . capitalize ) . new ( uri )
131
133
end
132
134
133
- def uri
134
- URI ( url )
135
+ def build_uri pact
136
+ URI ( gsub_url ( pact , url ) )
135
137
end
136
138
137
- def url_with_credentials
138
- u = URI ( url )
139
+ def url_with_credentials pact
140
+ u = build_uri ( pact )
139
141
u . userinfo = "#{ username } :#{ display_password } " if username
140
142
u
141
143
end
144
+
145
+ def gsub_body pact , body
146
+ base_url = PactBroker . configuration . base_url
147
+ body . gsub ( '${pactbroker.pactUrl}' , PactBroker ::Api ::PactBrokerUrls . pact_url ( base_url , pact ) )
148
+ end
149
+
150
+ def gsub_url pact , url
151
+ base_url = PactBroker . configuration . base_url
152
+ pact_url = PactBroker ::Api ::PactBrokerUrls . pact_url ( base_url , pact )
153
+ escaped_pact_url = CGI ::escape ( pact_url )
154
+ url . gsub ( '${pactbroker.pactUrl}' , escaped_pact_url )
155
+ end
142
156
end
143
157
end
144
158
end
0 commit comments