pact_broker 2.76.1 → 2.79.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (188) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +12 -0
  3. data/.github/workflows/release_gem.yml +1 -0
  4. data/.github/workflows/test-ruby-3.yml +19 -0
  5. data/.github/workflows/test.yml +16 -7
  6. data/.gitignore +3 -1
  7. data/CHANGELOG.md +50 -0
  8. data/DEVELOPER_DOCUMENTATION.md +13 -0
  9. data/DEVELOPER_SETUP.md +62 -3
  10. data/Dockerfile +1 -0
  11. data/ISSUES.md +12 -4
  12. data/config.ru +2 -1
  13. data/db/ddl_statements/head_pact_tags.rb +24 -1
  14. data/db/ddl_statements/latest_tagged_pact_consumer_version_orders.rb +11 -0
  15. data/db/ddl_statements/latest_tagged_pact_publications.rb +6 -0
  16. data/db/ddl_statements/latest_verification_ids_for_consumer_version_tags.rb +13 -0
  17. data/db/migrations/20210117_add_branch_to_version.rb +9 -0
  18. data/db/migrations/20210202_add_created_at_to_head_pact_tags.rb +14 -0
  19. data/db/migrations/20210205_add_pacticipant_id_to_tag.rb +17 -0
  20. data/db/migrations/20210206_add_index_to_tags_and_versions.rb +27 -0
  21. data/db/migrations/20210207_optimise_latest_verification_ids_for_consumer_version_tags.rb +13 -0
  22. data/db/migrations/20210208_optimise_latest_tagged_pact_cv_orders.rb +13 -0
  23. data/db/migrations/20210210_create_environments_table.rb +16 -0
  24. data/docker-compose-dev-postgres.yml +9 -1
  25. data/docker-compose-issue-repro-with-pact-broker-docker-image.yml +16 -5
  26. data/lib/pact_broker/api.rb +7 -2
  27. data/lib/pact_broker/api/contracts/dry_validation_predicates.rb +8 -0
  28. data/lib/pact_broker/api/contracts/environment_schema.rb +49 -0
  29. data/lib/pact_broker/api/decorators/base_decorator.rb +11 -0
  30. data/lib/pact_broker/api/decorators/dashboard_decorator.rb +5 -1
  31. data/lib/pact_broker/api/decorators/environment_decorator.rb +30 -0
  32. data/lib/pact_broker/api/decorators/environments_decorator.rb +21 -0
  33. data/lib/pact_broker/api/decorators/matrix_decorator.rb +3 -1
  34. data/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb +2 -0
  35. data/lib/pact_broker/api/decorators/version_decorator.rb +15 -2
  36. data/lib/pact_broker/api/pact_broker_urls.rb +8 -0
  37. data/lib/pact_broker/api/resources/default_base_resource.rb +18 -0
  38. data/lib/pact_broker/api/resources/environment.rb +76 -0
  39. data/lib/pact_broker/api/resources/environments.rb +75 -0
  40. data/lib/pact_broker/api/resources/index.rb +20 -0
  41. data/lib/pact_broker/api/resources/latest_version.rb +27 -0
  42. data/lib/pact_broker/api/resources/provider_pacts_for_verification.rb +1 -0
  43. data/lib/pact_broker/api/resources/verifications.rb +5 -2
  44. data/lib/pact_broker/api/resources/version.rb +15 -9
  45. data/lib/pact_broker/api/resources/webhook_execution.rb +1 -1
  46. data/lib/pact_broker/app.rb +1 -1
  47. data/lib/pact_broker/certificates/certificate.rb +1 -1
  48. data/lib/pact_broker/config/setting.rb +1 -1
  49. data/lib/pact_broker/config/space_delimited_integer_list.rb +25 -0
  50. data/lib/pact_broker/configuration.rb +22 -1
  51. data/lib/pact_broker/db/data_migrations/helpers.rb +4 -0
  52. data/lib/pact_broker/db/data_migrations/set_extra_columns_for_tags.rb +29 -0
  53. data/lib/pact_broker/db/migrate_data.rb +1 -0
  54. data/lib/pact_broker/db/seed_example_data.rb +13 -13
  55. data/lib/pact_broker/deployments/environment.rb +15 -0
  56. data/lib/pact_broker/deployments/environment_service.rb +39 -0
  57. data/lib/pact_broker/doc/views/index/environment.markdown +37 -0
  58. data/lib/pact_broker/doc/views/index/environments.markdown +53 -0
  59. data/lib/pact_broker/doc/views/index/latest-pact-versions.markdown +1 -1
  60. data/lib/pact_broker/doc/views/index/pacticipant-version-tag.markdown +1 -0
  61. data/lib/pact_broker/doc/views/index/pacticipant-version.markdown +13 -0
  62. data/lib/pact_broker/domain/index_item.rb +18 -4
  63. data/lib/pact_broker/domain/pacticipant.rb +9 -5
  64. data/lib/pact_broker/domain/tag.rb +131 -71
  65. data/lib/pact_broker/domain/verification.rb +3 -2
  66. data/lib/pact_broker/domain/version.rb +58 -23
  67. data/lib/pact_broker/domain/webhook.rb +12 -9
  68. data/lib/pact_broker/hash_refinements.rb +4 -0
  69. data/lib/pact_broker/index/service.rb +55 -49
  70. data/lib/pact_broker/locale/en.yml +3 -1
  71. data/lib/pact_broker/matrix/quick_row.rb +8 -0
  72. data/lib/pact_broker/metrics/service.rb +1 -1
  73. data/lib/pact_broker/pacts/eager_loaders.rb +52 -0
  74. data/lib/pact_broker/pacts/latest_pact_publication_id_for_consumer_version.rb +18 -13
  75. data/lib/pact_broker/pacts/lazy_loaders.rb +14 -0
  76. data/lib/pact_broker/pacts/pact_publication.rb +38 -84
  77. data/lib/pact_broker/pacts/pact_publication_dataset_module.rb +297 -0
  78. data/lib/pact_broker/pacts/pact_version.rb +1 -2
  79. data/lib/pact_broker/pacts/pacts_for_verification_repository.rb +286 -0
  80. data/lib/pact_broker/pacts/repository.rb +5 -240
  81. data/lib/pact_broker/pacts/selected_pact.rb +4 -0
  82. data/lib/pact_broker/pacts/selector.rb +56 -1
  83. data/lib/pact_broker/pacts/selectors.rb +16 -0
  84. data/lib/pact_broker/pacts/service.rb +6 -8
  85. data/lib/pact_broker/pacts/squash_pacts_for_verification.rb +1 -4
  86. data/lib/pact_broker/pacts/verifiable_pact.rb +23 -2
  87. data/lib/pact_broker/pacts/verifiable_pact_messages.rb +56 -16
  88. data/lib/pact_broker/repositories/helpers.rb +4 -0
  89. data/lib/pact_broker/services.rb +9 -0
  90. data/lib/pact_broker/tags/eager_loaders.rb +47 -0
  91. data/lib/pact_broker/tags/repository.rb +3 -1
  92. data/lib/pact_broker/tags/service.rb +0 -3
  93. data/lib/pact_broker/tags/tag_with_latest_flag.rb +1 -0
  94. data/lib/pact_broker/test/http_test_data_builder.rb +86 -25
  95. data/lib/pact_broker/test/test_data_builder.rb +50 -3
  96. data/lib/pact_broker/ui/view_models/index_item.rb +19 -2
  97. data/lib/pact_broker/ui/view_models/matrix_line.rb +38 -2
  98. data/lib/pact_broker/ui/views/index/show-with-tags.haml +14 -6
  99. data/lib/pact_broker/ui/views/matrix/show.haml +12 -2
  100. data/lib/pact_broker/verifications/latest_verification_id_for_pact_version_and_provider_version.rb +7 -5
  101. data/lib/pact_broker/verifications/service.rb +2 -1
  102. data/lib/pact_broker/version.rb +1 -1
  103. data/lib/pact_broker/versions/eager_loaders.rb +71 -0
  104. data/lib/pact_broker/versions/lazy_loaders.rb +13 -0
  105. data/lib/pact_broker/versions/repository.rb +22 -2
  106. data/lib/pact_broker/versions/service.rb +5 -1
  107. data/lib/pact_broker/webhooks/execution.rb +3 -2
  108. data/lib/pact_broker/webhooks/latest_triggered_webhook.rb +2 -0
  109. data/lib/pact_broker/webhooks/pact_and_verification_parameters.rb +10 -3
  110. data/lib/pact_broker/webhooks/service.rb +8 -7
  111. data/lib/pact_broker/webhooks/trigger_service.rb +56 -23
  112. data/lib/pact_broker/webhooks/triggered_webhook.rb +14 -5
  113. data/lib/pact_broker/webhooks/webhook.rb +1 -1
  114. data/lib/pact_broker/webhooks/webhook_event.rb +1 -1
  115. data/lib/pact_broker/webhooks/webhook_execution_result.rb +6 -1
  116. data/lib/pact_broker/webhooks/webhook_request_logger.rb +7 -1
  117. data/lib/rack/pact_broker/set_base_url.rb +35 -5
  118. data/lib/sequel/plugins/upsert.rb +18 -4
  119. data/public/javascripts/clipboard.js +8 -2
  120. data/public/stylesheets/index.css +22 -1
  121. data/public/stylesheets/matrix.css +0 -21
  122. data/regression/can_i_deploy_spec.rb +5 -4
  123. data/regression/index_spec.rb +26 -0
  124. data/regression/regression_helper.rb +29 -3
  125. data/regression/script/clear.sh +3 -0
  126. data/regression/script/run.sh +3 -0
  127. data/script/demonstrate-version-branches.rb +33 -0
  128. data/script/pry.rb +2 -2
  129. data/script/reproduce-issue-starting-up.rb +13 -23
  130. data/script/reproduce-issue.rb +2 -1
  131. data/script/trigger-release.sh +1 -1
  132. data/script/webhook-server.ru +5 -5
  133. data/spec/features/create_environment_spec.rb +47 -0
  134. data/spec/features/create_tag_spec.rb +32 -0
  135. data/spec/features/create_version_spec.rb +70 -0
  136. data/spec/features/delete_environment_spec.rb +16 -0
  137. data/spec/features/end_deployment_spec.rb +29 -0
  138. data/spec/features/get_environment_spec.rb +19 -0
  139. data/spec/features/get_environments_spec.rb +20 -0
  140. data/spec/features/record_deployment_spec.rb +28 -0
  141. data/spec/features/update_environment_spec.rb +44 -0
  142. data/spec/fixtures/approvals/modifiable_resources.approved.json +6 -0
  143. data/spec/fixtures/dashboard.json +4 -2
  144. data/spec/lib/pact_broker/api/contracts/environment_schema_spec.rb +83 -0
  145. data/spec/lib/pact_broker/api/decorators/dashboard_decorator_spec.rb +4 -2
  146. data/spec/lib/pact_broker/api/decorators/matrix_decorator_spec.rb +11 -6
  147. data/spec/lib/pact_broker/api/decorators/verifiable_pacts_query_decorator_spec.rb +6 -0
  148. data/spec/lib/pact_broker/api/decorators/version_decorator_spec.rb +18 -0
  149. data/spec/lib/pact_broker/api/resources/default_base_resource_approval_spec.rb +1 -1
  150. data/spec/lib/pact_broker/api/resources/provider_pacts_for_verification_spec.rb +4 -0
  151. data/spec/lib/pact_broker/api/resources/verifications_spec.rb +2 -5
  152. data/spec/lib/pact_broker/api/resources/webhook_execution_result_spec.rb +56 -0
  153. data/spec/lib/pact_broker/api/resources/webhook_execution_spec.rb +3 -2
  154. data/spec/lib/pact_broker/config/space_delimited_integer_list_spec.rb +47 -0
  155. data/spec/lib/pact_broker/configuration_spec.rb +12 -0
  156. data/spec/lib/pact_broker/domain/tag_spec.rb +101 -27
  157. data/spec/lib/pact_broker/domain/version_spec.rb +103 -15
  158. data/spec/lib/pact_broker/domain/webhook_spec.rb +7 -7
  159. data/spec/lib/pact_broker/index/service_spec.rb +89 -15
  160. data/spec/lib/pact_broker/pacts/pact_publication_dataset_module_spec.rb +400 -0
  161. data/spec/lib/pact_broker/pacts/pact_publication_spec.rb +434 -14
  162. data/spec/lib/pact_broker/pacts/repository_find_for_verification_fallback_spec.rb +1 -1
  163. data/spec/lib/pact_broker/pacts/repository_find_for_verification_spec.rb +1 -1
  164. data/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_branch_spec.rb +224 -0
  165. data/spec/lib/pact_broker/pacts/repository_find_wip_pact_versions_for_provider_spec.rb +20 -7
  166. data/spec/lib/pact_broker/pacts/selector_spec.rb +3 -2
  167. data/spec/lib/pact_broker/pacts/service_find_for_verification_spec.rb +2 -3
  168. data/spec/lib/pact_broker/pacts/service_spec.rb +9 -7
  169. data/spec/lib/pact_broker/pacts/verifiable_pact_messages_spec.rb +57 -10
  170. data/spec/lib/pact_broker/tags/repository_spec.rb +2 -0
  171. data/spec/lib/pact_broker/verifications/service_spec.rb +4 -1
  172. data/spec/lib/pact_broker/versions/repository_spec.rb +54 -0
  173. data/spec/lib/pact_broker/webhooks/render_spec.rb +6 -5
  174. data/spec/lib/pact_broker/webhooks/service_spec.rb +13 -9
  175. data/spec/lib/pact_broker/webhooks/trigger_service_spec.rb +60 -18
  176. data/spec/lib/pact_broker/webhooks/webhook_request_logger_spec.rb +8 -0
  177. data/spec/lib/rack/pact_broker/set_base_url_spec.rb +86 -0
  178. data/spec/lib/sequel/plugins/upsert_spec.rb +31 -3
  179. data/spec/service_consumers/hal_relation_proxy_app.rb +3 -1
  180. data/spec/service_consumers/provider_states_for_pact_broker_client.rb +16 -0
  181. data/spec/spec_helper.rb +17 -5
  182. data/spec/support/approvals.rb +24 -0
  183. data/spec/support/shared_examples_for_responses.rb +11 -0
  184. data/tasks/database.rb +1 -1
  185. data/tasks/db.rake +1 -0
  186. data/tasks/rspec.rake +1 -1
  187. metadata +68 -4
  188. data/docker-compose-issue-repro.yml +0 -22
