diff --git a/lib/pact_broker/api/resources/tag.rb b/lib/pact_broker/api/resources/tag.rb index 0baa67e64..5be0f1679 100644 --- a/lib/pact_broker/api/resources/tag.rb +++ b/lib/pact_broker/api/resources/tag.rb @@ -23,6 +23,7 @@ def from_json # Make it return a 201 by setting the Location header response.headers["Location"] = tag_url(base_url, tag) end + create_deployed_version response.body = to_json end @@ -46,6 +47,19 @@ def delete_resource def policy_name :'tags::tag' end + + def create_deployed_version + if create_deployed_versions_for_tags? + if (environment = environment_service.find_by_name(identifier_from_path[:tag_name])) + deployed_version_service.create(deployed_version_service.next_uuid, tag.version, environment, nil) + end + end + end + + # Come up with a cleaner way to abstract this for PF so it can be configured per tenant + def create_deployed_versions_for_tags? + PactBroker.configuration.create_deployed_versions_for_tags + end end end end diff --git a/lib/pact_broker/configuration.rb b/lib/pact_broker/configuration.rb index 0c9b3a9f9..6341cc283 100644 --- a/lib/pact_broker/configuration.rb +++ b/lib/pact_broker/configuration.rb @@ -41,7 +41,8 @@ class Configuration :allow_missing_migration_files, :auto_migrate_db_data, :use_rack_protection, - :metrics_sql_statement_timeout + :metrics_sql_statement_timeout, + :create_deployed_versions_for_tags ] attr_accessor :base_url, :log_dir, :database_connection, :auto_migrate_db, :auto_migrate_db_data, :allow_missing_migration_files, :example_data_seeder, :seed_example_data, :use_hal_browser, :html_pact_renderer, :use_rack_protection @@ -61,6 +62,7 @@ class Configuration attr_reader :custom_logger attr_accessor :policy_builder, :policy_scope_builder, :base_resource_class_factory attr_accessor :metrics_sql_statement_timeout + attr_accessor :create_deployed_versions_for_tags alias_method :policy_finder=, :policy_builder= alias_method :policy_scope_finder=, :policy_scope_builder= @@ -133,6 +135,7 @@ def self.default_configuration } config.warning_error_class_names = ["Sequel::ForeignKeyConstraintViolation", "PG::QueryCanceled"] config.metrics_sql_statement_timeout = 30 + config.create_deployed_versions_for_tags = true config end # rubocop: enable Metrics/MethodLength diff --git a/lib/pact_broker/db/seed_example_data.rb b/lib/pact_broker/db/seed_example_data.rb index 34f3776ea..e6efc9641 100644 --- a/lib/pact_broker/db/seed_example_data.rb +++ b/lib/pact_broker/db/seed_example_data.rb @@ -11,6 +11,7 @@ def self.call new.call end + # rubocop: disable Metrics/MethodLength def call(consumer_name: CONSUMER_NAME, provider_name: PROVIDER_NAME) return unless database_empty? PactBroker::Test::TestDataBuilder.new @@ -45,6 +46,7 @@ def call(consumer_name: CONSUMER_NAME, provider_name: PROVIDER_NAME) .create_deployed_version_for_consumer_version(environment_name: "test", created_at: days_ago(0.5)) .republish_same_pact(created_at: days_ago(0.5)) end + # rubocop: enable Metrics/MethodLength def database_empty? PactBroker::Pacticipants::Service.find_all_pacticipants.empty? diff --git a/spec/features/create_tag_spec.rb b/spec/features/create_tag_spec.rb index 72dabfa93..41ae4b302 100644 --- a/spec/features/create_tag_spec.rb +++ b/spec/features/create_tag_spec.rb @@ -29,4 +29,14 @@ expect(subject.status).to be 200 end end + + context "when there is an envionment with a matching name" do + before do + td.create_environment("foo") + end + + it "creates a deployed version" do + expect { subject }.to change { PactBroker::Deployments::DeployedVersion.count }.by(1) + end + end end diff --git a/spec/lib/pact_broker/api/resources/tag_spec.rb b/spec/lib/pact_broker/api/resources/tag_spec.rb index bc5990a76..c8ff18780 100644 --- a/spec/lib/pact_broker/api/resources/tag_spec.rb +++ b/spec/lib/pact_broker/api/resources/tag_spec.rb @@ -1,13 +1,9 @@ -require "spec_helper" require "pact_broker/api/resources/tag" module PactBroker module Api - module Resources - describe Tag do - let(:tag) { double("PactBroker::Domain::Tag") } let(:tag_decorator) { instance_double("PactBroker::Api::Decorators::TagDecorator", :to_json => tag_json) } let(:tag_json) { {"some" => "tag"}.to_json } @@ -40,7 +36,6 @@ module Resources end context "when the tag doesn't exist" do - let(:tag) { nil } it "returns a 404 Not Found" do @@ -87,7 +82,6 @@ module Resources subject expect(last_response).to be_successful end - end context "when the tag does not exist" do @@ -101,15 +95,16 @@ module Resources end describe "PUT" do - - let(:tag_url) { "http://example.org/tag/url"} - before do allow_any_instance_of(PactBroker::Api::Resources::Tag).to receive(:tag_url).and_return(tag_url) allow(Tags::Service).to receive(:find).and_return(tag) allow(PactBroker::Api::Decorators::TagDecorator).to receive(:new).and_return(tag_decorator) + allow_any_instance_of(described_class).to receive(:create_deployed_versions_for_tags?).and_return(create_deployed_versions_for_tags) end + let(:tag_url) { "http://example.org/tag/url"} + let(:create_deployed_versions_for_tags) { false } + subject { put("/pacticipants/Condor/versions/1.3.0/tags/prod", nil, "CONTENT_LENGTH" => "0", "CONTENT_TYPE" => "application/json") } it "returns a success response" do @@ -118,7 +113,6 @@ module Resources end context "when the tag already exists" do - it "returns a 200" do subject expect(last_response.status).to be 200 @@ -153,13 +147,45 @@ module Resources subject expect(last_response.body).to eq tag_json end - end - end + context "when create_deployed_versions_for_tags is true" do + before do + allow_any_instance_of(described_class).to receive(:environment_service).and_return(environment_service) + allow_any_instance_of(described_class).to receive(:deployed_version_service).and_return(deployed_version_service) + allow(environment_service).to receive(:find_by_name).and_return(environment) + allow(deployed_version_service).to receive(:next_uuid).and_return("uuid") + allow(tag).to receive(:version).and_return(version) + end + let(:environment_service) { class_double("PactBroker::Environments::Service").as_stubbed_const } + let(:deployed_version_service) { class_double("PactBroker::Deployments::DeployedVersionService").as_stubbed_const } + let(:environment) { nil } + let(:create_deployed_versions_for_tags) { true } + let(:version) { double("version") } + + it "looks for a matching environment" do + expect(environment_service).to receive(:find_by_name).with("prod") + subject + end - end + context "when the tag name matches an existing environment" do + let(:environment) { double("environment") } + it "creates a deployed version" do + expect(deployed_version_service).to receive(:create).with(anything, version, environment, nil) + subject + end + end + + context "when the tag name does not match an existing environment" do + it "does not create a deployed version" do + expect_any_instance_of(described_class).to_not receive(:deployed_version_service) + subject + end + end + end + end + end end end end diff --git a/spec/support/shared_context.rb b/spec/support/shared_context.rb index 7a6155247..72ce561e4 100644 --- a/spec/support/shared_context.rb +++ b/spec/support/shared_context.rb @@ -1,7 +1,6 @@ require "pact_broker/test/test_data_builder" RSpec.shared_context "stubbed services" do - let(:pact_service) { class_double("PactBroker::Pacts::Service").as_stubbed_const } let(:pacticipant_service) { class_double("PactBroker::Pacticipants::Service").as_stubbed_const } let(:version_service) { class_double("PactBroker::Versions::Service").as_stubbed_const }