Skip to content

Commit

Permalink
feat: publish pacts using the "all in one" endpoint (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque authored Apr 27, 2021
1 parent 29a7962 commit 3a1ea22
Show file tree
Hide file tree
Showing 16 changed files with 720 additions and 174 deletions.
100 changes: 100 additions & 0 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:pacticipant-version_relation_exists_in_the_index_resource) given the pb:pacticipant-version relation exists in the index resource

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:publish-contracts_relations_exists_in_the_index_resource) given the pb:publish-contracts relations exists in the index resource

* [A request for the index resource with the webhook relation](#a_request_for_the_index_resource_with_the_webhook_relation)

* [A request for the list of the latest pacts from all consumers for the Pricing Service'](#a_request_for_the_list_of_the_latest_pacts_from_all_consumers_for_the_Pricing_Service'_given_a_latest_pact_between_Condor_and_the_Pricing_Service_exists) given a latest pact between Condor and the Pricing Service exists
Expand Down Expand Up @@ -92,6 +94,8 @@

* [A request to publish a pact with method put](#a_request_to_publish_a_pact_with_method_put_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker,_and_Condor_already_has_a_pact_published_for_version_1.3.0) given the 'Pricing Service' and 'Condor' already exist in the pact-broker, and Condor already has a pact published for version 1.3.0

* [A request to publish contracts](#a_request_to_publish_contracts)

* [A request to record a deployment](#a_request_to_record_a_deployment_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_a_test_environment_available_for_deployment) given version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with a test environment available for deployment

* [A request to register the repository URL of a pacticipant](#a_request_to_register_the_repository_URL_of_a_pacticipant_given_the_'Pricing_Service'_already_exists_in_the_pact-broker) given the 'Pricing Service' already exists in the pact-broker
Expand Down Expand Up @@ -832,6 +836,33 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:publish-contracts_relations_exists_in_the_index_resource"></a>
Given **the pb:publish-contracts relations exists in the index resource**, upon receiving **a request for the index resource** from Pact Broker Client, with
```json
{
"method": "GET",
"path": "/",
"headers": {
"Accept": "application/hal+json"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_links": {
"pb:publish-contracts": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-PB-PUBLISH-CONTRACTS"
}
}
}
}
```
<a name="a_request_for_the_index_resource_with_the_webhook_relation"></a>
Upon receiving **a request for the index resource with the webhook relation** from Pact Broker Client, with
```json
Expand Down Expand Up @@ -1928,6 +1959,75 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_publish_contracts"></a>
Upon receiving **a request to publish contracts** from Pact Broker Client, with
```json
{
"method": "POST",
"path": "/HAL-REL-PLACEHOLDER-PB-PUBLISH-CONTRACTS",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"pacticipantName": "Foo",
"pacticipantVersionNumber": "5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
"branch": "main",
"tags": [
"dev"
],
"buildUrl": "http://build",
"contracts": [
{
"consumerName": "Foo",
"providerName": "Bar",
"specification": "pact",
"contentType": "application/json",
"content": "eyJjb25zdW1lciI6eyJuYW1lIjoiRm9vIn0sInByb3ZpZGVyIjp7Im5hbWUiOiJCYXIifSwiaW50ZXJhY3Rpb25zIjpbeyJkZXNjcmlwdGlvbiI6ImFuIGV4YW1wbGUgcmVxdWVzdCIsInByb3ZpZGVyU3RhdGUiOiJhIHByb3ZpZGVyIHN0YXRlIiwicmVxdWVzdCI6eyJtZXRob2QiOiJHRVQiLCJwYXRoIjoiLyIsImhlYWRlcnMiOnt9fSwicmVzcG9uc2UiOnsic3RhdHVzIjoyMDAsImhlYWRlcnMiOnsiQ29udGVudC1UeXBlIjoiYXBwbGljYXRpb24vaGFsK2pzb24ifX19XSwibWV0YWRhdGEiOnsicGFjdFNwZWNpZmljYXRpb24iOnsidmVyc2lvbiI6IjIuMC4wIn19fQ==",
"writeMode": "overwrite"
}
]
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"_embedded": {
"pacticipant": {
"name": "Foo"
},
"version": {
"number": "5556b8149bf8bac76bc30f50a8a2dd4c22c85f30",
"buildUrl": "http://build"
}
},
"logs": [
{
"level": "info",
"message": "some message"
}
],
"_links": {
"pb:pacticipant-version-tags": [
{
"name": "dev"
}
],
"pb:contracts": [
{
"href": "http://some-pact"
}
]
}
}
}
```
<a name="a_request_to_record_a_deployment_given_version_5556b8149bf8bac76bc30f50a8a2dd4c22c85f30_of_pacticipant_Foo_exists_with_a_test_environment_available_for_deployment"></a>
Given **version 5556b8149bf8bac76bc30f50a8a2dd4c22c85f30 of pacticipant Foo exists with a test environment available for deployment**, upon receiving **a request to record a deployment** from Pact Broker Client, with
```json
Expand Down
2 changes: 1 addition & 1 deletion example/scripts/publish-pact.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# assumes you've set PACT_BROKER_BASE_URL, PACT_BROKER_USERNAME and PACT_BROKER_PASSWORD already

bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version=1.0.0 --tag master
bundle exec bin/pact-broker publish $(dirname "$0")/pact.json --consumer-app-version=1.0.0 --tag master --verbose
8 changes: 5 additions & 3 deletions lib/pact_broker/client/cli/broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,16 @@ def can_i_deploy(*ignored_but_necessary)
method_option :tag_with_git_branch, aliases: "-g", type: :boolean, default: false, required: false, desc: "Tag consumer version with the name of the current git branch. Default: false"
method_option :build_url, desc: "The build URL that created the pact"
method_option :merge, type: :boolean, default: false, require: false, desc: "If a pact already exists for this consumer version and provider, merge the contents. Useful when running Pact tests concurrently on different build nodes."
method_option :output, aliases: "-o", desc: "json or text", default: 'text'
shared_authentication_options

def publish(*pact_files)
require 'pact_broker/client/error'
validate_credentials
validate_pact_files(pact_files)
success = publish_pacts(pact_files)
raise PactPublicationError, "One or more pacts failed to be published" unless success
result = publish_pacts(pact_files)
$stdout.puts result.message
exit(1) unless result.success
rescue PactBroker::Client::Error => e
raise PactPublicationError, "#{e.class} - #{e.message}"
end
Expand Down Expand Up @@ -283,6 +285,7 @@ def publish_pacts pact_files
options.broker_base_url,
file_list(pact_files),
consumer_version_params,
{ merge: options[:merge], output: options.output }.compact,
pact_broker_client_options.merge(write_options)
)
end
Expand Down Expand Up @@ -393,7 +396,6 @@ def parse_webhook_options(webhook_url)
provider: options.provider,
events: events
}

end

def run_webhook_commands webhook_url
Expand Down
8 changes: 6 additions & 2 deletions lib/pact_broker/client/hal/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def create_request uri, http_method, body = nil, headers = {}
end

def perform_request request, uri
response = until_truthy_or_max_times(times: 5, sleep: 5, condition: ->(resp) { resp.code.to_i < 500 }) do
response = until_truthy_or_max_times(condition: ->(resp) { resp.code.to_i < 500 }) do
http = Net::HTTP.new(uri.host, uri.port, :ENV)
http.set_debug_output(output_stream) if verbose
http.use_ssl = (uri.scheme == 'https')
Expand All @@ -71,7 +71,7 @@ def perform_request request, uri
end

def until_truthy_or_max_times options = {}
max_tries = options.fetch(:times, 3)
max_tries = options.fetch(:times, default_max_tries)
tries = 0
sleep_interval = options.fetch(:sleep, 5)
sleep(sleep_interval) if options[:sleep_first]
Expand All @@ -97,6 +97,10 @@ def until_truthy_or_max_times options = {}
end
end

def default_max_tries
5
end

def sleep seconds
Kernel.sleep seconds
end
Expand Down
1 change: 0 additions & 1 deletion lib/pact_broker/client/pacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
module PactBroker
module Client
class Pacts < BaseClient

def publish options
consumer_version = options[:consumer_version]
pact_hash = options[:pact_hash]
Expand Down
Loading

0 comments on commit 3a1ea22

Please sign in to comment.