Class: PactBroker::Pacts::Repository
- Inherits:
-
Object
- Object
- PactBroker::Pacts::Repository
show all
- Includes:
- Logging, Repositories
- Defined in:
- lib/pact_broker/pacts/repository.rb
Instance Method Summary
collapse
-
#create(params) ⇒ Object
-
#delete(params) ⇒ Object
-
#delete_all_pact_versions_between(consumer_name, options) ⇒ Object
-
#delete_by_version_id(version_id) ⇒ Object
-
#find_all_pact_versions_between(consumer_name, options) ⇒ Object
-
#find_all_revisions(consumer_name, consumer_version, provider_name) ⇒ Object
-
#find_by_consumer_version(consumer_name, consumer_version_number) ⇒ Object
Returns latest pact version for the consumer_version_number.
-
#find_by_version_and_provider(version_id, provider_id) ⇒ Object
-
#find_latest_pact(consumer_name, provider_name, tag = nil) ⇒ Object
-
#find_latest_pact_versions_for_provider(provider_name, tag = nil) ⇒ Object
-
#find_latest_pacts ⇒ Object
-
#find_next_pact(pact) ⇒ Object
-
#find_pact(consumer_name, consumer_version, provider_name, pact_version_sha = nil) ⇒ Object
-
#find_pact_versions_for_provider(provider_name, tag = nil) ⇒ Object
-
#find_pending_pact_versions_for_provider(provider_name) ⇒ Object
-
#find_previous_distinct_pact(pact) ⇒ Object
-
#find_previous_pact(pact, tag = nil) ⇒ Object
-
#find_previous_pacts(pact) ⇒ Object
-
#next_revision_number(existing_model) ⇒ Object
This logic is a separate method so we can stub it to create a “conflict” scenario.
-
#search_for_latest_pact(consumer_name, provider_name, tag = nil) ⇒ Object
Allows optional consumer_name and provider_name.
-
#update(id, params) ⇒ Object
-
#update_latest_pact_publication_ids(pact_publication) ⇒ Object
#label_repository, #matrix_repository, #pact_repository, #pacticipant_repository, #tag_repository, #verification_repository, #version_repository, #webhook_repository
Methods included from Logging
included, #log_error
Instance Method Details
#create(params) ⇒ Object
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/pact_broker/pacts/repository.rb', line 23
def create params
pact_publication = PactPublication.new(
consumer_version_id: params[:version_id],
provider_id: params[:provider_id],
consumer_id: params[:consumer_id],
pact_version: find_or_create_pact_version(params.fetch(:consumer_id), params.fetch(:provider_id), params[:json_content]),
).save
update_latest_pact_publication_ids(pact_publication)
pact_publication.to_domain
end
|
#delete(params) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/pact_broker/pacts/repository.rb', line 74
def delete params
id = AllPactPublications
.consumer(params.consumer_name)
.provider(params.provider_name)
.consumer_version_number(params.consumer_version_number)
.select_for_subquery(:id)
PactPublication.where(id: id).delete
end
|
#delete_all_pact_versions_between(consumer_name, options) ⇒ Object
94
95
96
97
98
|
# File 'lib/pact_broker/pacts/repository.rb', line 94
def delete_all_pact_versions_between consumer_name, options
ids = find_all_database_versions_between(consumer_name, options).select_for_subquery(:id)
webhook_repository.delete_triggered_webhooks_by_pact_publication_ids(ids)
PactPublication.where(id: ids).delete
end
|
#delete_by_version_id(version_id) ⇒ Object
83
84
85
|
# File 'lib/pact_broker/pacts/repository.rb', line 83
def delete_by_version_id version_id
Sequel::Model.db[:pact_publications].where(consumer_version_id: version_id).delete
end
|
#find_all_pact_versions_between(consumer_name, options) ⇒ Object
87
88
89
90
91
92
|
# File 'lib/pact_broker/pacts/repository.rb', line 87
def find_all_pact_versions_between consumer_name, options
find_all_database_versions_between(consumer_name, options)
.eager(:tags)
.reverse_order(:consumer_version_order)
.collect(&:to_domain)
end
|
#find_all_revisions(consumer_name, consumer_version, provider_name) ⇒ Object
195
196
197
198
199
200
201
|
# File 'lib/pact_broker/pacts/repository.rb', line 195
def find_all_revisions consumer_name, consumer_version, provider_name
AllPactPublications
.consumer(consumer_name)
.provider(provider_name)
.consumer_version_number(consumer_version)
.order(:consumer_version_order, :revision_number).collect(&:to_domain_with_content)
end
|
#find_by_consumer_version(consumer_name, consumer_version_number) ⇒ Object
Returns latest pact version for the consumer_version_number
133
134
135
136
137
138
|
# File 'lib/pact_broker/pacts/repository.rb', line 133
def find_by_consumer_version consumer_name, consumer_version_number
LatestPactPublicationsByConsumerVersion
.consumer(consumer_name)
.consumer_version_number(consumer_version_number)
.collect(&:to_domain_with_content)
end
|
#find_by_version_and_provider(version_id, provider_id) ⇒ Object
140
141
142
143
144
145
|
# File 'lib/pact_broker/pacts/repository.rb', line 140
def find_by_version_and_provider version_id, provider_id
LatestPactPublicationsByConsumerVersion
.eager(:tags)
.where(consumer_version_id: version_id, provider_id: provider_id)
.limit(1).collect(&:to_domain_with_content)[0]
end
|
#find_latest_pact(consumer_name, provider_name, tag = nil) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/pact_broker/pacts/repository.rb', line 151
def find_latest_pact(consumer_name, provider_name, tag = nil)
query = LatestPactPublicationsByConsumerVersion
.select_all_qualified
.consumer(consumer_name)
.provider(provider_name)
if tag == :untagged
query = query.untagged
elsif tag
query = query.tag(tag)
end
query.latest.all.collect(&:to_domain_with_content)[0]
end
|
#find_latest_pact_versions_for_provider(provider_name, tag = nil) ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/pact_broker/pacts/repository.rb', line 100
def find_latest_pact_versions_for_provider provider_name, tag = nil
if tag
LatestTaggedPactPublications.provider(provider_name).order_ignore_case(:consumer_name).where(tag_name: tag).collect(&:to_domain)
else
LatestPactPublications.provider(provider_name).order_ignore_case(:consumer_name).collect(&:to_domain)
end
end
|
#find_latest_pacts ⇒ Object
147
148
149
|
# File 'lib/pact_broker/pacts/repository.rb', line 147
def find_latest_pacts
LatestPactPublications.order(:consumer_name, :provider_name).collect(&:to_domain)
end
|
#find_next_pact(pact) ⇒ Object
219
220
221
222
223
224
225
226
|
# File 'lib/pact_broker/pacts/repository.rb', line 219
def find_next_pact pact
LatestPactPublicationsByConsumerVersion
.eager(:tags)
.consumer(pact.consumer.name)
.provider(pact.provider.name)
.consumer_version_order_after(pact.consumer_version.order)
.earliest.collect(&:to_domain_with_content)[0]
end
|
#find_pact(consumer_name, consumer_version, provider_name, pact_version_sha = nil) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/pact_broker/pacts/repository.rb', line 178
def find_pact consumer_name, consumer_version, provider_name, pact_version_sha = nil
query = if pact_version_sha
AllPactPublications
.pact_version_sha(pact_version_sha)
.reverse_order(:consumer_version_order)
.limit(1)
else
LatestPactPublicationsByConsumerVersion
end
query = query
.eager(:tags)
.consumer(consumer_name)
.provider(provider_name)
query = query.consumer_version_number(consumer_version) if consumer_version
query.collect(&:to_domain_with_content)[0]
end
|
#find_pact_versions_for_provider(provider_name, tag = nil) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/pact_broker/pacts/repository.rb', line 114
def find_pact_versions_for_provider provider_name, tag = nil
if tag
LatestPactPublicationsByConsumerVersion
.join(:tags, {version_id: :consumer_version_id})
.provider(provider_name)
.order_ignore_case(:consumer_name)
.order_append(:consumer_version_order)
.where(Sequel[:tags][:name] => tag)
.collect(&:to_domain)
else
LatestPactPublicationsByConsumerVersion
.provider(provider_name)
.order_ignore_case(:consumer_name)
.order_append(:consumer_version_order)
.collect(&:to_domain)
end
end
|
#find_pending_pact_versions_for_provider(provider_name) ⇒ Object
108
109
110
111
112
|
# File 'lib/pact_broker/pacts/repository.rb', line 108
def find_pending_pact_versions_for_provider provider_name
provider_id = pacticipant_repository.find_by_name(provider_name).id
pact_publication_ids = PactBroker::Matrix::HeadRow.where(provider_id: provider_id).exclude(success: true).select_for_subquery(:pact_publication_id)
AllPactPublications.where(id: pact_publication_ids).order_ignore_case(:consumer_name).order_append(:consumer_version_order).collect(&:to_domain)
end
|
#find_previous_distinct_pact(pact) ⇒ Object
228
229
230
231
232
233
234
235
|
# File 'lib/pact_broker/pacts/repository.rb', line 228
def find_previous_distinct_pact pact
previous, current = nil, pact
loop do
previous = find_previous_distinct_pact_by_sha current
return previous if previous.nil? || different?(current, previous)
current = previous
end
end
|
#find_previous_pact(pact, tag = nil) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/pact_broker/pacts/repository.rb', line 203
def find_previous_pact pact, tag = nil
query = LatestPactPublicationsByConsumerVersion
.eager(:tags)
.consumer(pact.consumer.name)
.provider(pact.provider.name)
if tag == :untagged
query = query.untagged
elsif tag
query = query.tag(tag)
end
query.consumer_version_order_before(pact.consumer_version.order)
.latest.collect(&:to_domain_with_content)[0]
end
|
#find_previous_pacts(pact) ⇒ Object
237
238
239
240
241
242
243
|
# File 'lib/pact_broker/pacts/repository.rb', line 237
def find_previous_pacts pact
if pact.consumer_version_tag_names.any?
pact.consumer_version_tag_names.map { |tag| find_previous_pact(pact, tag) }
else
[find_previous_pact(pact, :untagged)]
end
end
|
#next_revision_number(existing_model) ⇒ Object
This logic is a separate method so we can stub it to create a “conflict” scenario
58
59
60
|
# File 'lib/pact_broker/pacts/repository.rb', line 58
def next_revision_number(existing_model)
existing_model.revision_number + 1
end
|
#search_for_latest_pact(consumer_name, provider_name, tag = nil) ⇒ Object
Allows optional consumer_name and provider_name
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/pact_broker/pacts/repository.rb', line 165
def search_for_latest_pact(consumer_name, provider_name, tag = nil)
query = LatestPactPublicationsByConsumerVersion.select_all_qualified
query = query.consumer(consumer_name) if consumer_name
query = query.provider(provider_name) if provider_name
if tag == :untagged
query = query.untagged
elsif tag
query = query.tag(tag)
end
query.latest.all.collect(&:to_domain_with_content)[0]
end
|
#update(id, params) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/pact_broker/pacts/repository.rb', line 34
def update id, params
existing_model = PactPublication.find(id: id)
pact_version = find_or_create_pact_version(existing_model.consumer_version.pacticipant_id, existing_model.provider_id, params[:json_content])
if existing_model.pact_version_id != pact_version.id
key = {
consumer_version_id: existing_model.consumer_version_id,
provider_id: existing_model.provider_id,
revision_number: next_revision_number(existing_model),
}
new_params = key.merge(
consumer_id: existing_model.consumer_id,
pact_version_id: pact_version.id,
created_at: Sequel.datetime_class.now
)
PactPublication.upsert(new_params, key.keys)
pact_publication = PactPublication.where(key).single_record
update_latest_pact_publication_ids(pact_publication)
pact_publication.to_domain
else
existing_model.to_domain
end
end
|
#update_latest_pact_publication_ids(pact_publication) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/pact_broker/pacts/repository.rb', line 62
def update_latest_pact_publication_ids(pact_publication)
params = {
consumer_version_id: pact_publication.consumer_version_id,
provider_id: pact_publication.provider_id,
pact_publication_id: pact_publication.id,
consumer_id: pact_publication.consumer_id,
pact_version_id: pact_publication.pact_version_id
}
LatestPactPublicationIdForConsumerVersion.new(params).upsert
end
|