diff --git a/db/migrations/20210908_add_auto_created.rb b/db/migrations/20210908_add_auto_created.rb new file mode 100644 index 000000000..83ff742bf --- /dev/null +++ b/db/migrations/20210908_add_auto_created.rb @@ -0,0 +1,24 @@ +Sequel.migration do + up do + alter_table(:branch_versions) do + add_column(:auto_created, TrueClass, default: false) + end + + alter_table(:deployed_versions) do + add_column(:auto_created, TrueClass, default: false) + end + + from(:branch_versions).update(auto_created: true) + from(:deployed_versions).update(auto_created: true) + end + + down do + alter_table(:branch_versions) do + drop_column(:auto_created) + end + + alter_table(:deployed_versions) do + drop_column(:auto_created) + end + end +end diff --git a/lib/pact_broker/deployments/deployed_version.rb b/lib/pact_broker/deployments/deployed_version.rb index 2e246bd84..32d130c4e 100644 --- a/lib/pact_broker/deployments/deployed_version.rb +++ b/lib/pact_broker/deployments/deployed_version.rb @@ -15,6 +15,10 @@ class DeployedVersion < Sequel::Model dataset_module do include PactBroker::Repositories::Helpers + def user_created + where(auto_created: false) + end + def last_deployed_version(pacticipant, environment) currently_deployed .where(pacticipant_id: pacticipant.id) diff --git a/lib/pact_broker/deployments/deployed_version_service.rb b/lib/pact_broker/deployments/deployed_version_service.rb index 8a088e30d..8879baf58 100644 --- a/lib/pact_broker/deployments/deployed_version_service.rb +++ b/lib/pact_broker/deployments/deployed_version_service.rb @@ -17,7 +17,7 @@ def self.find_by_uuid(uuid) DeployedVersion.where(uuid: uuid).single_record end - def self.find_or_create(uuid, version, environment, target) + def self.find_or_create(uuid, version, environment, target, auto_created: false) if (deployed_version = find_currently_deployed_version_for_version_and_environment_and_target(version, environment, target)) deployed_version else @@ -27,7 +27,8 @@ def self.find_or_create(uuid, version, environment, target) version: version, pacticipant_id: version.pacticipant_id, environment: environment, - target: target + target: target, + auto_created: auto_created ) end end @@ -76,7 +77,7 @@ def self.maybe_create_deployed_version_for_tag(version, environment_name) if PactBroker.configuration.create_deployed_versions_for_tags if (environment = environment_service.find_by_name(environment_name)) logger.info("Creating deployed version for #{version.pacticipant.name} version #{version.number} in environment #{environment_name} (because create_deployed_versions_for_tags=true)") - find_or_create(next_uuid, version, environment, nil) + find_or_create(next_uuid, version, environment, nil, auto_created: true) end end end diff --git a/lib/pact_broker/domain/version.rb b/lib/pact_broker/domain/version.rb index c96ec50b5..d0cd51801 100644 --- a/lib/pact_broker/domain/version.rb +++ b/lib/pact_broker/domain/version.rb @@ -50,10 +50,14 @@ class Version < Sequel::Model(Sequel::Model.db[:versions].select(*VERSION_COLUMN dataset_module do include PactBroker::Repositories::Helpers - def with_branch_set + def with_branch where(id: PactBroker::Versions::BranchVersion.select(:version_id)) end + def with_user_created_branch + where(id: PactBroker::Versions::BranchVersion.select(:version_id).where(auto_created: false)) + end + def latest_version_for_pacticipant(pacticipant) where(pacticipant: pacticipant) .order(Sequel.desc(:order)) diff --git a/lib/pact_broker/metrics/service.rb b/lib/pact_broker/metrics/service.rb index 02e79f2ac..ee51603dc 100644 --- a/lib/pact_broker/metrics/service.rb +++ b/lib/pact_broker/metrics/service.rb @@ -44,7 +44,9 @@ def metrics }, pacticipantVersions: { count: PactBroker::Domain::Version.count, - withBranchSetCount: PactBroker::Domain::Version.with_branch_set.count + withUserCreatedBranchCount: PactBroker::Domain::Version.with_user_created_branch.count, + withBranchCount: PactBroker::Domain::Version.with_branch.count, + withBranchSetCount: PactBroker::Domain::Version.with_branch.count # todo remove when checked it's not used }, webhooks: { count: PactBroker::Webhooks::Webhook.count @@ -68,6 +70,7 @@ def metrics }, deployedVersions: { count: PactBroker::Deployments::DeployedVersion.count, + userCreatedCount: PactBroker::Deployments::DeployedVersion.user_created.count, currentlyDeployedCount: PactBroker::Deployments::DeployedVersion.currently_deployed.count }, releasedVersions: { diff --git a/lib/pact_broker/versions/branch_version_repository.rb b/lib/pact_broker/versions/branch_version_repository.rb index a0c7a3a47..abcca842d 100644 --- a/lib/pact_broker/versions/branch_version_repository.rb +++ b/lib/pact_broker/versions/branch_version_repository.rb @@ -3,13 +3,13 @@ module Versions class BranchVersionRepository include PactBroker::Services - def add_branch(version, branch_name) + def add_branch(version, branch_name, auto_created: false) branch = find_or_create_branch(version.pacticipant, branch_name) branch_version = version.branch_version_for_branch(branch) if branch_version branch_version.update(updated_at: Sequel.datetime_class.now) else - branch_version = PactBroker::Versions::BranchVersion.new(version: version, branch: branch).insert_ignore + branch_version = PactBroker::Versions::BranchVersion.new(version: version, branch: branch, auto_created: auto_created).insert_ignore PactBroker::Versions::BranchHead.new(branch: branch, branch_version: branch_version).upsert end pacticipant_service.maybe_set_main_branch(version.pacticipant, branch_name) diff --git a/lib/pact_broker/versions/service.rb b/lib/pact_broker/versions/service.rb index 3b9ed3862..77bb7a2bd 100644 --- a/lib/pact_broker/versions/service.rb +++ b/lib/pact_broker/versions/service.rb @@ -49,7 +49,7 @@ def self.delete version def self.maybe_set_version_branch_from_tag(version, tag_name) if use_tag_as_branch?(version) && version.branch_versions.empty? logger.info "Adding #{version.pacticipant.name} version #{version.number} to branch '#{tag_name}' (from first tag, because use_first_tag_as_branch=true)" - branch_version_repository.add_branch(version, tag_name) + branch_version_repository.add_branch(version, tag_name, auto_created: true) end end diff --git a/spec/features/create_tag_spec.rb b/spec/features/create_tag_spec.rb index 41ae4b302..84019df6e 100644 --- a/spec/features/create_tag_spec.rb +++ b/spec/features/create_tag_spec.rb @@ -36,7 +36,7 @@ end it "creates a deployed version" do - expect { subject }.to change { PactBroker::Deployments::DeployedVersion.count }.by(1) + expect { subject }.to change { PactBroker::Deployments::DeployedVersion.where(auto_created: true).count }.by(1) end end end diff --git a/spec/lib/pact_broker/metrics/service_spec.rb b/spec/lib/pact_broker/metrics/service_spec.rb index de883a089..01cbe1bfe 100644 --- a/spec/lib/pact_broker/metrics/service_spec.rb +++ b/spec/lib/pact_broker/metrics/service_spec.rb @@ -24,7 +24,8 @@ module Service end its([:pacticipantVersions, :count]) { is_expected.to eq 2 } - its([:pacticipantVersions, :withBranchSetCount]) { is_expected.to eq 1 } + its([:pacticipantVersions, :withBranchCount]) { is_expected.to eq 1 } + its([:pacticipantVersions, :withUserCreatedBranchCount]) { is_expected.to eq 1 } end describe "environments, deployed versions, released versions" do @@ -46,6 +47,7 @@ module Service its([:environments, :count]) { is_expected.to eq 1 } its([:deployedVersions, :count]) { is_expected.to eq 2 } its([:deployedVersions, :currentlyDeployedCount]) { is_expected.to eq 1 } + its([:deployedVersions, :userCreatedCount]) { is_expected.to eq 2 } its([:releasedVersions, :count]) { is_expected.to eq 3 } its([:releasedVersions, :currentlySupportedCount]) { is_expected.to eq 2 } end diff --git a/spec/lib/pact_broker/versions/branch_version_repository_spec.rb b/spec/lib/pact_broker/versions/branch_version_repository_spec.rb index aa476ce6a..c0f84052b 100644 --- a/spec/lib/pact_broker/versions/branch_version_repository_spec.rb +++ b/spec/lib/pact_broker/versions/branch_version_repository_spec.rb @@ -21,6 +21,11 @@ module Versions end context "when the branch does not already exist" do + it "default auto_created to false" do + subject + expect(PactBroker::Versions::BranchVersion.last.auto_created).to be false + end + it "creates a branch" do expect { subject }.to change { PactBroker::Versions::Branch.count }.by(1) end @@ -38,6 +43,15 @@ module Versions branch_head = subject.pacticipant.branch_head_for("new-branch") expect(branch_head.version.id).to eq subject.refresh.id end + + context "when auto_created is true" do + subject { repository.add_branch(version, new_branch_name, auto_created: true) } + + it "default auto_created to false" do + subject + expect(PactBroker::Versions::BranchVersion.last.auto_created).to be true + end + end end context "when the branch and branch version do already exist" do