Skip to content

Commit

Permalink
fix: remove deprecated method of handling branches
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Sep 21, 2021
1 parent 5e5aa49 commit 6d26487
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 416 deletions.
101 changes: 0 additions & 101 deletions doc/pacts/markdown/Pact Broker Client - Pact Broker.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@

* [A request for the index resource](#a_request_for_the_index_resource_given_the_pb:pacticipant-version_and_pb:environments_relations_exist_in_the_index_resource) given the pb:pacticipant-version and pb:environments relations exist in the index resource

* [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)
Expand All @@ -58,10 +56,6 @@

* [A request to create a pacticipant](#a_request_to_create_a_pacticipant)

* [A request to create a pacticipant version](#a_request_to_create_a_pacticipant_version_given_version_26f353580936ad3b9baddb17b00e84f33c69e7cb_of_pacticipant_Foo_does_exist) given version 26f353580936ad3b9baddb17b00e84f33c69e7cb of pacticipant Foo does exist

* [A request to create a pacticipant version](#a_request_to_create_a_pacticipant_version_given_version_26f353580936ad3b9baddb17b00e84f33c69e7cb_of_pacticipant_Foo_does_not_exist) given version 26f353580936ad3b9baddb17b00e84f33c69e7cb of pacticipant Foo does not exist

* [A request to create a webhook for a consumer and provider](#a_request_to_create_a_webhook_for_a_consumer_and_provider_given_'Condor'_does_not_exist_in_the_pact-broker) given 'Condor' does not exist in the pact-broker

* [A request to create a webhook with a JSON body and a uuid](#a_request_to_create_a_webhook_with_a_JSON_body_and_a_uuid_given_the_'Pricing_Service'_and_'Condor'_already_exist_in_the_pact-broker) given the 'Pricing Service' and 'Condor' already exist in the pact-broker
Expand Down Expand Up @@ -859,33 +853,6 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_for_the_index_resource_given_the_pb:pacticipant-version_relation_exists_in_the_index_resource"></a>
Given **the pb:pacticipant-version relation 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:pacticipant-version": {
"href": "http://localhost:1234/HAL-REL-PLACEHOLDER-INDEX-PB-PACTICIPANT-VERSION-{pacticipant}-{version}"
}
}
}
}
```
<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
Expand Down Expand Up @@ -1165,74 +1132,6 @@ Pact Broker will respond with:
}
}
```
<a name="a_request_to_create_a_pacticipant_version_given_version_26f353580936ad3b9baddb17b00e84f33c69e7cb_of_pacticipant_Foo_does_exist"></a>
Given **version 26f353580936ad3b9baddb17b00e84f33c69e7cb of pacticipant Foo does exist**, upon receiving **a request to create a pacticipant version** from Pact Broker Client, with
```json
{
"method": "put",
"path": "/HAL-REL-PLACEHOLDER-INDEX-PB-PACTICIPANT-VERSION-Foo-26f353580936ad3b9baddb17b00e84f33c69e7cb",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"branch": "main",
"buildUrl": "http://my-ci/builds/1"
}
}
```
Pact Broker will respond with:
```json
{
"status": 200,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"number": "26f353580936ad3b9baddb17b00e84f33c69e7cb",
"buildUrl": "http://my-ci/builds/1",
"_links": {
"self": {
"href": "http://localhost:1234/some-url"
}
}
}
}
```
<a name="a_request_to_create_a_pacticipant_version_given_version_26f353580936ad3b9baddb17b00e84f33c69e7cb_of_pacticipant_Foo_does_not_exist"></a>
Given **version 26f353580936ad3b9baddb17b00e84f33c69e7cb of pacticipant Foo does not exist**, upon receiving **a request to create a pacticipant version** from Pact Broker Client, with
```json
{
"method": "put",
"path": "/HAL-REL-PLACEHOLDER-INDEX-PB-PACTICIPANT-VERSION-Foo-26f353580936ad3b9baddb17b00e84f33c69e7cb",
"headers": {
"Content-Type": "application/json",
"Accept": "application/hal+json"
},
"body": {
"branch": "main",
"buildUrl": "http://my-ci/builds/1"
}
}
```
Pact Broker will respond with:
```json
{
"status": 201,
"headers": {
"Content-Type": "application/hal+json;charset=utf-8"
},
"body": {
"number": "26f353580936ad3b9baddb17b00e84f33c69e7cb",
"buildUrl": "http://my-ci/builds/1",
"_links": {
"self": {
"href": "http://localhost:1234/some-url"
}
}
}
}
```
<a name="a_request_to_create_a_webhook_for_a_consumer_and_provider_given_&#39;Condor&#39;_does_not_exist_in_the_pact-broker"></a>
Given **'Condor' does not exist in the pact-broker**, upon receiving **a request to create a webhook for a consumer and provider** from Pact Broker Client, with
```json
Expand Down
53 changes: 4 additions & 49 deletions lib/pact_broker/client/publish_pacts_the_old_way.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ def initialize pact_broker_base_url, pact_file_paths, consumer_version_params, o
@branch = consumer_version_params[:branch]
@build_url = consumer_version_params[:build_url]
@tags = consumer_version_params[:tags] ? consumer_version_params[:tags].collect{ |tag| tag.respond_to?(:strip) ? tag.strip : tag } : []
@version_required = consumer_version_params[:version_required]
@pact_broker_client_options = pact_broker_client_options
end

def call
validate
$stdout.puts("")
result = create_consumer_versions && apply_tags && publish_pacts
result = apply_tags && publish_pacts
$stdout.puts("")
if result
PactBroker::Client::CommandResult.new(true)
Expand All @@ -59,10 +58,6 @@ def index_resource
end
end

def can_create_version_with_branch?
@can_create_version_with_branch ||= index_resource.can?('pb:pacticipant-version')
end

def merge_on_server?
pact_broker_client_options[:write] == :merge
end
Expand Down Expand Up @@ -96,49 +91,6 @@ def publish_pact pact
end
end

def create_consumer_versions
if create_versions?
consumer_names.collect do | consumer_name |
create_version(index_resource, consumer_name)
end
true
else
true
end
end

def create_versions?
if version_required
if can_create_version_with_branch?
true
else
raise PactBroker::Client::Error.new("This version of the Pact Broker does not support versions with branches or build URLs. Please upgrade your broker to 2.76.2 or later.")
end
elsif (branch || build_url) && can_create_version_with_branch?
true
else
false
end
end

def create_version(index_resource, consumer_name)
Retry.while_error do
version_resource = index_resource._link('pb:pacticipant-version').expand(version: consumer_version_number, pacticipant: consumer_name).put(version_body).assert_success!
message = if version_resource.response.status == 200
"Replaced version #{consumer_version_number} of #{consumer_name}"
else
"Created version #{consumer_version_number} of #{consumer_name}"
end

message = message + " (branch #{branch})" if branch
$stdout.puts message
if version_resource.response.status == 200
$stdout.puts ::Term::ANSIColor.yellow("Replacing the version resource is not recommended under normal circumstances and may indicate that you have not configured your Pact pipeline correctly (unless you are just re-running a build for a particular commit). For more information see https://docs.pact.io/versioning")
end
true
end
end

def version_body
{
branch: branch,
Expand Down Expand Up @@ -185,6 +137,9 @@ def publish_pact_contents(pact)
end

def validate
if branch || build_url
$stdout.puts ::Term::ANSIColor.yellow("WARN: This version of the Pact Broker does not support versions with branches or build URLs. Please upgrade your broker to 2.86.0 or later.")
end
raise PactBroker::Client::Error.new("Please specify the consumer_version_number") unless (consumer_version_number && consumer_version_number.to_s.strip.size > 0)
raise PactBroker::Client::Error.new("Please specify the pact_broker_base_url") unless (pact_broker_base_url && pact_broker_base_url.to_s.strip.size > 0)
raise PactBroker::Client::Error.new("No pact files found") unless (pact_file_paths && pact_file_paths.any?)
Expand Down
77 changes: 3 additions & 74 deletions spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,80 +263,9 @@ module Client
context "when the broker does not support creation of a version with a branch but a branch is specified" do
let(:branch) { "main" }

context "when version_required is true" do
let(:version_required) { true }

it "raises an error" do
expect { subject.call }.to raise_error PactBroker::Client::Error
end
end
end

context "when the broker supports creation of a version with a branch" do
before do
allow(version_link).to receive(:expand).and_return(version_link)
allow(version_resource).to receive(:assert_success!).and_return(version_resource)
allow(version_resource).to receive_message_chain(:response, :status).and_return(version_creation_response_status)
end
let(:can_create_version) { true }
let(:version_link) { instance_double("PactBroker::Client::Hal::Link", put: version_resource) }
let(:version_resource) { instance_double("PactBroker::Client::Hal::Entity") }
let(:version_creation_response_status) { 201 }

before do
allow(index_resource).to receive(:_link).and_return(version_link)
end

context "when there is a branch, build_url or tags specified" do
let(:tags) { ["dev"] }
let(:branch) { ["main"] }
let(:build_url) { "build_url" }

it "creates a version with the branch, build_url and tags" do
expect(index_resource).to receive(:_link)
expect(version_link).to receive(:expand).with(pacticipant: "Consumer", version: "1.2.3")
expect(version_link).to receive(:put).with(branch: branch, buildUrl: build_url)
subject.call
end

context "when there is a branch but no tags" do
let(:tags) { [] }

it "does not set the tags, as this would overwrite the existing ones - not sure about this implementation" do
expect(version_link).to receive(:put).with(branch: branch, buildUrl: build_url)
subject.call
end
end

context "when the version response status is 201" do
it "puts a message indicating the version was created" do
expect($stdout).to receive(:puts).with(/Created/)
subject.call
end
end

context "when the version response status is 200" do
let(:version_creation_response_status) { 200 }

it "puts a message indicating the version was replaced" do
expect($stdout).to receive(:puts).with(/Replaced/)
subject.call
end
end
end

context "when there is no branch, tags or build_url specified" do
before do
allow(Retry).to receive(:while_error) { |&block| block.call }
end
let(:tags) { [] }
let(:branch) { nil }
let(:build_url) { nil }

it "does not create a version resource" do
expect(index_resource).to_not receive(:_link)
subject.call
end
it "logs a warning" do
expect($stdout).to receive(:puts).with(/WARN: This version/)
subject.call
end
end
end
Expand Down
Loading

0 comments on commit 6d26487

Please sign in to comment.