Class: PactBroker::Deployments::DeployedVersionService

Inherits:
Object
  • Object
show all
Extended by:
Events::Publisher, Repositories::Scopes, Services
Includes:
Logging
Defined in:
lib/pact_broker/deployments/deployed_version_service.rb

Constant Summary

Constants included from Services

Services::SERVICE_FACTORIES

Class Method Summary collapse

Methods included from Repositories::Scopes

scope_for, unscoped, with_no_scope

Methods included from Services

badge_service, branch_service, certificate_service, contract_service, deployed_version_service, environment_service, get_service, group_service, index_service, integration_service, label_service, matrix_service, metrics_service, pact_service, pacticipant_service, register_default_services, register_service, released_version_service, tag_service, verification_service, version_service, webhook_service, webhook_trigger_service

Methods included from Logging

included, #log_error, #log_with_tag, #measure_info

Class Method Details

.find_by_uuid(uuid) ⇒ Object

Policy applied at resource level to Version



18
19
20
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 18

def self.find_by_uuid(uuid)
  DeployedVersion.where(uuid: uuid).single_record
end

.find_currently_deployed_version_for_version_and_environment_and_target(version, environment, target) ⇒ Object

Policy applied at resource level to Version



48
49
50
51
52
53
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 48

def self.find_currently_deployed_version_for_version_and_environment_and_target(version, environment, target)
  DeployedVersion
    .currently_deployed
    .for_version_and_environment_and_target(version, environment, target)
    .single_record
end

.find_currently_deployed_versions_for_environment(environment, pacticipant_name: nil, target: :unspecified) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 55

def self.find_currently_deployed_versions_for_environment(environment, pacticipant_name: nil, target: :unspecified)
  query = scope_for(DeployedVersion)
    .currently_deployed
    .for_environment(environment)
    .order_by_date_desc

  query = query.for_pacticipant_name(pacticipant_name) if pacticipant_name
  query = query.for_target(target) if target != :unspecified
  query.all
end

.find_currently_deployed_versions_for_pacticipant(pacticipant) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 66

def self.find_currently_deployed_versions_for_pacticipant(pacticipant)
  scope_for(DeployedVersion)
    .currently_deployed
    .where(pacticipant_id: pacticipant.id)
    .eager(:version)
    .eager(:environment)
    .order(:created_at, :id)
    .all
end

.find_deployed_versions_for_version_and_environment(version, environment) ⇒ Object



40
41
42
43
44
45
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 40

def self.find_deployed_versions_for_version_and_environment(version, environment)
  scope_for(DeployedVersion)
    .for_version_and_environment(version, environment)
    .order_by_date_desc
    .all
end

.find_or_create(uuid, version, environment, target, auto_created: false) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 22

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
    record_previous_version_undeployed(version.pacticipant, environment, target)
    deployed_version = DeployedVersion.create(
      uuid: uuid,
      version: version,
      pacticipant_id: version.pacticipant_id,
      environment: environment,
      target: target,
      auto_created: auto_created
    )
    broadcast(:deployed_version_created, { deployed_version: deployed_version })
    deployed_version
  end
end

.maybe_create_deployed_version_for_tag(version, environment_name) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 81

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, auto_created: true)
    end
  end
end

.next_uuidObject



13
14
15
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 13

def self.next_uuid
  SecureRandom.uuid
end

.record_version_undeployed(deployed_version) ⇒ Object



76
77
78
79
# File 'lib/pact_broker/deployments/deployed_version_service.rb', line 76

def self.record_version_undeployed(deployed_version)
  deployed_version.currently_deployed_version_id&.delete
  deployed_version.record_undeployed
end