Class: PactBroker::Versions::Repository

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

Constant Summary

Constants included from Repositories

Repositories::REPOSITORY_FACTORIES

Instance Method Summary collapse

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

Methods included from Logging

included, #log_error, #log_with_tag, #measure_info

Instance Method Details

#create(args) ⇒ Object

There may be a race condition if two simultaneous requests come in to create the same version



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pact_broker/versions/repository.rb', line 74

def create(args)
  version_params = {
    number: args[:number],
    pacticipant_id: args[:pacticipant_id],
    created_at: Sequel.datetime_class.now,
    updated_at: Sequel.datetime_class.now,
    build_url: args[:build_url]
  }.compact


  version = PactBroker::Domain::Version.new(version_params).upsert
  # branch can't be set from CRUD on the version resource, but it's convenient to be able
  # to make a version with a branch for internal code.
  branch_version_repository.add_branch(version, args[:branch]) if args[:branch]
  version
end

#create_or_overwrite(pacticipant, version_number, open_struct_version) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/pact_broker/versions/repository.rb', line 114

def create_or_overwrite(pacticipant, version_number, open_struct_version)
  saved_version = PactBroker::Domain::Version.new(
    number: version_number,
    pacticipant: pacticipant,
    build_url: open_struct_version.build_url
  ).upsert

  if open_struct_version.tags
    replace_tags(saved_version, open_struct_version.tags)
  end

  saved_version
end

#create_or_update(pacticipant, version_number, open_struct_version) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pact_broker/versions/repository.rb', line 91

def create_or_update(pacticipant, version_number, open_struct_version)
  saved_version = PactBroker::Domain::Version.where(pacticipant_id: pacticipant.id, number: version_number).single_record
  params = open_struct_version.to_h
  tags = params.delete(:tags)
  branch_name = params.delete(:branch)
  if saved_version
    saved_version.update(params)
  else
    # Upsert is only for race conditions
    # Upsert blanks out any fields that are not provided
    saved_version = PactBroker::Domain::Version.new(
      params.merge(
        pacticipant_id: pacticipant.id,
        number: version_number
      ).compact
    ).upsert
  end

  branch_version_repository.add_branch(saved_version, branch_name) if branch_name
  replace_tags(saved_version, tags) if tags
  saved_version
end

#delete_by_id(version_ids) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
# File 'lib/pact_broker/versions/repository.rb', line 142

def delete_by_id version_ids
  branches = Versions::Branch.where(id: Versions::BranchHead.select(:branch_id).where(version_id: version_ids)).all # these will be deleted
  Domain::Version.where(id: version_ids).delete
  branches.each do | branch |
    new_head_branch_version = Versions::BranchVersion.find_latest_for_branch(branch)
    if new_head_branch_version
      PactBroker::Versions::BranchHead.new(branch: branch, branch_version: new_head_branch_version).upsert
    end
  end
  nil
end

#delete_orphan_versions(consumer, provider) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/pact_broker/versions/repository.rb', line 154

def delete_orphan_versions consumer, provider
  version_ids_with_pact_publications = PactBroker::Pacts::PactPublication.where(consumer_id: [consumer.id, provider.id]).select(:consumer_version_id).collect{|r| r[:consumer_version_id]}
  version_ids_with_verifications = PactBroker::Domain::Verification.where(provider_id: [provider.id, consumer.id]).select(:provider_version_id).collect{|r| r[:provider_version_id]}
  # Hope we don't hit max parameter constraints here...
  PactBroker::Domain::Version
    .where(Sequel[:versions][:pacticipant_id] => [consumer.id, provider.id])
    .exclude(id: (version_ids_with_pact_publications + version_ids_with_verifications).uniq)
    .delete
end

#find_by_ids_in_reverse_order(version_ids, pagination_options = {}, eager_load_associations = []) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/pact_broker/versions/repository.rb', line 178

def find_by_ids_in_reverse_order version_ids, pagination_options = {}, eager_load_associations =[]
  query = PactBroker::Domain::Version
            .where(id: version_ids)
            .eager(*eager_load_associations)
            .reverse_order(:order)

  query.all_with_pagination_options(pagination_options)
end

#find_by_pacticipant_id_and_number(pacticipant_id, number) ⇒ Object



