Skip to content

Commit

Permalink
feat: automatically create a deployed version when a tag is created w…
Browse files Browse the repository at this point in the history
…ith the same name as an environment
  • Loading branch information
bethesque committed Jul 14, 2021
1 parent cda9c15 commit 56a583a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
14 changes: 14 additions & 0 deletions lib/pact_broker/api/resources/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/pact_broker/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/pact_broker/db/seed_example_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down
10 changes: 10 additions & 0 deletions spec/features/create_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 39 additions & 13 deletions spec/lib/pact_broker/api/resources/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -87,7 +82,6 @@ module Resources
subject
expect(last_response).to be_successful
end

end

context "when the tag does not exist" do
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
1 change: 0 additions & 1 deletion spec/support/shared_context.rb
Original file line number Diff line number Diff line change
@@ -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 }
Expand Down

0 comments on commit 56a583a

Please sign in to comment.