Class: PactBroker::Test::HttpTestDataBuilder
- Inherits:
-
Object
- Object
- PactBroker::Test::HttpTestDataBuilder
- Defined in:
- lib/pact_broker/test/http_test_data_builder.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#last_consumer_name ⇒ Object
readonly
Returns the value of attribute last_consumer_name.
-
#last_consumer_version_number ⇒ Object
readonly
Returns the value of attribute last_consumer_version_number.
-
#last_provider_name ⇒ Object
readonly
Returns the value of attribute last_provider_name.
-
#last_provider_version_branch ⇒ Object
readonly
Returns the value of attribute last_provider_version_branch.
-
#last_provider_version_number ⇒ Object
readonly
Returns the value of attribute last_provider_version_number.
-
#last_provider_version_tag ⇒ Object
readonly
Returns the value of attribute last_provider_version_tag.
Instance Method Summary collapse
- #can_i_deploy(pacticipant:, version:, to: nil, to_environment: nil) ⇒ Object
- #comment(string) ⇒ Object
- #create_environment(name:, production: false) ⇒ Object
- #create_global_webhook_for_anything_published(uuid: nil, url: "https://postman-echo.com/post") ⇒ Object
- #create_global_webhook_for_contract_changed(uuid: nil, url: "https://postman-echo.com/post", body: nil) ⇒ Object
- #create_global_webhook_for_event(**kwargs) ⇒ Object
- #create_label(name, label) ⇒ Object
- #create_pacticipant(name, main_branch: nil) ⇒ Object
- #create_tag(pacticipant:, version:, tag:) ⇒ Object
- #create_tagged_pacticipant_version(pacticipant:, version:, tag:) ⇒ Object
- #create_version(pacticipant:, version:, branch: nil) ⇒ Object
- #create_webhook_for_event(uuid: nil, url: "https://postman-echo.com/post", body: nil, provider: nil, consumer: nil, event_name:) ⇒ Object
- #delete_integration(consumer:, provider:) ⇒ Object
- #delete_pacticipant(name) ⇒ Object
- #delete_webhook(uuid:) ⇒ Object
- #deploy_to_prod(pacticipant:, version:) ⇒ Object
- #generate_content(consumer_name, provider_name, content_id) ⇒ Object
- #get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors: nil, enable_pending: nil, include_wip_pacts_since: nil) ⇒ Object
-
#initialize(pact_broker_base_url, auth = {}) ⇒ HttpTestDataBuilder
constructor
A new instance of HttpTestDataBuilder.
- #print_pacts_for_verification ⇒ Object
- #print_pacts_for_verification_response ⇒ Object
- #publish_contract(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) ⇒ Object
- #publish_pact(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) ⇒ Object
- #record_deployment(pacticipant:, version:, environment_name:) ⇒ Object
- #record_release(pacticipant:, version:, environment_name:) ⇒ Object
- #separate ⇒ Object
- #sleep(seconds = 0.5) ⇒ Object
- #verify_latest_pact_for_tag(success: true, provider: last_provider_name, consumer: last_consumer_name, consumer_version_tag:, provider_version:, provider_version_tag: nil, provider_version_branch: nil) ⇒ Object
- #verify_pact(index: 0, success: true, provider: last_provider_name, provider_version_tag: last_provider_version_tag, provider_version_branch: last_provider_version_branch, provider_version:) ⇒ Object
Constructor Details
#initialize(pact_broker_base_url, auth = {}) ⇒ HttpTestDataBuilder
Returns a new instance of HttpTestDataBuilder.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 14 def initialize(pact_broker_base_url, auth = {}) @client = Faraday.new(url: pact_broker_base_url) do |faraday| faraday.request :json faraday.response :json, :content_type => /\bjson$/ if ENV["DEBUG"] == "true" faraday.response :logger, ::Logger.new($stdout), headers: false, body: true do | logger | logger.filter(/(Authorization: ).*/,'\1[REMOVED]') end end faraday.basic_auth(auth[:username], auth[:password]) if auth[:username] faraday.headers["Authorization"] = "Bearer #{auth[:token]}" if auth[:token] faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
12 13 14 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 12 def client @client end |
#last_consumer_name ⇒ Object (readonly)
Returns the value of attribute last_consumer_name.
12 13 14 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 12 def last_consumer_name @last_consumer_name end |
#last_consumer_version_number ⇒ Object (readonly)
Returns the value of attribute last_consumer_version_number.
12 13 14 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 12 def last_consumer_version_number @last_consumer_version_number end |
#last_provider_name ⇒ Object (readonly)
Returns the value of attribute last_provider_name.
12 13 14 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 12 def last_provider_name @last_provider_name end |
#last_provider_version_branch ⇒ Object (readonly)
Returns the value of attribute last_provider_version_branch.
12 13 14 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 12 def last_provider_version_branch @last_provider_version_branch end |
#last_provider_version_number ⇒ Object (readonly)
Returns the value of attribute last_provider_version_number.
12 13 14 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 12 def last_provider_version_number @last_provider_version_number end |
#last_provider_version_tag ⇒ Object (readonly)
Returns the value of attribute last_provider_version_tag.
12 13 14 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 12 def last_provider_version_tag @last_provider_version_tag end |
Instance Method Details
#can_i_deploy(pacticipant:, version:, to: nil, to_environment: nil) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 303 def can_i_deploy(pacticipant:, version:, to: nil, to_environment: nil) can_i_deploy_response = client.get("can-i-deploy", { pacticipant: pacticipant, version: version, to: to, environment: to_environment}.compact ).tap { |response| check_for_error(response) } can = !!(can_i_deploy_response.body["summary"] || {})["deployable"] puts "can-i-deploy #{pacticipant} version #{version} to #{to || to_environment}: #{can ? 'yes' : 'no'}" summary = can_i_deploy_response.body["summary"] verification_result_urls = (can_i_deploy_response.body["matrix"] || []).collect do | row | row.dig("verificationResult", "_links", "self", "href") end.compact summary.merge!("verification_result_urls" => verification_result_urls) puts summary.to_yaml separate self end |
#comment(string) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 38 def comment string puts "**********************************************************" puts string puts "**********************************************************\n\n" self end |
#create_environment(name:, production: false) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 92 def create_environment(name:, production: false) puts "Creating environment #{name}" client.post("/environments", { name: name, displayName: name, production: production }).tap { |response| check_for_error(response) } separate self end |
#create_global_webhook_for_anything_published(uuid: nil, url: "https://postman-echo.com/post") ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 258 def create_global_webhook_for_anything_published(uuid: nil, url: "https://postman-echo.com/post") puts "Creating global webhook for contract changed event with uuid #{uuid}" uuid ||= SecureRandom.uuid request_body = { "description" => "A webhook for all consumers and providers", "events" => [{ "name" => "contract_published" },{ "name" => "provider_verification_published" }], "request" => { "method" => "POST", "url" => url, "headers" => { "Content-Type" => "application/json"}, "body" => { "eventName" => "${pactbroker.eventName}", "consumerVersionNumber" => "${pactbroker.consumerVersionNumber}", "consumerVersionTags" => "${pactbroker.consumerVersionTags}", "consumerVersionBranch" => "${pactbroker.consumerVersionBranch}", "githubVerificationStatus" => "${pactbroker.githubVerificationStatus}", "providerVersionNumber" => "${pactbroker.providerVersionNumber}", "providerVersionTags" => "${pactbroker.providerVersionTags}", "canIMerge" => "${pactbroker.providerMainBranchGithubVerificationStatus}" } } } path = "webhooks/#{uuid}" client.put(path, request_body.to_json).tap { |response| check_for_error(response) } separate self end |
#create_global_webhook_for_contract_changed(uuid: nil, url: "https://postman-echo.com/post", body: nil) ⇒ Object
254 255 256 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 254 def create_global_webhook_for_contract_changed(uuid: nil, url: "https://postman-echo.com/post", body: nil) create_global_webhook_for_event(uuid: uuid, url: url, body: body, event_name: "contract_content_changed") end |
#create_global_webhook_for_event(**kwargs) ⇒ Object
217 218 219 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 217 def create_global_webhook_for_event(**kwargs) create_webhook_for_event(**kwargs) end |
#create_label(name, label) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 106 def create_label(name, label) puts "Creating label '#{label}' for #{name}" client.put("pacticipants/#{encode(name)}/labels/#{encode(label)}", {}).tap { |response| check_for_error(response) } separate self end |
#create_pacticipant(name, main_branch: nil) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 99 def create_pacticipant(name, main_branch: nil) puts "Creating pacticipant with name #{name}" client.post("pacticipants", { name: name, mainBranch: main_branch }).tap { |response| check_for_error(response) } separate self end |
#create_tag(pacticipant:, version:, tag:) ⇒ Object
52 53 54 55 56 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 52 def create_tag(pacticipant:, version:, tag:) puts "Creating tag '#{tag}' for #{pacticipant} version #{version}" client.put("pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}/tags/#{encode(tag)}", {}).tap { |response| check_for_error(response) } self end |
#create_tagged_pacticipant_version(pacticipant:, version:, tag:) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 45 def create_tagged_pacticipant_version(pacticipant:, version:, tag:) [*tag].each do | t | create_tag(pacticipant: pacticipant, version: version, tag: t) end self end |
#create_version(pacticipant:, version:, branch: nil) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 58 def create_version(pacticipant:, version:, branch: nil) if branch client.put("pacticipants/#{encode(pacticipant)}/branches/#{encode(branch)}/versions/#{encode(version)}", {}).tap { |response| check_for_error(response) } else client.put("pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}").tap { |response| check_for_error(response) } end self end |
#create_webhook_for_event(uuid: nil, url: "https://postman-echo.com/post", body: nil, provider: nil, consumer: nil, event_name:) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 221 def create_webhook_for_event(uuid: nil, url: "https://postman-echo.com/post", body: nil, provider: nil, consumer: nil, event_name:) require "securerandom" webhook_prefix = "global " if provider.nil? && consumer.nil? puts "Creating #{webhook_prefix}webhook for contract changed event with uuid #{uuid}" uuid ||= SecureRandom.uuid default_body = { "pactUrl" => "${pactbroker.pactUrl}", "eventName" => "${pactbroker.eventName}", "consumerName" => "${pactbroker.consumerName}", "consumerVersionNumber" => "${pactbroker.consumerVersionNumber}", "providerVersionBranch" => "${pactbroker.providerVersionBranch}", "providerName" => "${pactbroker.providerName}", "providerVersionNumber" => "${pactbroker.providerVersionNumber}", "providerVersionDescriptions" => "${pactbroker.providerVersionDescriptions}", "consumerVersionBranch" => "${pactbroker.consumerVersionBranch}", } request_body = { "consumer" => consumer, "provider" => provider, "description" => webhook_description(consumer, provider), "events" => Array(event_name).map { |name| {"name" => name} }, "request" => { "method" => "POST", "url" => url, "body" => body || default_body } }.compact path = "webhooks/#{uuid}" client.put(path, request_body.to_json).tap { |response| check_for_error(response) } separate self end |
#delete_integration(consumer:, provider:) ⇒ Object
317 318 319 320 321 322 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 317 def delete_integration(consumer:, provider:) puts "Deleting all data for the integration between #{consumer} and #{provider}" client.delete("integrations/provider/#{encode(provider)}/consumer/#{encode(consumer)}").tap { |response| check_for_error(response) } separate self end |
#delete_pacticipant(name) ⇒ Object
324 325 326 327 328 329 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 324 def delete_pacticipant(name) puts "Deleting pacticipant #{name}" @publish_pact_response = client.delete("pacticipants/#{encode(name)}").tap { |response| check_for_error(response) } separate self end |
#delete_webhook(uuid:) ⇒ Object
290 291 292 293 294 295 296 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 290 def delete_webhook(uuid:) puts "Deleting webhook with uuid #{uuid}" path = "webhooks/#{uuid}" client.delete(path).tap { |response| check_for_error(response) } separate self end |
#deploy_to_prod(pacticipant:, version:) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 67 def deploy_to_prod(pacticipant:, version:) puts "Deploying #{pacticipant} version #{version} to prod" create_tag(pacticipant: pacticipant, version: version, tag: "prod") separate self end |
#generate_content(consumer_name, provider_name, content_id) ⇒ Object
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 331 def generate_content(consumer_name, provider_name, content_id) { consumer: { name: consumer_name }, provider: { name: provider_name }, interactions: [ { description: "a request", request: { method: "GET", path: "/things/#{content_id}" }, response: { status: 200 } } ] } end |
#get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors: nil, enable_pending: nil, include_wip_pacts_since: nil) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 156 def get_pacts_for_verification(provider: last_provider_name, provider_version_tag: nil, provider_version_branch: nil, consumer_version_selectors: nil, enable_pending: nil, include_wip_pacts_since: nil) @last_provider_name = provider @last_provider_version_tag = provider_version_tag @last_provder_version_branch = provider_version_branch puts "Fetching pacts for verification for #{provider}" request_body = { providerVersionTags: [*provider_version_tag], providerVersionBranch: provider_version_branch, consumerVersionSelectors: consumer_version_selectors, includePendingStatus: enable_pending, includeWipPactsSince: include_wip_pacts_since }.compact puts request_body.to_yaml puts "" @pacts_for_verification_response = client.post("pacts/provider/#{encode(provider)}/for-verification", request_body).tap { |response| check_for_error(response) } print_pacts_for_verification separate self end |
#print_pacts_for_verification ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 177 def print_pacts_for_verification pacts = @pacts_for_verification_response.body&.dig("_embedded", "pacts") if pacts puts "Pacts for verification (#{pacts.count}):" pacts.each do | pact | puts({ "url" => pact["_links"]["self"]["href"], "wip" => pact["verificationProperties"]["wip"], "pending" => pact["verificationProperties"]["pending"], "why" => pact["verificationProperties"]["notices"].select { | n | n["when"] == "before_verification" }.collect{ | n | n["text"] } }.to_yaml) end end self end |
#print_pacts_for_verification_response ⇒ Object
298 299 300 301 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 298 def print_pacts_for_verification_response puts @pacts_for_verification_response.body self end |
#publish_contract(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 113 def publish_contract(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) content = generate_content(consumer, provider, content_id) request_body_hash = { :pacticipantName => consumer, :pacticipantVersionNumber => consumer_version, :branch => branch, :tags => tag ? [tag] : nil, :contracts => [ { :consumerName => consumer, :providerName => provider, :specification => "pact", :contentType => "application/json", :content => Base64.strict_encode64(content.to_json) } ] }.compact response = client.post("contracts/publish", request_body_hash).tap { |resp| check_for_error(resp) } puts response.body["logs"].collect{ |log| log["message"]} separate self end |
#publish_pact(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 136 def publish_pact(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) @last_consumer_name = consumer @last_provider_name = provider @last_consumer_version_number = consumer_version create_version(pacticipant: consumer, version: consumer_version, branch: branch) if branch [*tag].each do | t | create_tag(pacticipant: consumer, version: consumer_version, tag: t) end puts "" if [*tag].any? content = generate_content(consumer, provider, content_id) puts "Publishing pact for consumer #{consumer} version #{consumer_version} and provider #{provider}" pact_path = "pacts/provider/#{encode(provider)}/consumer/#{encode(consumer)}/version/#{encode(consumer_version)}" @publish_pact_response = client.put(pact_path, content).tap { |response| check_for_error(response) } separate self end |
#record_deployment(pacticipant:, version:, environment_name:) ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 74 def record_deployment(pacticipant:, version:, environment_name:) puts "Recording deployment of #{pacticipant} version #{version} to #{environment_name}" version_body = client.get("/pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}").tap { |response| check_for_error(response) }.body environment_relation = version_body["_links"]["pb:record-deployment"].find { |relation| relation["name"] == environment_name } client.post(environment_relation["href"], { replacedPreviousDeployedVersion: true }).tap { |response| check_for_error(response) } separate self end |
#record_release(pacticipant:, version:, environment_name:) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 83 def record_release(pacticipant:, version:, environment_name:) puts "Recording release of #{pacticipant} version #{version} to #{environment_name}" version_body = client.get("/pacticipants/#{encode(pacticipant)}/versions/#{encode(version)}").tap { |response| check_for_error(response) }.body environment_relation = version_body["_links"]["pb:record-release"].find { |relation| relation["name"] == environment_name } client.post(environment_relation["href"], { replacedPreviousDeployedVersion: true }).tap { |response| check_for_error(response) } separate self end |
#separate ⇒ Object
34 35 36 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 34 def separate puts "\n=============================================================\n\n" end |
#sleep(seconds = 0.5) ⇒ Object
29 30 31 32 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 29 def sleep(seconds = 0.5) Kernel.sleep(seconds) self end |
#verify_latest_pact_for_tag(success: true, provider: last_provider_name, consumer: last_consumer_name, consumer_version_tag:, provider_version:, provider_version_tag: nil, provider_version_branch: nil) ⇒ Object
193 194 195 196 197 198 199 200 201 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 193 def verify_latest_pact_for_tag(success: true, provider: last_provider_name, consumer: last_consumer_name, consumer_version_tag: , provider_version:, provider_version_tag: nil, provider_version_branch: nil) @last_provider_name = provider @last_consumer_name = consumer url_of_pact_to_verify = "pacts/provider/#{encode(provider)}/consumer/#{encode(consumer)}/latest/#{encode(consumer_version_tag)}" publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, provider_version_branch, success) separate self end |
#verify_pact(index: 0, success: true, provider: last_provider_name, provider_version_tag: last_provider_version_tag, provider_version_branch: last_provider_version_branch, provider_version:) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/pact_broker/test/http_test_data_builder.rb', line 203 def verify_pact(index: 0, success: true, provider: last_provider_name, provider_version_tag: last_provider_version_tag, provider_version_branch: last_provider_version_branch, provider_version: ) @last_provider_name = provider @last_provider_version_tag = provider_version_tag @last_provider_version_branch = provider_version_branch pact_to_verify = @pacts_for_verification_response.body["_embedded"]["pacts"][index] raise "No pact found to verify at index #{index}" unless pact_to_verify url_of_pact_to_verify = pact_to_verify["_links"]["self"]["href"] publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, provider_version_branch, success) separate self end |