pact_broker 2.76.2 → 2.77.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +12 -0
  3. data/.github/workflows/test-ruby-3.yml +19 -0
  4. data/.github/workflows/test.yml +16 -7
  5. data/.gitignore +3 -1
  6. data/CHANGELOG.md +15 -0
  7. data/DEVELOPER_SETUP.md +62 -3
  8. data/Dockerfile +1 -0
  9. data/ISSUES.md +7 -7
  10. data/config.ru +1 -0
  11. data/db/ddl_statements/head_pact_tags.rb +24 -1
  12. data/db/ddl_statements/latest_tagged_pact_consumer_version_orders.rb +11 -0
  13. data/db/ddl_statements/latest_tagged_pact_publications.rb +6 -0
  14. data/db/ddl_statements/latest_verification_ids_for_consumer_version_tags.rb +13 -0
  15. data/db/migrations/20210117_add_branch_to_version.rb +9 -0
  16. data/db/migrations/20210202_add_created_at_to_head_pact_tags.rb +14 -0
  17. data/db/migrations/20210205_add_pacticipant_id_to_tag.rb +17 -0
  18. data/db/migrations/20210206_add_index_to_tags_and_versions.rb +27 -0
  19. data/db/migrations/20210207_optimise_latest_verification_ids_for_consumer_version_tags.rb +13 -0
  20. data/db/migrations/20210208_optimise_latest_tagged_pact_cv_orders.rb +13 -0
  21. data/lib/pact_broker/api.rb +2 -2
  22. data/lib/pact_broker/api/decorators/dashboard_decorator.rb +5 -1
  23. data/lib/pact_broker/api/decorators/matrix_decorator.rb +3 -1
  24. data/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb +2 -0
  25. data/lib/pact_broker/api/decorators/version_decorator.rb +4 -2
  26. data/lib/pact_broker/api/resources/default_base_resource.rb +4 -0
  27. data/lib/pact_broker/api/resources/index.rb +6 -0
  28. data/lib/pact_broker/api/resources/latest_version.rb +27 -0
  29. data/lib/pact_broker/api/resources/provider_pacts_for_verification.rb +1 -0
  30. data/lib/pact_broker/api/resources/version.rb +15 -9
  31. data/lib/pact_broker/certificates/certificate.rb +1 -1
  32. data/lib/pact_broker/config/setting.rb +1 -1
  33. data/lib/pact_broker/config/space_delimited_integer_list.rb +25 -0
  34. data/lib/pact_broker/configuration.rb +17 -1
  35. data/lib/pact_broker/db/data_migrations/helpers.rb +4 -0
  36. data/lib/pact_broker/db/data_migrations/set_extra_columns_for_tags.rb +29 -0
  37. data/lib/pact_broker/db/migrate_data.rb +1 -0
  38. data/lib/pact_broker/db/seed_example_data.rb +13 -13
  39. data/lib/pact_broker/doc/views/index/pacticipant-version.markdown +13 -0
  40. data/lib/pact_broker/domain/index_item.rb +18 -4
  41. data/lib/pact_broker/domain/pacticipant.rb +9 -5
  42. data/lib/pact_broker/domain/tag.rb +131 -71
  43. data/lib/pact_broker/domain/verification.rb +3 -2
  44. data/lib/pact_broker/domain/version.rb +58 -23
  45. data/lib/pact_broker/domain/webhook.rb +6 -3
  46. data/lib/pact_broker/index/service.rb +55 -49
  47. data/lib/pact_broker/matrix/quick_row.rb +8 -0
  48. data/lib/pact_broker/metrics/service.rb +1 -1
  49. data/lib/pact_broker/pacts/eager_loaders.rb +52 -0
  50. data/lib/pact_broker/pacts/latest_pact_publication_id_for_consumer_version.rb +18 -13
  51. data/lib/pact_broker/pacts/lazy_loaders.rb +14 -0
  52. data/lib/pact_broker/pacts/pact_publication.rb +38 -84
  53. data/lib/pact_broker/pacts/pact_publication_dataset_module.rb +297 -0
  54. data/lib/pact_broker/pacts/pact_version.rb +1 -2
  55. data/lib/pact_broker/pacts/pacts_for_verification_repository.rb +286 -0
  56. data/lib/pact_broker/pacts/repository.rb +5 -240
  57. data/lib/pact_broker/pacts/selected_pact.rb +4 -0
  58. data/lib/pact_broker/pacts/selector.rb +56 -1
  59. data/lib/pact_broker/pacts/selectors.rb +16 -0
  60. data/lib/pact_broker/pacts/service.rb +2 -6
  61. data/lib/pact_broker/pacts/squash_pacts_for_verification.rb +1 -4
  62. data/lib/pact_broker/pacts/verifiable_pact.rb +23 -2
  63. data/lib/pact_broker/pacts/verifiable_pact_messages.rb +56 -16
  64. data/lib/pact_broker/repositories/helpers.rb +4 -0
  65. data/lib/pact_broker/tags/eager_loaders.rb +47 -0
  66. data/lib/pact_broker/tags/repository.rb +3 -1
  67. data/lib/pact_broker/tags/service.rb +0 -3
  68. data/lib/pact_broker/tags/tag_with_latest_flag.rb +1 -0
  69. data/lib/pact_broker/test/http_test_data_builder.rb +23 -7
  70. data/lib/pact_broker/test/test_data_builder.rb +36 -3
  71. data/lib/pact_broker/ui/view_models/index_item.rb +9 -0
  72. data/lib/pact_broker/ui/view_models/matrix_line.rb +36 -0
  73. data/lib/pact_broker/ui/views/index/show-with-tags.haml +8 -0
  74. data/lib/pact_broker/ui/views/matrix/show.haml +10 -0
  75. data/lib/pact_broker/verifications/latest_verification_id_for_pact_version_and_provider_version.rb +7 -5
  76. data/lib/pact_broker/version.rb +1 -1
  77. data/lib/pact_broker/versions/eager_loaders.rb +71 -0
  78. data/lib/pact_broker/versions/lazy_loaders.rb +13 -0
  79. data/lib/pact_broker/versions/repository.rb +11 -2
  80. data/lib/pact_broker/versions/service.rb +5 -1
  81. data/lib/pact_broker/webhooks/execution.rb +3 -2
  82. data/lib/pact_broker/webhooks/latest_triggered_webhook.rb +2 -0
  83. data/lib/pact_broker/webhooks/triggered_webhook.rb +11 -4
  84. data/lib/pact_broker/webhooks/webhook.rb +1 -1
  85. data/lib/pact_broker/webhooks/webhook_event.rb +1 -1
  86. data/lib/pact_broker/webhooks/webhook_request_logger.rb +5 -1
  87. data/lib/sequel/plugins/upsert.rb +18 -4
  88. data/public/stylesheets/index.css +22 -1
  89. data/public/stylesheets/matrix.css +0 -21
  90. data/regression/can_i_deploy_spec.rb +5 -4
  91. data/regression/index_spec.rb +26 -0
  92. data/regression/regression_helper.rb +29 -3
  93. data/regression/script/clear.sh +3 -0
  94. data/regression/script/run.sh +3 -0
  95. data/script/demonstrate-version-branches.rb +33 -0
  96. data/script/pry.rb +2 -2
  97. data/spec/features/create_version_spec.rb +44 -0
  98. data/spec/fixtures/dashboard.json +4 -2
  99. data/spec/lib/pact_broker/api/decorators/dashboard_decorator_spec.rb +4 -2
  100. data/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb +11 -6
  101. data/spec/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator_spec.rb +6 -0
  102. data/spec/lib/pact_broker/api/resources/provider_pacts_for_verification_spec.rb +4 -0
  103. data/spec/lib/pact_broker/config/space_delimited_integer_list_spec.rb +47 -0
  104. data/spec/lib/pact_broker/configuration_spec.rb +12 -0
  105. data/spec/lib/pact_broker/domain/tag_spec.rb +101 -27
  106. data/spec/lib/pact_broker/domain/version_spec.rb +103 -15
  107. data/spec/lib/pact_broker/domain/webhook_spec.rb +1 -1
  108. data/spec/lib/pact_broker/index/service_spec.rb +89 -15
  109. data/spec/lib/pact_broker/pacts/pact_publication_dataset_module_spec.rb +400 -0
  110. data/spec/lib/pact_broker/pacts/pact_publication_spec.rb +434 -14
  111. data/spec/lib/pact_broker/pacts/repository_find_for_verification_fallback_spec.rb +1 -1
  112. data/spec/lib/pact_broker/pacts/repository_find_for_verification_spec.rb +1 -1
  113. data/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_branch_spec.rb +224 -0
  114. data/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_spec.rb +20 -7
  115. data/spec/lib/pact_broker/pacts/selector_spec.rb +3 -2
  116. data/spec/lib/pact_broker/pacts/service_find_for_verification_spec.rb +2 -3
  117. data/spec/lib/pact_broker/pacts/service_spec.rb +2 -2
  118. data/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb +57 -10
  119. data/spec/lib/pact_broker/tags/repository_spec.rb +2 -0
  120. data/spec/lib/pact_broker/versions/repository_spec.rb +44 -0
  121. data/spec/lib/pact_broker/webhooks/webhook_request_logger_spec.rb +8 -0
  122. data/spec/lib/sequel/plugins/upsert_spec.rb +31 -3
  123. data/spec/spec_helper.rb +17 -5
  124. data/spec/support/approvals.rb +24 -0
  125. data/tasks/db.rake +1 -0
  126. data/tasks/rspec.rake +1 -1
  127. metadata +36 -3