@@ -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
@@ -19,11 +19,11 @@ require 'pact_broker/pacts/selected_pact'
19
19
  require 'pact_broker/pacts/selector'
20
20
  require 'pact_broker/pacts/selectors'
21
21
  require 'pact_broker/feature_toggle'
22
+ require 'pact_broker/pacts/pacts_for_verification_repository'
22
23
 
23
24
  module PactBroker
24
25
  module Pacts
25
26
  class Repository
26
-
27
27
  include PactBroker::Logging
28
28
  include PactBroker::Repositories
29
29
  include PactBroker::Repositories::Helpers
@@ -146,91 +146,12 @@ module PactBroker
146
146
  end
147
147
  end
148
148
 
149
- def find_all_pact_versions_for_provider_with_consumer_version_tags provider_name, selector
150
- provider = pacticipant_repository.find_by_name(provider_name)
151
- consumer = selector.consumer ? pacticipant_repository.find_by_name(selector.consumer) : nil
152
-
153
- scope_for(PactPublication)
154
- .select_all_qualified
155
- .select_append(Sequel[:cv][:order].as(:consumer_version_order))
156
- .select_append(Sequel[:ct][:name].as(:consumer_version_tag_name))
157
- .remove_overridden_revisions
158
- .join_consumer_versions(:cv)
159
- .join_consumer_version_tags_with_names(selector.tag)
160
- .where(provider: provider)
161
- .where_consumer_if_set(consumer)
162
- .eager(:consumer)
163
- .eager(:consumer_version)
164
- .eager(:provider)
165
- .eager(:pact_version)
166
- .all
167
- .group_by(&:pact_version_id)
168
- .values
169
- .collect do | pact_publications |
170
- latest_pact_publication = pact_publications.sort_by{ |p| p.values.fetch(:consumer_version_order) }.last
171
- SelectedPact.new(latest_pact_publication.to_domain, Selectors.new(selector).resolve(latest_pact_publication.consumer_version))
172
- end
149
+ def find_for_verification(provider_name, consumer_version_selectors)
150
+ PactsForVerificationRepository.new.find(provider_name, consumer_version_selectors)
173
151
  end
