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
@@ -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
@@ -21,6 +21,10 @@ module PactBroker
21
21
  selectors.tag_names_of_selectors_for_latest_pacts
22
22
  end
23
23
 
24
+ def branches_of_selectors_for_latest_pacts
25
+ selectors.branches_of_selectors_for_latest_pacts
26
+ end
27
+
24
28
  def overall_latest?
25
29
  selectors.overall_latest?
26
30
  end
@@ -1,11 +1,19 @@
1
+ require 'pact_broker/hash_refinements'
2
+
1
3
  module PactBroker
2
4
  module Pacts
3
5
  class Selector < Hash
6
+ using PactBroker::HashRefinements
7
+
4
8
  def initialize(options = {})
5
9
  merge!(options)
6
10
  end
7
11
 
8
12
  def resolve(consumer_version)
13
+ ResolvedSelector.new(self.to_h.without(:fallback_tag, :fallback_branch), consumer_version)
14
+ end
15
+
16
+ def resolve_for_fallback(consumer_version)
9
17
  ResolvedSelector.new(self.to_h, consumer_version)
10
18
  end
11
19
 
@@ -13,6 +21,10 @@ module PactBroker
13
21
  self[:tag] = tag
14
22
  end
15
23
 
24
+ def branch= branch
25
+ self[:branch] = branch
26
+ end
27
+
16
28
  def latest= latest
17
29
  self[:latest] = latest
18
30
  end
@@ -25,10 +37,18 @@ module PactBroker
25
37
  self[:fallback_tag] = fallback_tag
26
38
  end
27
39
 
40
+ def fallback_branch= fallback_branch
41
+ self[:fallback_branch] = fallback_branch
42
+ end
43
+
28
44
  def fallback_tag
29
45
  self[:fallback_tag]
30
46
  end
31
47
 
48
+ def fallback_branch
49
+ self[:fallback_branch]
50
+ end
51
+
32
52
  def consumer= consumer
33
53
  self[:consumer] = consumer
34
54
  end
@@ -45,10 +65,18 @@ module PactBroker
45
65
  Selector.new(latest: true, tag: tag)
46
66
  end
47
67
 
68
+ def self.latest_for_branch(branch)
69
+ Selector.new(latest: true, branch: branch)
70
+ end
71
+
48
72
  def self.latest_for_tag_with_fallback(tag, fallback_tag)
49
73
  Selector.new(latest: true, tag: tag, fallback_tag: fallback_tag)
50
74
  end
51
75
 
76
+ def self.latest_for_branch_with_fallback(branch, fallback_branch)
77
+ Selector.new(latest: true, branch: branch, fallback_branch: fallback_branch)
78
+ end
79
+
52
80
  def self.all_for_tag(tag)
53
81
  Selector.new(tag: tag)
54
82
  end
@@ -61,6 +89,10 @@ module PactBroker
61
89
  Selector.new(latest: true, tag: tag, consumer: consumer)
62
90
  end
63
91
 
92
+ def self.latest_for_branch_and_consumer(branch, consumer)
93
+ Selector.new(latest: true, branch: branch, consumer: consumer)
94
+ end
95
+
64
96
  def self.latest_for_consumer(consumer)
65
97
  Selector.new(latest: true, consumer: consumer)
66
98
  end
@@ -73,12 +105,20 @@ module PactBroker
73
105
  !!fallback_tag
74
106
  end
75
107
 
108
+ def fallback_branch?
109
+ !!fallback_branch
110
+ end
111
+
76
112
  def tag
77
113
  self[:tag]
78
114
  end
79
115
 
116
+ def branch
117
+ self[:branch]
118
+ end
119
+
80
120
  def overall_latest?
81
- !!(latest? && !tag)
121
+ !!(latest? && !tag && !branch)
82
122
  end
83
123
 
84
124
  # Not sure if the fallback_tag logic is needed
@@ -90,6 +130,15 @@ module PactBroker
90
130
  end
91
131
  end
92
132
 
133
+ # Not sure if the fallback_tag logic is needed
134
+ def latest_for_branch? potential_branch = nil
135
+ if potential_branch
136
+ !!(latest && branch == potential_branch)
137
+ else
138
+ !!(latest && !!branch)
139
+ end
140
+ end
141
+
93
142
  def all_for_tag_and_consumer?
94
143
  !!(tag && !latest? && consumer)
95
144
  end
@@ -109,6 +158,12 @@ module PactBroker
109
158
  else
110
159
  overall_latest? ? -1 : 1
111
160
  end
161
+ elsif latest_for_branch? || other.latest_for_branch?
162
+ if latest_for_branch? == other.latest_for_branch?
163
+ branch <=> other.branch
164
+ else
165
+ latest_for_branch? ? -1 : 1
166
+ end
112
167
  elsif latest_for_tag? || other.latest_for_tag?
113
168
  if latest_for_tag? == other.latest_for_tag?
114
169
  tag <=> other.tag
@@ -15,6 +15,18 @@ module PactBroker
15
15
  Selectors.new(tag_names.collect{ | tag_name | Selector.latest_for_tag(tag_name) })
16
16
  end
17
17
 
18
+ def self.create_for_latest_for_tag(tag_name)
19
+ Selectors.new([Selector.latest_for_tag(tag_name)])
20
+ end
21
+
22
+ def self.create_for_latest_of_each_branch(branches)
23
+ Selectors.new(branches.collect{ | branch | Selector.latest_for_branch(branch) })
24
+ end
25
+
26
+ def self.create_for_latest_for_branch(branch)
27
+ Selectors.new([Selector.latest_for_branch(branch)])
28
+ end
29
+
18
30
  def self.create_for_overall_latest
19
31
  Selectors.new([Selector.overall_latest])
20
32
  end
@@ -42,6 +54,10 @@ module PactBroker
42
54
  def tag_names_of_selectors_for_latest_pacts
43
55
  select(&:latest_for_tag?).collect(&:tag).uniq
44
56
  end
57
+
58
+ def branches_of_selectors_for_latest_pacts
59
+ select(&:latest_for_branch?).collect(&:branch).uniq
60
+ end
45
61
  end
46
62
  end
47
63
  end