Class: PactBroker::Pacticipants::Repository

Inherits:
Object
  • Object
show all
Includes:
Repositories::Helpers
Defined in:
lib/pact_broker/pacticipants/repository.rb

Instance Method Summary collapse

Methods included from Repositories::Helpers

#mysql?, #name_like, #order_append_ignore_case, #order_ignore_case, #pacticipant_id_for_name, #postgres?, #select_all_qualified, #select_for_subquery

Instance Method Details

#create(args) ⇒ Object

Need to be able to handle two calls that make the pacticipant at the same time. TODO raise error if attributes apart from name are different, because this indicates that the second request is not at the same time.



56
57
58
59
60
61
62
63
64
# File 'lib/pact_broker/pacticipants/repository.rb', line 56

def create args
  PactBroker::Domain::Pacticipant.new(
    name: args[:name],
    repository_url: args[:repository_url],
    created_at: Sequel.datetime_class.now,
    updated_at: Sequel.datetime_class.now
  ).insert_ignore
  PactBroker::Domain::Pacticipant.find(name: args[:name])
end

#delete_if_orphan(pacticipant) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/pact_broker/pacticipants/repository.rb', line 70

def delete_if_orphan(pacticipant)
  if PactBroker::Domain::Version.where(pacticipant: pacticipant).empty? &&
    PactBroker::Pacts::PactPublication.where(provider: pacticipant).or(consumer: pacticipant).empty? &&
      PactBroker::Pacts::PactVersion.where(provider: pacticipant).or(consumer: pacticipant).empty? &&
      PactBroker::Webhooks::Webhook.where(provider: pacticipant).or(consumer: pacticipant).empty?
    pacticipant.destroy
  end
end

#find(options = {}) ⇒ Object



32
33
34
35
36
# File 'lib/pact_broker/pacticipants/repository.rb', line 32

def find options = {}
  query = PactBroker::Domain::Pacticipant.select_all_qualified
  query = query.label(options[:label_name]) if options[:label_name]
  query.order_ignore_case(Sequel[:pacticipants][:name]).eager(:labels).eager(:latest_version).all
end

#find_allObject



28
29
30
# File 'lib/pact_broker/pacticipants/repository.rb', line 28

def find_all
  find
end

#find_all_pacticipant_versions_in_reverse_order(name) ⇒ Object



38
39
40
41
42
43
# File 'lib/pact_broker/pacticipants/repository.rb', line 38

def find_all_pacticipant_versions_in_reverse_order name
  PactBroker::Domain::Version.select_all_qualified
    .join(:pacticipants, {id: :pacticipant_id})
    .where(name_like(:name, name))
    .reverse_order(:order)
end

#find_by_id(id) ⇒ Object



24
25
26
# File 'lib/pact_broker/pacticipants/repository.rb', line 24

def find_by_id id
  PactBroker::Domain::Pacticipant.where(id: id).single_record
end

#find_by_name(name) ⇒ Object



12
13
14
15
16
# File 'lib/pact_broker/pacticipants/repository.rb', line 12

def find_by_name name
  pacticipants = PactBroker::Domain::Pacticipant.where(name_like(:name, name)).all
  handle_multiple_pacticipants_found(name, pacticipants) if pacticipants.size > 1
  pacticipants.first
end

#find_by_name!(name) ⇒ Object

Raises:



18
19
20
21
22
# File 'lib/pact_broker/pacticipants/repository.rb', line 18

def find_by_name! name
  pacticipant = find_by_name(name)
  raise PactBroker::Error, "No pacticipant found with name '#{name}'" unless pacticipant
  pacticipant
end

#find_by_name_or_create(name) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/pact_broker/pacticipants/repository.rb', line 45

def find_by_name_or_create name
  if pacticipant = find_by_name(name)
    pacticipant
  else
    create name: name
  end
end

#handle_multiple_pacticipants_found(name, pacticipants) ⇒ Object

Raises:



79
80
81
82
# File 'lib/pact_broker/pacticipants/repository.rb', line 79

def handle_multiple_pacticipants_found(name, pacticipants)
  names = pacticipants.collect(&:name).join(", ")
  raise PactBroker::Error.new("Found multiple pacticipants with a case insensitive name match for '#{name}': #{names}. Please delete one of them, or set PactBroker.configuration.use_case_sensitive_resource_names = true")
end

#pacticipant_namesObject



66
67
68
# File 'lib/pact_broker/pacticipants/repository.rb', line 66

def pacticipant_names
  PactBroker::Domain::Pacticipant.select(:name).order(:name).collect(&:name)
end