174
152
 
175
- # To find the work in progress pacts for this verification execution:
176
- # For each provider tag that will be applied to this verification result (usually there will just be one, but
177
- # we have to allow for multiple tags),
178
- # find the head pacts (the pacts that are the latest for their tag) that have been successfully
179
- # verified against the provider tag.
180
- # Then, find all the head pacts, and remove the ones that have been successfully verified by ALL
181
- # of the provider tags supplied, and the ones that were published before the include_wip_pacts_since date.
182
- # Then, for all of the head pacts that are remaining (these are the WIP ones) work out which
183
- # provider tags they are pending for.
184
- # Don't include pact publications that were created before the provider tag was first used
185
- # (that is, before the provider's git branch was created).
186
- def find_wip_pact_versions_for_provider provider_name, provider_tags_names = [], options = {}
187
- # TODO not sure about this
188
- return [] if provider_tags_names.empty?
189
-
190
- provider = pacticipant_repository.find_by_name(provider_name)
191
-
192
- # Hash of provider tag name => list of head pacts that have been successfully verified by that tag
193
- successfully_verified_head_pacts_for_provider_tags = find_successfully_verified_head_pacts_by_provider_tag(provider.id, provider_tags_names, options)
194
- # Create hash of provider tag name => list of pact publication ids
195
- successfully_verified_head_pact_publication_ids_for_each_provider_tag = successfully_verified_head_pacts_for_provider_tags.each_with_object({}) do | (provider_tag_name, head_pacts), hash |
196
- hash[provider_tag_name] = head_pacts.collect(&:id).uniq
197
- end
198
-
199
- # list of head pact_publication_ids that are NOT work in progress because they've been verified by all of the provider version tags supplied
200
- non_wip_pact_publication_ids = successfully_verified_head_pacts_for_provider_tags.values.collect{ |head_pacts| head_pacts.collect(&:id) }.reduce(:&)
201
-
202
- wip_pact_publication_ids = find_head_pacts_that_have_not_been_successfully_verified_by_all_provider_tags(
203
- provider.id,
204
- non_wip_pact_publication_ids,
205
- options)
206
-
207
- wip_pacts = scope_for(PactPublication).where(id: wip_pact_publication_ids)
208
-
209
- # The first instance (by date) of each provider tag with that name
210
- provider_tag_collection = PactBroker::Domain::Tag
211
- .select_group(Sequel[:tags][:name], Sequel[:pacticipant_id])
212
- .select_append(Sequel.function(:min, Sequel[:tags][:created_at]).as(:created_at))
213
- .distinct
214
- .join(:versions, { Sequel[:tags][:version_id] => Sequel[:versions][:id] } )
215
- .where(pacticipant_id: provider.id)
216
- .where(name: provider_tags_names)
217
- .all
218
-
219
- provider_version_count = scope_for(PactBroker::Domain::Version).where(pacticipant: provider).count
220
-
221
- verifiable_pacts = wip_pacts.collect do | pact|
222
- pending_tag_names = find_provider_tags_for_which_pact_publication_id_is_pending(pact, successfully_verified_head_pact_publication_ids_for_each_provider_tag)
223
- pre_existing_tag_names = find_provider_tag_names_that_were_first_used_before_pact_published(pact, provider_tag_collection)
224
-
225
- pre_existing_pending_tags = pending_tag_names & pre_existing_tag_names
226
-
227
- if pre_existing_pending_tags.any? || (PactBroker.feature_enabled?(:experimental_no_provider_versions_makes_all_head_pacts_wip) && provider_version_count == 0)
228
- selectors = Selectors.create_for_latest_of_each_tag(pact.head_tag_names)
229
- VerifiablePact.new(pact.to_domain, selectors, true, pre_existing_pending_tags, [], true)
230
- end
231
- end.compact.sort
232
-
233
- deduplicate_verifiable_pacts(verifiable_pacts)
153
+ def find_wip_pact_versions_for_provider provider_name, provider_version_branch, provider_tags_names = [], options = {}
154
+ PactsForVerificationRepository.new.find_wip(provider_name, provider_version_branch, provider_tags_names, options)
234
155
  end
