Class: PactBroker::Pacticipants::Repository

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

Instance Method Summary collapse

Methods included from Repositories

#branch_version_repository, #label_repository, #matrix_repository, #pact_repository, #pacticipant_repository, #tag_repository, #verification_repository, #version_repository, #webhook_repository

Methods included from Repositories::Helpers

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

Instance Method Details

#create(params) ⇒ 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.



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

def create params
  PactBroker::Domain::Pacticipant.new(
    name: params.fetch(:name),
    display_name: params[:display_name],
    repository_url: params[:repository_url],
    repository_name: params[:repository_name],
    repository_namespace: params[:repository_namespace],
    main_branch: params[:main_branch]
  ).insert_ignore
end

#delete_if_orphan(pacticipant) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/pact_broker/pacticipants/repository.rb', line 83

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



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

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



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

def find_all
  find
end

#find_all_pacticipant_versions_in_reverse_order(name, pagination_options = nil) ⇒ Object



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

def find_all_pacticipant_versions_in_reverse_order name, pagination_options = nil
  pacticipant = pacticipant_repository.find_by_name!(name)
  query = PactBroker::Domain::Version.where(pacticipant: pacticipant).reverse_order(:order)
  query = query.paginate(pagination_options[:page_number], pagination_options[:page_size]) if pagination_options
  query
end

#find_by_id(id) ⇒ Object



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

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

#find_by_name(name) ⇒ Object



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

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:



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

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



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

def find_by_name_or_create name
  pacticipant = find_by_name(name)
  pacticipant ? pacticipant : create(name: name)
end

#handle_multiple_pacticipants_found(name, pacticipants) ⇒ Object

Raises:



92
93
94
95
# File 'lib/pact_broker/pacticipants/repository.rb', line 92

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



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

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

#replace(_pacticipant_name, open_struct_pacticipant) ⇒ Object



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

def replace(_pacticipant_name, open_struct_pacticipant)
  PactBroker::Domain::Pacticipant.new(
    display_name: open_struct_pacticipant.display_name,
    repository_url: open_struct_pacticipant.repository_url,
    repository_name: open_struct_pacticipant.repository_name,
    repository_namespace: open_struct_pacticipant.repository_namespace,
    main_branch: open_struct_pacticipant.main_branch
  ).save
end

#search_by_name(pacticipant_name) ⇒ Object



97
98
99
100
101
# File 'lib/pact_broker/pacticipants/repository.rb', line 97

def search_by_name(pacticipant_name)
  terms = pacticipant_name.split.map { |v| v.gsub("_", "\\_") }
  string_match_query = Sequel.|( *terms.map { |term| Sequel.ilike(Sequel[:pacticipants][:name], "%#{term}%") })
  PactBroker::Domain::Pacticipant.where(string_match_query)
end

#set_main_branch(pacticipant, main_branch) ⇒ Object



103
104
105
# File 'lib/pact_broker/pacticipants/repository.rb', line 103

def set_main_branch(pacticipant, main_branch)
  pacticipant.update(main_branch: main_branch)
end

#update(pacticipant_name, pacticipant) ⇒ Object



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

def update(pacticipant_name, pacticipant)
  pacticipant.name = pacticipant_name
  pacticipant.save
end