Skip to content

Commit

Permalink
chore: track whether a branch version or deployed version was auto cr…
Browse files Browse the repository at this point in the history
…eated from a tag or not
  • Loading branch information
bethesque committed Sep 7, 2021
1 parent 3b97d27 commit e41c35d
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 10 deletions.
24 changes: 24 additions & 0 deletions db/migrations/20210908_add_auto_created.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/pact_broker/deployments/deployed_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions lib/pact_broker/deployments/deployed_version_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/pact_broker/domain/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion lib/pact_broker/metrics/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: {
Expand Down
4 changes: 2 additions & 2 deletions lib/pact_broker/versions/branch_version_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/pact_broker/versions/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion spec/features/create_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion spec/lib/pact_broker/metrics/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/pact_broker/versions/branch_version_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e41c35d

Please sign in to comment.