235
156
 
236
157
  def find_pact_versions_for_provider provider_name, tag = nil
@@ -389,110 +310,8 @@ module PactBroker
389
310
  end
390
311
  end
391
312
 
392
- # Returns a list of Domain::Pact objects the represent pact publications
393
- def find_for_verification(provider_name, consumer_version_selectors)
394
- selected_pacts = find_pacts_for_which_the_latest_version_is_required(provider_name, consumer_version_selectors) +
395
- find_pacts_for_which_the_latest_version_for_the_tag_is_required(provider_name, consumer_version_selectors) +
396
- find_pacts_for_which_all_versions_for_the_tag_are_required(provider_name, consumer_version_selectors)
397
-
398
- selected_pacts = selected_pacts + find_pacts_for_fallback_tags(selected_pacts, provider_name, consumer_version_selectors)
399
-
400
- selected_pacts
401
- .group_by{ |p| [p.consumer_name, p.pact_version_sha] }
402
- .values
403
- .collect do | selected_pacts_for_pact_version_id |
404
- SelectedPact.merge(selected_pacts_for_pact_version_id)
405
- end
406
- end
407
-
408
313
  private
409
314
 
410
- def find_pacts_for_fallback_tags(selected_pacts, provider_name, consumer_version_selectors)
411
- # TODO at the moment, the validation doesn't stop fallback being used with 'all' selectors
412
- selectors_with_fallback_tags = consumer_version_selectors.select(&:fallback_tag?)
413
- selectors_missing_a_pact = selectors_with_fallback_tags.reject do | selector |
414
- selected_pacts.any? do | selected_pact |
415
- selected_pact.latest_for_tag?(selector.tag)
416
- end
417
- end
418
-
419
- if selectors_missing_a_pact.any?
420
- find_pacts_for_which_the_latest_version_for_the_fallback_tag_is_required(provider_name, selectors_missing_a_pact)
421
- else
422
- []
423
- end
424
- end
425
-
426
- def find_pacts_for_which_the_latest_version_is_required(provider_name, consumer_version_selectors)
427
- if consumer_version_selectors.empty?
428
- scope_for(LatestPactPublications)
429
- .provider(provider_name)
430
- .order_ignore_case(:consumer_name)
431
- .collect do | latest_pact_publication |
432
- pact_publication = PactPublication.find(id: latest_pact_publication.id)
433
- SelectedPact.new(pact_publication.to_domain, Selectors.create_for_overall_latest.resolve(pact_publication.consumer_version))
434
- end
435
- else
436
- selectors_for_overall_latest = consumer_version_selectors.select(&:overall_latest?)
437
- selectors_for_overall_latest.flat_map do | selector |
438
- query = scope_for(LatestPactPublications).provider(provider_name)
439
- query = query.consumer(selector.consumer) if selector.consumer
440
- query.collect do | latest_pact_publication |
441
- pact_publication = PactPublication.find(id: latest_pact_publication.id)
442
- resolved_selector = selector.consumer ? Selector.latest_for_consumer(selector.consumer) : Selector.overall_latest
443
- SelectedPact.new(pact_publication.to_domain, Selectors.new(resolved_selector).resolve(pact_publication.consumer_version))
444
- end
445
- end
446
- end
447
- end
448
-
449
- def find_pacts_for_which_the_latest_version_for_the_tag_is_required(provider_name, consumer_version_selectors)
450
- # The tags for which only the latest version is specified
451
- selectors = consumer_version_selectors.select(&:latest_for_tag?)
452
-
453
- selectors.flat_map do | selector |
454
- query = scope_for(LatestTaggedPactPublications).where(tag_name: selector.tag).provider(provider_name)
455
- query = query.consumer(selector.consumer) if selector.consumer
456
- query.all.collect do | latest_tagged_pact_publication |
457
- pact_publication = PactPublication.find(id: latest_tagged_pact_publication.id)
458
- resolved_selector = if selector.consumer
459
- Selector.latest_for_tag_and_consumer(selector.tag, selector.consumer).resolve(pact_publication.consumer_version)
460
- else
461
- Selector.latest_for_tag(selector.tag).resolve(pact_publication.consumer_version)
462
- end
463
- SelectedPact.new(
464
- pact_publication.to_domain,
465
- Selectors.new(resolved_selector)
466
- )
467
- end
468
- end
469
- end
470
-
471
- def find_pacts_for_which_the_latest_version_for_the_fallback_tag_is_required(provider_name, selectors)
472
- selectors.collect do | selector |
473
- query = scope_for(LatestTaggedPactPublications).provider(provider_name).where(tag_name: selector.fallback_tag)
474
- query = query.consumer(selector.consumer) if selector.consumer
475
- query.all
476
- .collect do | latest_tagged_pact_publication |
477
- pact_publication = unscoped(PactPublication).find(id: latest_tagged_pact_publication.id)
478
- SelectedPact.new(
479
- pact_publication.to_domain,
480
- Selectors.new(selector.resolve(pact_publication.consumer_version))
481
- )
482
- end
483
- end.flatten
484
- end
485
-
486
-
487
- def find_pacts_for_which_all_versions_for_the_tag_are_required(provider_name, consumer_version_selectors)
488
- # The tags for which all versions are specified
489
- selectors = consumer_version_selectors.select(&:all_for_tag?)
490
-
491
- selectors.flat_map do | selector |
492
- find_all_pact_versions_for_provider_with_consumer_version_tags(provider_name, selector)
493
- end
494
- end
495
-
496
315
  def find_previous_distinct_pact_by_sha pact
