Class: PactBroker::Pacticipants::Repository

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

Constant Summary

Constants included from Repositories

Repositories::REPOSITORY_FACTORIES

Instance Method Summary collapse

Methods included from Repositories::Scopes

#scope_for, #unscoped, #with_no_scope

Methods included from Repositories

#branch_repository, #branch_version_repository, #get_repository, #integration_repository, #label_repository, #matrix_repository, #pact_repository, #pacticipant_repository, #register_default_repositories, #register_repository, #tag_repository, #verification_repository, #version_repository, #webhook_repository

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.



60
61
62
63
64
65
66
67
68
69
# File 'lib/pact_broker/pacticipants/repository.rb', line 60

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.refresh
end

#delete(pacticipant) ⇒ Object



87
88
89
# File 'lib/pact_broker/pacticipants/repository.rb', line 87

def delete(pacticipant)
  pacticipant.destroy
end

#delete_if_orphan(pacticipant) ⇒ Object



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

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 = {}, pagination_options = {}, eager_load_associations = []) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/pact_broker/pacticipants/repository.rb', line 40

def find(options = {}, pagination_options = {}, eager_load_associations = [])
  query = scope_for(PactBroker::Domain::Pacticipant)
  return [] if query.empty?

  query = query.select_all_qualified
  query = query.filter(Sequel[:pacticipants][:name], options[:query_string]) if options[:query_string]
  query = query.label(options[:label_name]) if options[:label_name]
  query.order_ignore_case(Sequel[:pacticipants][:name]).eager(*eager_load_associations).all_with_pagination_options(pagination_options)
end

#find_all(options = {}, pagination_options = {}, eager_load_associations = []) ⇒ Object



36
37
38
# File 'lib/pact_broker/pacticipants/repository.rb', line 36

def find_all(options = {}, pagination_options = {}, eager_load_associations = [])
  find(options, pagination_options, eager_load_associations)
end

#find_by_id(id) ⇒ Object



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

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(Sequel.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

Raises:



50
51
52
53
54
55
# File 'lib/pact_broker/pacticipants/repository.rb', line 50

def find_by_name_or_create name
  raise PactBroker::Error, "Cannot create a pacticipant with a blank name" if name.to_s.strip.empty?
  
  pacticipant = find_by_name(name)
  pacticipant ? pacticipant : create(name: name)
end

#find_by_names(names) ⇒ Object

Parameters:

  • the (Array<String>)

    array of names by which to find the pacticipants



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

def find_by_names(names)
  return [] if names.empty?
  name_likes = names.collect{ | name | Sequel.name_like(:name, name) }
  scope_for(PactBroker::Domain::Pacticipant).where(Sequel.|(*name_likes)).all
end

#handle_multiple_pacticipants_found(name, pacticipants) ⇒ Object

Raises:



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

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



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

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

#replace(pacticipant_name, open_struct_pacticipant) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/pact_broker/pacticipants/repository.rb', line 76

def replace(pacticipant_name, open_struct_pacticipant)
  PactBroker::Domain::Pacticipant.new(
    name: pacticipant_name,
    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
  ).upsert
end

#search_by_name(pacticipant_name) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pact_broker/pacticipants/repository.rb', line 109

def search_by_name(pacticipant_name)
  terms = pacticipant_name.split.map { |v| PactBroker::Dataset::Helpers.escape_wildcards(v) }
  columns = [:name, :display_name]
  string_match_query = Sequel.|(
    *terms.map do |term|
      Sequel.|(
        *columns.map do |column|
          Sequel.ilike(Sequel[:pacticipants][column], "%#{term}%")
        end
      )
    end
  )
  scope_for(PactBroker::Domain::Pacticipant).where(string_match_query)
end

#set_main_branch(pacticipant, main_branch) ⇒ Object



124
125
126
# File 'lib/pact_broker/pacticipants/repository.rb', line 124

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

#update(pacticipant_name, pacticipant) ⇒ Object



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

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