@@ -0,0 +1,297 @@
1
+ module PactBroker
2
+ module Pacts
3
+ module PactPublicationDatasetModule
4
+ def for_provider provider
5
+ where(provider: provider)
6
+ end
7
+
8
+ def for_consumer consumer
9
+ where(consumer: consumer)
10
+ end
11
+
12
+ def for_provider_and_consumer_version_selector provider, selector
13
+ # Does not yet support "all pacts for specified tag" - that code is still in the Pact::Repository
14
+ query = for_provider(provider)
15
+ query = query.for_consumer(PactBroker::Domain::Pacticipant.find_by_name(selector.consumer)) if selector.consumer
16
+ # Do this last so that the provider/consumer criteria get included in the "latest" query before the join, rather than after
17
+ query = query.latest_for_consumer_branch(selector.branch) if selector.latest_for_branch?
18
+ query = query.latest_for_consumer_tag(selector.tag) if selector.latest_for_tag?
19
+ query = query.overall_latest if selector.overall_latest?
20
+ query
21
+ end
22
+
23
+ def latest_by_consumer_branch
24
+ versions_join = {
25
+ Sequel[:pact_publications][:consumer_version_id] => Sequel[:cv][:id]
26
+ }
27
+
28
+ base_query = join(:versions, versions_join, { table_alias: :cv }) do
29
+ Sequel.lit("cv.branch is not null")
30
+ end
31
+
32
+ self_join = {
33
+ Sequel[:pact_publications][:consumer_id] => Sequel[:pp2][:consumer_id],
34
+ Sequel[:cv][:branch] => Sequel[:pp2][:branch]
35
+ }
36
+
37
+ if no_columns_selected?
38
+ base_query = base_query.select_all_qualified.select_append(Sequel[:cv][:branch])
39
+ end
40
+
41
+ base_query.left_join(base_query.select(:consumer_id, :branch, :order), self_join, { table_alias: :pp2 } ) do
42
+ Sequel[:pp2][:order] > Sequel[:cv][:order]
43
+ end
44
+ .where(Sequel[:pp2][:order] => nil)
45
+ .remove_overridden_revisions_from_complete_query
46
+ end
47
+
48
+ def overall_latest
49
+ base_query = join_consumer_versions # won't need to do this when we add the order to latest_pact_publication_ids_for_consumer_versions
50
+
51
+ self_join = {
52
+ Sequel[:pact_publications][:consumer_id] => Sequel[:pp2][:consumer_id],
53
+ Sequel[:pact_publications][:provider_id] => Sequel[:pp2][:provider_id]
54
+ }
55
+
56
+ base_query = base_query.select_all_qualified if no_columns_selected?
57
+
58
+ base_query.left_join(base_query.select(:consumer_id, :provider_id, :order), self_join, { table_alias: :pp2 } ) do
59
+ Sequel[:pp2][:order] > Sequel[:cv][:order]
60
+ end
61
+ .where(Sequel[:pp2][:order] => nil)
62
+ .remove_overridden_revisions_from_complete_query
63
+ end
64
+
65
+ def latest_for_consumer_branch(branch)
66
+ versions_join = {
67
+ Sequel[:pact_publications][:consumer_version_id] => Sequel[:cv][:id],
68
+ Sequel[:cv][:branch] => branch
69
+ }
70
+
71
+ base_query = join(:versions, versions_join, { table_alias: :cv }) do
72
+ Sequel.lit("cv.branch is not null")
73
+ end
74
+
75
+ self_join = {
76
+ Sequel[:pact_publications][:consumer_id] => Sequel[:pp2][:consumer_id],
77
+ Sequel[:cv][:branch] => Sequel[:pp2][:branch]
78
+ }
79
+
80
+ if no_columns_selected?
81
+ base_query = base_query.select_all_qualified.select_append(Sequel[:cv][:branch])
82
+ end
83
+
84
+ base_query.left_join(base_query.select(:consumer_id, :branch, :order), self_join, { table_alias: :pp2 } ) do
85
+ Sequel[:pp2][:order] > Sequel[:cv][:order]
86
+ end
87
+ .where(Sequel[:pp2][:order] => nil)
88
+ .remove_overridden_revisions_from_complete_query
89
+ end
90
+
91
+ def latest_by_consumer_tag
92
+ tags_join = {
93
+ Sequel[:pact_publications][:consumer_version_id] => Sequel[:tags][:version_id]
94
+ }
95
+
96
+ base_query = join(:tags, tags_join)
97
+
98
+ if no_columns_selected?
99
+ base_query = base_query.select_all_qualified.select_append(Sequel[:tags][:name].as(:tag_name))
100
+ end
101
+
102
+ joined_query = base_query.select(
103
+ Sequel[:pact_publications][:consumer_id],
104
+ Sequel[:tags][:version_order],
105
+ Sequel[:tags][:name].as(:tag_name)
106
+ )
107
+
108
+ self_join = {
109
+ Sequel[:pact_publications][:consumer_id] => Sequel[:pp2][:consumer_id],
110
+ Sequel[:tags][:name] => Sequel[:pp2][:tag_name]
111
+ }
112
+ base_query.left_join(joined_query, self_join, { table_alias: :pp2 } ) do
113
+ Sequel[:pp2][:version_order] > Sequel[:tags][:version_order]
114
+ end
115
+ .where(Sequel[:pp2][:version_order] => nil)
116
+ .remove_overridden_revisions_from_complete_query
117
+ end
118
+
119
+ def latest_for_consumer_tag(tag_name)
120
+ tags_join = {
121
+ Sequel[:pact_publications][:consumer_version_id] => Sequel[:tags][:version_id],
122
+ Sequel[:tags][:name] => tag_name
123
+ }
124
+
125
+ base_query = self
126
+ if no_columns_selected?
127
+ base_query = base_query.select_all_qualified.select_append(Sequel[:tags][:name].as(:tag_name))
128
+ end
129
+
130
+ base_query = base_query
131
+ .join(:tags, tags_join)
132
+ .where(Sequel[:tags][:name] => tag_name)
133
+
134
+ joined_query = base_query.select(
135
+ Sequel[:pact_publications][:consumer_id],
136
+ Sequel[:tags][:name].as(:tag_name),
137
+ Sequel[:tags][:version_order]
138
+ )
139
+
140
+ self_join = {
141
+ Sequel[:pact_publications][:consumer_id] => Sequel[:pp2][:consumer_id],
142
+ Sequel[:tags][:name] => Sequel[:pp2][:tag_name]
143
+ }
144
+
145
+ base_query.left_join(joined_query, self_join, { table_alias: :pp2 } ) do
146
+ Sequel[:pp2][:version_order] > Sequel[:tags][:version_order]
147
+ end
148
+ .where(Sequel[:pp2][:version_order] => nil)
149
+ .remove_overridden_revisions_from_complete_query
150
+ end
151
+
152
+ def successfully_verified_by_provider_branch(provider_id, provider_version_branch)
153
+ verifications_join = {
154
+ pact_version_id: :pact_version_id,
155
+ Sequel[:verifications][:success] => true,
156
+ Sequel[:verifications][:wip] => false,
157
+ Sequel[:verifications][:provider_id] => provider_id
158
+ }
159
+ versions_join = {
160
+ Sequel[:verifications][:provider_version_id] => Sequel[:provider_versions][:id],
161
+ Sequel[:provider_versions][:branch] => provider_version_branch,
162
+ Sequel[:provider_versions][:pacticipant_id] => provider_id
163
+ }
164
+
165
+ from_self(alias: :pp).select(Sequel[:pp].*)
166
+ .join(:verifications, verifications_join)
167
+ .join(:versions, versions_join, { table_alias: :provider_versions } )
168
+ .where(Sequel[:pp][:provider_id] => provider_id)
169
+ .distinct
170
+ end
171
+
172
+ def successfully_verified_by_provider_tag(provider_id, provider_tag)
173
+ verifications_join = {
174
+ pact_version_id: :pact_version_id,
175
+ Sequel[:verifications][:success] => true,
176
+ Sequel[:verifications][:wip] => false,
177
+ Sequel[:verifications][:provider_id] => provider_id
178
+ }
179
+ tags_join = {
180
+ Sequel[:verifications][:provider_version_id] => Sequel[:provider_tags][:version_id],
181
+ Sequel[:provider_tags][:name] => provider_tag
182
+ }
183
+
184
+ from_self(alias: :pp).select(Sequel[:pp].*)
185
+ .join(:verifications, verifications_join)
186
+ .join(:tags, tags_join, { table_alias: :provider_tags } )
187
+ .where(Sequel[:pp][:provider_id] => provider_id)
188
+ .distinct
189
+ end
190
+
191
+ def created_after date
192
+ where(Sequel.lit("#{first_source_alias}.created_at > ?", date))
193
+ end
194
+
195
+ def remove_overridden_revisions(pact_publications_alias = :pact_publications)
196
+ join(:latest_pact_publication_ids_for_consumer_versions, { Sequel[:lp][:pact_publication_id] => Sequel[pact_publications_alias][:id] }, { table_alias: :lp})
197
+ end
198
+
199
+ def remove_overridden_revisions_from_complete_query
200
+ from_self(alias: :p)
201
+ .select(Sequel[:p].*)
202
+ .remove_overridden_revisions(:p)
203
+ end
204
+
205
+ def join_consumer_versions(table_alias = :cv, extra_join_criteria = {}, &block)
206
+ versions_join = {
207
+ Sequel[:pact_publications][:consumer_version_id] => Sequel[table_alias][:id]
208
+ }.merge(extra_join_criteria)
209
+ join(:versions, versions_join, { table_alias: table_alias }, &block)
210
+ end
211
+
212
+ def join_consumer_version_tags(table_alias = :ct)
213
+ join(:tags, { Sequel[table_alias][:version_id] => Sequel[:pact_publications][:consumer_version_id]}, { table_alias: table_alias })
214
+ end
215
+
216
+ def join_consumer_version_tags_with_names(consumer_version_tag_names)
217
+ join(:tags, {
218
+ Sequel[:ct][:version_id] => Sequel[:pact_publications][:consumer_version_id],
219
+ Sequel[:ct][:name] => consumer_version_tag_names
220
+ }, {
221
+ table_alias: :ct
222
+ })
223
+ end
224
+
225
+ def join_providers(table_alias = :providers, base_table = :pact_publications, extra_join_criteria = {})
226
+ provider_join = {
227
+ Sequel[base_table][:provider_id] => Sequel[table_alias][:id]
228
+ }.merge(extra_join_criteria)
229
+ join(:pacticipants, provider_join, { table_alias: table_alias })
230
+ end
231
+
232
+ def join_consumers(table_alias = :consumers, base_table = :pact_publications, extra_join_criteria = {})
233
+ consumer_join = {
234
+ Sequel[base_table][:consumer_id] => Sequel[table_alias][:id]
235
+ }.merge(extra_join_criteria)
236
+ join(:pacticipants, consumer_join, { table_alias: table_alias })
237
+ end
238
+
239
+ def join_pact_versions
240
+ join(:pact_versions, { Sequel[:pact_publications][:pact_version_id] => Sequel[:pact_versions][:id] })
241
+ end
242
+
243
+ def eager_load_pact_versions
244
+ eager(:pact_versions)
245
+ end
246
+
247
+ def tag tag_name
248
+ filter = name_like(Sequel.qualify(:tags, :name), tag_name)
249
+ join(:tags, {version_id: :consumer_version_id}).where(filter)
250
+ end
251
+
252
+ def provider_name_like(name)
253
+ where(name_like(Sequel[:providers][:name], name))
254
+ end
255
+
256
+ def consumer_name_like(name)
257
+ where(name_like(Sequel[:consumers][:name], name))
258
+ end
259
+
260
+ def consumer_version_number_like(number)
261
+ where(name_like(Sequel[:cv][:number], number))
262
+ end
263
+
264
+ def consumer_version_tag(tag)
265
+ where(Sequel[:ct][:name] => tag)
266
+ end
267
+
268
+ def order_by_consumer_name
269
+ order_append_ignore_case(Sequel[:consumers][:name])
270
+ end
271
+
272
+ def order_by_consumer_version_order
273
+ order_append(Sequel[:cv][:order])
274
+ end
275
+
276
+ def where_consumer_if_set(consumer)
277
+ if consumer
278
+ where(consumer: consumer)
279
+ else
280
+ self
281
+ end
282
+ end
283
+
284
+ def delete
285
+ require 'pact_broker/webhooks/triggered_webhook'
286
+ PactBroker::Webhooks::TriggeredWebhook.where(pact_publication: self).delete
287
+ super
288
+ end
289
+
290
+ private
291
+
292
+ def no_columns_selected?
293
+ opts[:select].nil?
294
+ end
295
+ end
296
+ end
297
+ end
@@ -85,10 +85,9 @@ module PactBroker
85
85
  end