497
316
  current_pact_content_sha =
498
317
  scope_for(LatestPactPublicationsByConsumerVersion).select(:pact_version_sha)
@@ -541,17 +360,6 @@ module PactBroker
541
360
  query
542
361
  end
543
362
 
544
- def find_provider_tags_for_which_pact_publication_id_is_pending(pact_publication, successfully_verified_head_pact_publication_ids_for_each_provider_tag)
545
- successfully_verified_head_pact_publication_ids_for_each_provider_tag
546
- .select do | _, pact_publication_ids |
547
- !pact_publication_ids.include?(pact_publication.id)
548
- end.keys
549
- end
550
-
551
- def find_provider_tag_names_that_were_first_used_before_pact_published(pact_publication, provider_tag_collection)
552
- provider_tag_collection.select { | tag| to_datetime(tag.created_at) < pact_publication.created_at }.collect(&:name)
553
- end
554
-
555
363
  # Note: created_at is coming back as a string for sqlite
556
364
  # Can't work out how to to tell Sequel that this should be a date
557
365
  def to_datetime string_or_datetime
@@ -561,49 +369,6 @@ module PactBroker
561
369
  string_or_datetime
562
370
  end
563
371
  end
564
-
565
- def find_head_pacts_that_have_not_been_successfully_verified_by_all_provider_tags(provider_id, pact_publication_ids_successfully_verified_by_all_provider_tags, options)
566
- # Exclude the head pacts that have been successfully verified by all the specified provider tags
567
- scope_for(LatestTaggedPactPublications)
568
- .where(provider_id: provider_id)
569
- .where(Sequel.lit('latest_tagged_pact_publications.created_at > ?', options.fetch(:include_wip_pacts_since)))
570
- .exclude(id: pact_publication_ids_successfully_verified_by_all_provider_tags)
571
- .select_for_subquery(:id)
572
- end
573
-
574
- # Find the head pacts that have been successfully verified by a provider version with the specified
575
- # provider version tags.
576
- # Returns a Hash of provider_tag => LatestTaggedPactPublications with only pact publication id and tag_name populated
577
- # This is the list of pacts we are EXCLUDING from the WIP list because they have already been verified successfully
578
- def find_successfully_verified_head_pacts_by_provider_tag(provider_id, provider_tags, options)
579
- provider_tags.compact.each_with_object({}) do | provider_tag, hash |
580
- verifications_join = {
581
- pact_version_id: :pact_version_id,
582
- Sequel[:verifications][:success] => true,
583
- Sequel[:verifications][:wip] => false,
584
- Sequel[:verifications][:provider_id] => provider_id
585
- }
586
- tags_join = {
587
- Sequel[:verifications][:provider_version_id] => Sequel[:provider_tags][:version_id],
588
- Sequel[:provider_tags][:name] => provider_tag
589
- }
590
- head_pacts = scope_for(LatestTaggedPactPublications)
591
- .select(Sequel[:latest_tagged_pact_publications][:id].as(:id))
592
- .join(:verifications, verifications_join)
593
- .join(:tags, tags_join, { table_alias: :provider_tags } )
594
- .where(Sequel[:latest_tagged_pact_publications][:provider_id] => provider_id)
595
- .distinct
596
- .all
597
- hash[provider_tag] = head_pacts
598
- end
599
- end
600
-
601
- def deduplicate_verifiable_pacts(verifiable_pacts)
602
- verifiable_pacts
603
- .group_by { | verifiable_pact | verifiable_pact.pact_version_sha }
604
- .values
605
- .collect { | verifiable_pacts | verifiable_pacts.reduce(&:+) }
606
- end
607
372
  end
608
373
  end
609
374
  end