15
16
17
# File 'lib/pact_broker/versions/repository.rb', line 15

def find_by_pacticipant_id_and_number pacticipant_id, number
  PactBroker::Domain::Version.where(number: number, pacticipant_id: pacticipant_id).single_record
end

#find_by_pacticipant_id_and_number_or_create(pacticipant_id, number) ⇒ Object



136
137
138
139
140
# File 'lib/pact_broker/versions/repository.rb', line 136

def find_by_pacticipant_id_and_number_or_create pacticipant_id, number
  version = find_by_pacticipant_id_and_number(pacticipant_id, number)

  version ? version : create(pacticipant_id: pacticipant_id, number: number)
end

#find_by_pacticipant_name_and_latest_tag(pacticipant_name, tag) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/pact_broker/versions/repository.rb', line 19

def find_by_pacticipant_name_and_latest_tag pacticipant_name, tag
  PactBroker::Domain::Version
    .select_all_qualified
    .where_tag(tag)
    .where_pacticipant_name(pacticipant_name)
    .reverse_order(:order)
    .first
end

#find_by_pacticipant_name_and_number(pacticipant_name, number) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/pact_broker/versions/repository.rb', line 52

def find_by_pacticipant_name_and_number pacticipant_name, number
  PactBroker::Domain::Version
    .select_all_qualified
    .where_pacticipant_name(pacticipant_name)
    .where_number(number)
    .single_record
end

#find_by_pacticipant_name_and_tag(pacticipant_name, tag) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/pact_broker/versions/repository.rb', line 36

def find_by_pacticipant_name_and_tag pacticipant_name, tag
  PactBroker::Domain::Version
    .select_all_qualified
    .where_pacticipant_name(pacticipant_name)
    .where_tag(tag)
    .all
end

#find_latest_by_pacticipant_name_and_branch_name(pacticipant_name, branch_name) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/pact_broker/versions/repository.rb', line 28

def find_latest_by_pacticipant_name_and_branch_name(pacticipant_name, branch_name)
  branch_heads_join = { Sequel[:versions][:id] => Sequel[:branch_heads][:version_id], Sequel[:branch_heads][:branch_name] => branch_name }
  PactBroker::Domain::Version
    .where_pacticipant_name(pacticipant_name)
    .join(:branch_heads, branch_heads_join)
    .single_record
end

#find_latest_by_pacticpant_name(pacticipant_name) ⇒ Object



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

def find_latest_by_pacticpant_name pacticipant_name
  PactBroker::Domain::Version
    .select_all_qualified
    .where_pacticipant_name(pacticipant_name)
    .reverse_order(:order)
    .first
end

#find_latest_version_from_main_branch(pacticipant) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/pact_broker/versions/repository.rb', line 168

def find_latest_version_from_main_branch(pacticipant)
  if pacticipant.main_branch
    latest_from_main_branch = PactBroker::Domain::Version
      .latest_versions_for_pacticipant_branches(pacticipant.id, pacticipant.main_branch)
      .single_record

    latest_from_main_branch || find_by_pacticipant_name_and_latest_tag(pacticipant.name, pacticipant.main_branch)
  end
end

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



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

def find_pacticipant_versions_in_reverse_order(pacticipant_name, options = {}, pagination_options = {}, eager_load_associations = [])
  pacticipant = pacticipant_repository.find_by_name!(pacticipant_name)
  query = PactBroker::Domain::Version
            .where(pacticipant: pacticipant)
            .eager(*eager_load_associations)
            .reverse_order(:order)

  if options[:branch_name]
    query = query.where_branch_name(options[:branch_name])
  end
  query.all_with_pagination_options(pagination_options)
end

#find_versions_for_selector(selector) ⇒ Object



164
165
166
# File 'lib/pact_broker/versions/repository.rb', line 164

def find_versions_for_selector(selector)
  PactBroker::Domain::Version.select_all_qualified.for_selector(selector).all
end

#replace_tags(saved_version, open_struct_tags) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/pact_broker/versions/repository.rb', line 128

def replace_tags(saved_version, open_struct_tags)
  tag_repository.delete_by_version_id(saved_version.id)
  open_struct_tags.collect do | open_struct_tag |
    tag_repository.create(version: saved_version, name: open_struct_tag.name)
  end
  saved_version.refresh
end