class PactBroker::Test::HttpTestDataBuilder

Attributes

client[R]
last_consumer_name[R]
last_consumer_version_number[R]
last_provider_name[R]
last_provider_version_branch[R]
last_provider_version_number[R]
last_provider_version_tag[R]

Public Class Methods

new(pact_broker_base_url, auth = {}) click to toggle source
# 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

Public Instance Methods

can_i_deploy(pacticipant:, version:, to: nil, to_environment: nil) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 302
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) click to toggle source
# 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) click to toggle source
# 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") click to toggle source
# 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}",
        "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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 316
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) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 323
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:) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 289
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:) click to toggle source
# 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) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 330
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) click to toggle source
# 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() click to toggle source
print_pacts_for_verification_response() click to toggle source
publish_contract(consumer: last_consumer_name, consumer_version:, provider: last_provider_name, content_id:, tag: nil, branch: nil) click to toggle source
# 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) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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() click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 34
def separate
  puts "\n=============================================================\n\n"
end
sleep(seconds = 0.5) click to toggle source
# 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) click to toggle source
# 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: ) click to toggle source
# 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

Private Instance Methods

check_for_error(response) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 391
def check_for_error(response)
  if ! response.success?
    puts response.status
    puts response.body
  end
end
encode(string) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 387
def encode string
  ERB::Util.url_encode(string)
end
publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, provider_version_branch, success) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 366
def publish_verification_results(url_of_pact_to_verify, provider, provider_version, provider_version_tag, provider_version_branch, success)
  [*provider_version_tag].each do | tag |
    create_tag(pacticipant: provider, version: provider_version, tag: tag)
  end
  puts "" if [*provider_version_tag].any?

  create_version(pacticipant: provider, version: provider_version, branch: provider_version_branch) if provider_version_branch

  pact_response = client.get(url_of_pact_to_verify).tap { |response| check_for_error(response) }
  verification_results_url = pact_response.body["_links"]["pb:publish-verification-results"]["href"]

  results = {
    success: success,
    testResults: [],
    providerApplicationVersion: provider_version
  }
  puts "Publishing verification"
  puts results.to_yaml
  client.post(verification_results_url, results.to_json).tap { |response| check_for_error(response) }
end
webhook_description(consumer, provider) click to toggle source
# File lib/pact_broker/test/http_test_data_builder.rb, line 355
def webhook_description(consumer, provider)
  return "A webhook for all consumers and providers" if consumer.nil? && provider.nil?

  suffix = {consumer: consumer, provider: provider}.compact.map do |name, pacticipant|
    desc = pacticipant.compact.map { |k, v| "#{k}: '#{v}'"}.first
    "#{name}s by #{desc}"
  end

  "A webhook for #{suffix.join(' and ')}"
end