86
86
  end
87
87
 
88
-
89
88
  # Table: pact_versions
90
89
  # Columns:
91
- # id | integer | PRIMARY KEY DEFAULT nextval('pact_versions_id_seq'::regclass)
90
+ # id | integer | PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY
92
91
  # consumer_id | integer | NOT NULL
93
92
  # provider_id | integer | NOT NULL
94
93
  # sha | text | NOT NULL
@@ -0,0 +1,286 @@
1
+ require 'sequel'
2
+ require 'pact_broker/logging'
3
+ require 'pact_broker/pacts/pact_publication'
4
+ require 'pact_broker/domain'
5
+ require 'pact_broker/pacts/verifiable_pact'
6
+ require 'pact_broker/repositories/helpers'
7
+ require 'pact_broker/pacts/selected_pact'
8
+ require 'pact_broker/pacts/selector'
9
+ require 'pact_broker/pacts/selectors'
10
+ require 'pact_broker/feature_toggle'
11
+
12
+ module PactBroker
13
+ module Pacts
14
+ class PactsForVerificationRepository
15
+ include PactBroker::Logging
16
+ include PactBroker::Repositories
17
+ include PactBroker::Repositories::Helpers
18
+
19
+ def find(provider_name, consumer_version_selectors)
20
+ selected_pacts = find_pacts_for_which_the_latest_version_of_something_is_required(provider_name, consumer_version_selectors) +
21
+ find_pacts_for_which_all_versions_for_the_tag_are_required(provider_name, consumer_version_selectors)
22
+ selected_pacts = selected_pacts + find_pacts_for_fallback_tags(selected_pacts, provider_name, consumer_version_selectors)
23
+ merge_selected_pacts(selected_pacts)
24
+ end
25
+
26
+ # To find the work in progress pacts for this verification execution:
27
+ # For each provider tag that will be applied to this verification result (usually there will just be one, but
28
+ # we have to allow for multiple tags),
29
+ # find the head pacts (the pacts that are the latest for their tag) that have been successfully
30
+ # verified against the provider tag.
31
+ # Then, find all the head pacts, and remove the ones that have been successfully verified by ALL
32
+ # of the provider tags supplied, and the ones that were published before the include_wip_pacts_since date.
33
+ # Then, for all of the head pacts that are remaining (these are the WIP ones) work out which
34
+ # provider tags they are pending for.
35
+ # Don't include pact publications that were created before the provider tag was first used
36
+ # (that is, before the provider's git branch was created).
37
+ def find_wip provider_name, provider_version_branch, provider_tags_names = [], options = {}
38
+ # TODO not sure about this
39
+ return [] if provider_tags_names.empty? && provider_version_branch == nil
40
+
41
+ if provider_version_branch
42
+ return find_wip_pact_versions_for_provider_by_provider_branch(provider_name, provider_version_branch, options)
43
+ end
44
+
45
+ provider = pacticipant_repository.find_by_name(provider_name)
46
+ wip_start_date = options.fetch(:include_wip_pacts_since)
47
+ provider_tags = provider_tag_objects_for(provider, provider_tags_names)
48
+
49
+ wip_by_consumer_tags = find_wip_pact_versions_for_provider_by_provider_tags(
50
+ provider,
51
+ provider_tags_names,
52
+ provider_tags,
53
+ wip_start_date,
54
+ :latest_by_consumer_tag)
55
+
56
+ wip_by_consumer_branches = find_wip_pact_versions_for_provider_by_provider_tags(
57
+ provider,
58
+ provider_tags_names,
59
+ provider_tags,
60
+ wip_start_date,
61
+ :latest_by_consumer_branch)
62
+
63
+ deduplicate_verifiable_pacts(wip_by_consumer_tags + wip_by_consumer_branches).sort
64
+ end
65
+
66
+ private
67
+
68
+ def scope_for(scope)
69
+ PactBroker.policy_scope!(scope)
70
+ end
71
+
72
+ # For the times when it doesn't make sense to use the scoped class, this is a way to
73
+ # indicate that it is an intentional use of the PactVersion class directly.
74
+ def unscoped(scope)
75
+ scope
76
+ end
77
+
78
+ # Note: created_at is coming back as a string for sqlite
79
+ # Can't work out how to to tell Sequel that this should be a date
80
+ def to_datetime string_or_datetime
81
+ if string_or_datetime.is_a?(String)
82
+ Sequel.string_to_datetime(string_or_datetime)
83
+ else
84
+ string_or_datetime
85
+ end
86
+ end
87
+
88
+ def find_pacts_for_fallback_tags(selected_pacts, provider_name, consumer_version_selectors)
89
+ # TODO at the moment, the validation doesn't stop fallback being used with 'all' selectors
90
+ selectors_with_fallback_tags = consumer_version_selectors.select(&:fallback_tag?)
91
+ selectors_missing_a_pact = selectors_with_fallback_tags.reject do | selector |
92
+ selected_pacts.any? do | selected_pact |
93
+ selected_pact.latest_for_tag?(selector.tag)
94
+ end
95
+ end
96
+
97
+ if selectors_missing_a_pact.any?
98
+ find_pacts_for_which_the_latest_version_for_the_fallback_tag_is_required(provider_name, selectors_missing_a_pact)
99
+ else
100
+ []
101
+ end
102
+ end
103
+
104
+ def find_pacts_for_which_the_latest_version_of_something_is_required(provider_name, consumer_version_selectors)
105
+ provider = pacticipant_repository.find_by_name(provider_name)
106
+
107
+ selectors = if consumer_version_selectors.empty?
108
+ Selectors.create_for_overall_latest
109
+ else
110
+ consumer_version_selectors.select(&:latest_for_tag?) +
111
+ consumer_version_selectors.select(&:latest_for_branch?) +
112
+ consumer_version_selectors.select(&:overall_latest?)
113
+ end
114
+
115
+ selectors.flat_map do | selector |
116
+ query = scope_for(PactPublication).for_provider_and_consumer_version_selector(provider, selector)
117
+ query.all.collect do | pact_publication |
118
+ SelectedPact.new(
119
+ pact_publication.to_domain,
120
+ Selectors.new(selector.resolve(pact_publication.consumer_version))
121
+ )
122
+ end
123
+ end
124
+ end
125
+
126
+ def find_pacts_for_which_the_latest_version_for_the_fallback_tag_is_required(provider_name, selectors)
127
+ selectors.collect do | selector |
128
+ query = scope_for(LatestTaggedPactPublications).provider(provider_name).where(tag_name: selector.fallback_tag)
129
+ query = query.consumer(selector.consumer) if selector.consumer
130
+ query.all
131
+ .collect do | latest_tagged_pact_publication |
132
+ pact_publication = unscoped(PactPublication).find(id: latest_tagged_pact_publication.id)
133
+ SelectedPact.new(
134
+ pact_publication.to_domain,
135
+ Selectors.new(selector.resolve_for_fallback(pact_publication.consumer_version))
136
+ )
137
+ end
138
+ end.flatten
139
+ end
140
+
141
+
142
+ def find_pacts_for_which_all_versions_for_the_tag_are_required(provider_name, consumer_version_selectors)
143
+ # The tags for which all versions are specified
144
+ selectors = consumer_version_selectors.select(&:all_for_tag?)
145
+
146
+ selectors.flat_map do | selector |
147
+ find_all_pact_versions_for_provider_with_consumer_version_tags(provider_name, selector)
148
+ end
149
+ end
150
+
151
+ def find_provider_tags_for_which_pact_publication_id_is_pending(pact_publication, successfully_verified_head_pact_publication_ids_for_each_provider_tag)
152
+ successfully_verified_head_pact_publication_ids_for_each_provider_tag
153
+ .select do | _, pact_publication_ids |
154
+ !pact_publication_ids.include?(pact_publication.id)
155
+ end.keys
156
+ end
157
+
158
+ def find_provider_tag_names_that_were_first_used_before_pact_published(pact_publication, provider_tag_collection)
159
+ provider_tag_collection.select { | tag| to_datetime(tag.created_at) < pact_publication.created_at }.collect(&:name)
160
+ end
161
+
162
+ def deduplicate_verifiable_pacts(verifiable_pacts)
163
+ VerifiablePact.deduplicate(verifiable_pacts)
164
+ end
165
+
166
+ def merge_selected_pacts(selected_pacts)
167
+ selected_pacts
168
+ .group_by{ |p| [p.consumer_name, p.pact_version_sha] }
169
+ .values
170
+ .collect do | selected_pacts_for_pact_version_id |
171
+ SelectedPact.merge(selected_pacts_for_pact_version_id)
172
+ end
173
+ end
174
+
175
+ def provider_tag_objects_for(provider, provider_tags_names)
176
+ PactBroker::Domain::Tag
177
+ .select_group(Sequel[:tags][:name], Sequel[:pacticipant_id])
178
+ .select_append(Sequel.function(:min, Sequel[:tags][:created_at]).as(:created_at))
179
+ .distinct
180
+ .where(pacticipant_id: provider.id)
181
+ .where(name: provider_tags_names)
182
+ .all
183
+ end
184
+
185
+ def find_wip_pact_versions_for_provider_by_provider_tags(provider, provider_tags_names, provider_tags, wip_start_date, pact_publication_scope)
186
+ potential_wip_pacts_by_consumer_tag_query = PactPublication.for_provider(provider).created_after(wip_start_date).send(pact_publication_scope)
187
+ potential_wip_pacts_by_consumer_tag = potential_wip_pacts_by_consumer_tag_query.all
188
+
189
+ tag_to_pact_publications = provider_tags_names.each_with_object({}) do | provider_tag_name, tag_to_pact_publications |
190
+ tag_to_pact_publications[provider_tag_name] = remove_already_verified_by_tag(
191
+ potential_wip_pacts_by_consumer_tag,
192
+ potential_wip_pacts_by_consumer_tag_query,
193
+ provider,
194
+ provider_tag_name
195
+ )
196
+ end
197
+
198
+ provider_has_no_versions = !provider.any_versions?
199
+
200
+ tag_to_pact_publications.flat_map do | provider_tag_name, pact_publications |
201
+ pact_publications.collect do | pact_publication |
202
+ force_include = PactBroker.feature_enabled?(:experimental_no_provider_versions_makes_all_head_pacts_wip) && provider_has_no_versions
203
+
204
+ pending_tag_names_to_use = if force_include
205
+ [provider_tag_name]
206
+ else
207
+ pre_existing_tag_names = find_provider_tag_names_that_were_first_used_before_pact_published(pact_publication, provider_tags)
208
+ [provider_tag_name] & pre_existing_tag_names
209
+ end
210
+
211
+ if pending_tag_names_to_use.any?
212
+ selectors = create_selectors_for_wip_pact(pact_publication)
213
+ VerifiablePact.create_for_wip_for_provider_tags(pact_publication.to_domain, selectors, pending_tag_names_to_use)
214
+ end
215
+ end
216
+ end.compact
217
+ end
218
+
219
+ def create_selectors_for_wip_pact(pact_publication)
220
+ if pact_publication.values[:tag_name]
221
+ Selectors.create_for_latest_for_tag(pact_publication.values[:tag_name])
222
+ else
223
+ Selectors.create_for_latest_for_branch(pact_publication.consumer_version.branch)
224
+ end
225
+ end
226
+
227
+ def find_wip_pact_versions_for_provider_by_provider_branch(provider_name, provider_version_branch, options)
228
+ provider = pacticipant_repository.find_by_name(provider_name)
229
+ wip_start_date = options.fetch(:include_wip_pacts_since)
230
+
231
+ wip_pact_publications_by_branch = remove_already_verified_by_branch(
232
+ PactPublication.for_provider(provider).created_after(wip_start_date).latest_by_consumer_branch,
233
+ provider,
234
+ provider_version_branch
235
+ )
236
+
237
+ wip_pact_publications_by_tag = remove_already_verified_by_branch(
238
+ PactPublication.for_provider(provider).created_after(wip_start_date).latest_by_consumer_tag,
239
+ provider,
240
+ provider_version_branch
241
+ )
242
+
243
+ verifiable_pacts = (wip_pact_publications_by_branch + wip_pact_publications_by_tag).collect do | pact_publication |
244
+ selectors = create_selectors_for_wip_pact(pact_publication)
245
+ VerifiablePact.create_for_wip_for_provider_branch(pact_publication.to_domain, selectors, provider_version_branch)
246
+ end
247
+
248
+ deduplicate_verifiable_pacts(verifiable_pacts).sort
249
+ end
250
+
251
+ def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name, selector
252
+ provider = pacticipant_repository.find_by_name(provider_name)
253
+ consumer = selector.consumer ? pacticipant_repository.find_by_name(selector.consumer) : nil
254
+
255
+ scope_for(PactPublication)
256
+ .select_all_qualified
257
+ .select_append(Sequel[:cv][:order].as(:consumer_version_order))
258
+ .select_append(Sequel[:ct][:name].as(:consumer_version_tag_name))
259
+ .remove_overridden_revisions
260
+ .join_consumer_versions(:cv)
261
+ .join_consumer_version_tags_with_names(selector.tag)
262
+ .where(provider: provider)
263
+ .where_consumer_if_set(consumer)
264
+ .eager(:consumer)
265
+ .eager(:consumer_version)
266
+ .eager(:provider)
267
+ .eager(:pact_version)
268
+ .all
269
+ .group_by(&:pact_version_id)
270
+ .values
271
+ .collect do | pact_publications |
272
+ latest_pact_publication = pact_publications.sort_by{ |p| p.values.fetch(:consumer_version_order) }.last
273
+ SelectedPact.new(latest_pact_publication.to_domain, Selectors.new(selector).resolve(latest_pact_publication.consumer_version))
274
+ end
275
+ end
276
+
277
+ def remove_already_verified_by_branch(pact_publications, provider, provider_version_branch)
278
+ PactPublication.subtract(pact_publications.all, pact_publications.successfully_verified_by_provider_branch(provider.id, provider_version_branch).all)
279
+ end
280
+
281
+ def remove_already_verified_by_tag(pact_publications, query, provider, tag)
282
+ PactPublication.subtract(pact_publications, query.successfully_verified_by_provider_tag(provider.id, tag).all)
283
+ end
284
+ end
285
+ end
286
+ end