diff --git a/doc/pacts/markdown/Pact Broker Client - Pact Broker.md b/doc/pacts/markdown/Pact Broker Client - Pact Broker.md index 51e41503..7723e71f 100644 --- a/doc/pacts/markdown/Pact Broker Client - Pact Broker.md +++ b/doc/pacts/markdown/Pact Broker Client - Pact Broker.md @@ -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) @@ -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 @@ -859,33 +853,6 @@ Pact Broker will respond with: } } ``` - -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}" - } - } - } -} -``` 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 @@ -1165,74 +1132,6 @@ Pact Broker will respond with: } } ``` - -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" - } - } - } -} -``` - -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" - } - } - } -} -``` 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 diff --git a/lib/pact_broker/client/publish_pacts_the_old_way.rb b/lib/pact_broker/client/publish_pacts_the_old_way.rb index d3228872..07ef9ec8 100644 --- a/lib/pact_broker/client/publish_pacts_the_old_way.rb +++ b/lib/pact_broker/client/publish_pacts_the_old_way.rb @@ -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) @@ -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 @@ -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, @@ -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?) diff --git a/spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb b/spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb index b8444a0d..a997b269 100644 --- a/spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb +++ b/spec/lib/pact_broker/client/publish_pacts_the_old_way_spec.rb @@ -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 diff --git a/spec/pacts/pact_broker_client-pact_broker.json b/spec/pacts/pact_broker_client-pact_broker.json index a239f38f..a0a81878 100644 --- a/spec/pacts/pact_broker_client-pact_broker.json +++ b/spec/pacts/pact_broker_client-pact_broker.json @@ -186,110 +186,6 @@ } } }, - { - "description": "a request for the index resource", - "providerState": "the pb:pacticipant-version relation exists in the index resource", - "request": { - "method": "get", - "path": "/", - "headers": { - "Accept": "application/hal+json" - } - }, - "response": { - "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}" - } - } - }, - "matchingRules": { - "$.body._links.pb:pacticipant-version.href": { - "match": "regex", - "regex": "http:\\/\\/.*{pacticipant}.*{version}" - } - } - } - }, - { - "description": "a request to create a pacticipant version", - "providerState": "version 26f353580936ad3b9baddb17b00e84f33c69e7cb of pacticipant Foo does not exist", - "request": { - "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" - } - }, - "response": { - "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" - } - } - }, - "matchingRules": { - "$.body._links.self.href": { - "match": "regex", - "regex": "http:\\/\\/.*" - } - } - } - }, - { - "description": "a request to create a pacticipant version", - "providerState": "version 26f353580936ad3b9baddb17b00e84f33c69e7cb of pacticipant Foo does exist", - "request": { - "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" - } - }, - "response": { - "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" - } - } - }, - "matchingRules": { - "$.body._links.self.href": { - "match": "regex", - "regex": "http:\\/\\/.*" - } - } - } - }, { "description": "a request to determine if Bar can be deployed with all Foo tagged prod, ignoring the verification for Foo version 3.4.5", "providerState": "provider Bar version 4.5.6 has a successful verification for Foo version 1.2.3 tagged prod and a failed verification for version 3.4.5 tagged prod", diff --git a/spec/service_providers/pact_broker_client_create_version_spec.rb b/spec/service_providers/pact_broker_client_create_version_spec.rb deleted file mode 100644 index f281674a..00000000 --- a/spec/service_providers/pact_broker_client_create_version_spec.rb +++ /dev/null @@ -1,88 +0,0 @@ -require_relative 'pact_helper' -require 'pact_broker/client' -require 'pact_broker/client/publish_pacts_the_old_way' - -describe PactBroker::Client::Versions, pact: true do - - include_context "pact broker" - - - describe "creating a pacticipant version" do - before do - allow(publish_pacts_the_old_way).to receive(:consumer_names).and_return(["Foo"]) - allow($stdout).to receive(:puts) - end - let(:version_path) { "/HAL-REL-PLACEHOLDER-INDEX-PB-PACTICIPANT-VERSION-{pacticipant}-{version}" } - let(:version_url) { pact_broker.mock_service_base_url + version_path } - let(:number) { "26f353580936ad3b9baddb17b00e84f33c69e7cb" } - let(:branch) { "main" } - let(:build_url) { "http://my-ci/builds/1" } - let(:consumer_version_params) { { number: number, branch: branch, build_url: build_url } } - let(:publish_pacts_the_old_way) { PactBroker::Client::PublishPactsTheOldWay.new(pact_broker.mock_service_base_url, ["some-pact.json"], consumer_version_params, {}) } - let(:provider_state) { "version #{number} of pacticipant Foo does not exist" } - let(:expected_response_status) { 201 } - - subject { publish_pacts_the_old_way.send(:create_consumer_versions) } - - before do - pact_broker - .given("the pb:pacticipant-version relation exists in the index resource") - .upon_receiving("a request for the index resource") - .with( - method: :get, - path: '/', - headers: get_request_headers). - will_respond_with( - status: 200, - headers: pact_broker_response_headers, - body: { - _links: { - :'pb:pacticipant-version' => { - href: Pact.term(version_url, /http:\/\/.*{pacticipant}.*{version}/) - } - } - } - ) - - pact_broker - .given(provider_state) - .upon_receiving("a request to create a pacticipant version") - .with( - method: :put, - path: "/HAL-REL-PLACEHOLDER-INDEX-PB-PACTICIPANT-VERSION-Foo-#{number}", - headers: put_request_headers, - body: { - branch: branch, - buildUrl: build_url - }). - will_respond_with( - status: expected_response_status, - headers: pact_broker_response_headers, - body: { - number: number, - buildUrl: build_url, - _links: { - self: { - href: Pact.term('http://localhost:1234/some-url', %r{http://.*}) - } - } - } - ) - end - - context "when the version does not already exist" do - it "returns true" do - expect(subject).to be true - end - end - - context "when the version does exist" do - let(:provider_state) { "version #{number} of pacticipant Foo does exist" } - let(:expected_response_status) { 200 } - - it "returns true" do - expect(subject).to be true - end - end - end -end