pact_broker 1.18.0 → 2.0.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -9
  3. data/DEVELOPER_DOCUMENTATION.md +53 -0
  4. data/README.md +13 -13
  5. data/db/migrations/23_create_pact_contents_table.rb +19 -0
  6. data/db/migrations/24_populate_pact_contents.rb +9 -0
  7. data/db/migrations/25_populate_pact_contents_pacticipants.rb +7 -0
  8. data/db/migrations/26_make_pc_pacticipants_mandatory.rb +8 -0
  9. data/db/migrations/27_create_pact_publications.rb +18 -0
  10. data/db/migrations/28_populate_pact_publications.rb +13 -0
  11. data/db/migrations/29_recreate_all_pacts.rb +16 -0
  12. data/db/migrations/30_drop_pacts.rb +8 -0
  13. data/db/migrations/31_drop_pact_version_contents.rb +5 -0
  14. data/db/migrations/32_rename_pact_contents.rb +5 -0
  15. data/db/migrations/33_recreate_all_pacts.rb +52 -0
  16. data/db/migrations/34_recreate_latest_tagged_pacts.rb +23 -0
  17. data/db/migrations/35_create_verifications.rb +15 -0
  18. data/db/migrations/36_create_latest_verifications.rb +14 -0
  19. data/db/pact_broker_database.sqlite3 +0 -0
  20. data/lib/pact_broker/api.rb +25 -10
  21. data/lib/pact_broker/api/contracts/verification_contract.rb +41 -0
  22. data/lib/pact_broker/api/decorators/pact_decorator.rb +7 -0
  23. data/lib/pact_broker/api/decorators/pact_version_decorator.rb +0 -1
  24. data/lib/pact_broker/api/decorators/representable_pact.rb +4 -3
  25. data/lib/pact_broker/api/decorators/timestamps.rb +1 -1
  26. data/lib/pact_broker/api/decorators/verification_decorator.rb +30 -0
  27. data/lib/pact_broker/api/decorators/verifications_decorator.rb +21 -0
  28. data/lib/pact_broker/api/decorators/version_decorator.rb +14 -0
  29. data/lib/pact_broker/api/decorators/versions_decorator.rb +1 -1
  30. data/lib/pact_broker/api/pact_broker_urls.rb +41 -2
  31. data/lib/pact_broker/api/renderers/html_pact_renderer.rb +1 -1
  32. data/lib/pact_broker/api/resources/base_resource.rb +11 -2
  33. data/lib/pact_broker/api/resources/latest_verifications_for_consumer_version.rb +39 -0
  34. data/lib/pact_broker/api/resources/pacticipants.rb +1 -1
  35. data/lib/pact_broker/api/resources/previous_distinct_pact_version.rb +1 -1
  36. data/lib/pact_broker/api/resources/verifications.rb +67 -0
  37. data/lib/pact_broker/api/resources/versions.rb +1 -1
  38. data/lib/pact_broker/api/resources/webhooks.rb +1 -1
  39. data/lib/pact_broker/app.rb +0 -2
  40. data/lib/pact_broker/configuration.rb +1 -4
  41. data/lib/pact_broker/doc/views/latest-verifications.markdown +7 -0
  42. data/lib/pact_broker/doc/views/publish-verification.markdown +14 -0
  43. data/lib/pact_broker/domain.rb +1 -1
  44. data/lib/pact_broker/domain/order_versions.rb +5 -15
  45. data/lib/pact_broker/domain/pact.rb +10 -2
  46. data/lib/pact_broker/domain/relationship.rb +31 -4
  47. data/lib/pact_broker/domain/verification.rb +77 -0
  48. data/lib/pact_broker/domain/version.rb +6 -1
  49. data/lib/pact_broker/domain/webhook.rb +1 -0
  50. data/lib/pact_broker/domain/webhook_request.rb +4 -6
  51. data/lib/pact_broker/locale/en.yml +2 -1
  52. data/lib/pact_broker/logging.rb +0 -4
  53. data/lib/pact_broker/pacticipants/repository.rb +1 -2
  54. data/lib/pact_broker/pacticipants/service.rb +11 -5
  55. data/lib/pact_broker/pacts/all_pact_publications.rb +96 -0
  56. data/lib/pact_broker/pacts/all_pacts.rb +4 -89
  57. data/lib/pact_broker/pacts/diff.rb +2 -2
  58. data/lib/pact_broker/pacts/latest_pact_publications.rb +11 -0
  59. data/lib/pact_broker/pacts/latest_pact_publications_by_consumer_version.rb +11 -0
  60. data/lib/pact_broker/pacts/latest_pacts.rb +1 -0
  61. data/lib/pact_broker/pacts/latest_tagged_pact_publications.rb +11 -0
  62. data/lib/pact_broker/pacts/pact_params.rb +11 -1
  63. data/lib/pact_broker/pacts/pact_publication.rb +50 -0
  64. data/lib/pact_broker/pacts/pact_version.rb +39 -0
  65. data/lib/pact_broker/pacts/repository.rb +46 -36
  66. data/lib/pact_broker/pacts/service.rb +2 -2
  67. data/lib/pact_broker/repositories.rb +5 -0
  68. data/lib/pact_broker/services.rb +5 -0
  69. data/lib/pact_broker/ui/view_models/relationship.rb +38 -0
  70. data/lib/pact_broker/ui/views/relationships/show.haml +9 -0
  71. data/lib/pact_broker/verifications/repository.rb +45 -0
  72. data/lib/pact_broker/verifications/service.rb +40 -0
  73. data/lib/pact_broker/version.rb +1 -1
  74. data/lib/pact_broker/versions/parse_semantic_version.rb +9 -6
  75. data/lib/pact_broker/webhooks/service.rb +8 -9
  76. data/lib/pact_broker/webhooks/webhook.rb +1 -1
  77. data/pact_broker.gemspec +3 -4
  78. data/pact_broker_client-pact_broker.json +4 -4
  79. data/script/publish.sh +2 -2
  80. data/script/record_verification.sh +3 -0
  81. data/spec/features/delete_pact_spec.rb +1 -1
  82. data/spec/features/get_verifications_for_consumer_version_spec.rb +33 -0
  83. data/spec/features/publish_verification_spec.rb +28 -0
  84. data/spec/fixtures/a_consumer-a_provider-2.json +1 -1
  85. data/spec/fixtures/a_consumer-a_provider-3.json +1 -1
  86. data/spec/fixtures/a_consumer-a_provider-conflict.json +1 -1
  87. data/spec/fixtures/a_consumer-a_provider-merged.json +2 -2
  88. data/spec/fixtures/a_consumer-a_provider.json +1 -1
  89. data/spec/fixtures/consumer-provider.json +1 -1
  90. data/spec/fixtures/renderer_pact.json +1 -1
  91. data/spec/fixtures/verification.json +4 -0
  92. data/spec/lib/pact_broker/api/contracts/verification_contract_spec.rb +91 -0
  93. data/spec/lib/pact_broker/api/decorators/latest_pact_decorator_spec.rb +0 -18
  94. data/spec/lib/pact_broker/api/decorators/pact_decorator_spec.rb +6 -2
  95. data/spec/lib/pact_broker/api/decorators/pact_version_decorator_spec.rb +0 -3
  96. data/spec/lib/pact_broker/api/decorators/verification_decorator_spec.rb +61 -0
  97. data/spec/lib/pact_broker/api/decorators/verifications_decorator_spec.rb +42 -0
  98. data/spec/lib/pact_broker/api/decorators/version_decorator_spec.rb +4 -0
  99. data/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb +1 -1
  100. data/spec/lib/pact_broker/api/resources/latest_verifications_for_consumer_version_spec.rb +68 -0
  101. data/spec/lib/pact_broker/api/resources/pact_spec.rb +1 -1
  102. data/spec/lib/pact_broker/api/resources/pacticipants_spec.rb +1 -1
  103. data/spec/lib/pact_broker/api/resources/verifications_spec.rb +92 -0
  104. data/spec/lib/pact_broker/api/resources/webhooks_spec.rb +1 -1
  105. data/spec/lib/pact_broker/domain/order_versions_spec.rb +10 -30
  106. data/spec/lib/pact_broker/domain/verification_spec.rb +41 -0
  107. data/spec/lib/pact_broker/domain/version_spec.rb +26 -0
  108. data/spec/lib/pact_broker/domain/webhook_request_spec.rb +1 -3
  109. data/spec/lib/pact_broker/pacticipants/repository_spec.rb +0 -16
  110. data/spec/lib/pact_broker/pacticipants/service_spec.rb +4 -3
  111. data/spec/lib/pact_broker/pacts/pact_params_spec.rb +14 -1
  112. data/spec/lib/pact_broker/pacts/pact_version_spec.rb +50 -0
  113. data/spec/lib/pact_broker/pacts/repository_spec.rb +85 -36
  114. data/spec/lib/pact_broker/ui/view_models/relationship_spec.rb +48 -2
  115. data/spec/lib/pact_broker/verifications/repository_spec.rb +92 -0
  116. data/spec/lib/pact_broker/verifications/service_spec.rb +61 -0
  117. data/spec/lib/pact_broker/versions/parse_semantic_version_spec.rb +39 -0
  118. data/spec/lib/pact_broker/webhooks/service_spec.rb +3 -40
  119. data/spec/migrations/23_pact_versions_spec.rb +95 -0
  120. data/spec/migrations/24_populate_pact_contents_spec.rb +48 -0
  121. data/spec/migrations/34_latest_tagged_pacts_spec.rb +108 -0
  122. data/spec/migrations/34_pact_revisions_spec.rb +106 -0
  123. data/spec/support/database_cleaner.rb +8 -4
  124. data/spec/support/provider_state_builder.rb +22 -8
  125. metadata +81 -36
  126. data/lib/pact_broker/pacts/database_model.rb +0 -35
  127. data/lib/pact_broker/pacts/latest_tagged_pacts.rb +0 -11
  128. data/lib/pact_broker/pacts/pact_version_content.rb +0 -11
  129. data/lib/pact_broker/webhooks/job.rb +0 -46
  130. data/script/foo-bar.json +0 -22
  131. data/script/publish-new.sh +0 -7
  132. data/script/recreate-pg-db.sh +0 -7
  133. data/spec/lib/pact_broker/webhooks/job_spec.rb +0 -67
@@ -1,96 +1,11 @@
1
- require 'pact_broker/domain/tag'
2
- require 'pact_broker/domain/pacticipant'
3
- require 'pact_broker/domain/version'
4
- require 'pact_broker/pacts/pact_version_content'
5
- require 'pact_broker/repositories/helpers'
1
+ require 'pact_broker/pacts/all_pact_publications'
6
2
 
7
3
  module PactBroker
8
4
  module Pacts
9
5
 
10
- class AllPacts < Sequel::Model(:all_pacts)
11
-
12
- set_primary_key :id
13
- associate(:one_to_many, :tags, :class => "PactBroker::Domain::Tag", :reciprocal => :version, :key => :version_id, :primary_key => :consumer_version_id)
14
- associate(:many_to_one, :pact_version_content, :key => :pact_version_content_sha, :primary_key => :sha)
15
-
16
- dataset_module do
17
- # def latest_versions
18
- # join(:latest_pact_consumer_version_orders,
19
- # {
20
- # consumer_id: :consumer_id,
21
- # provider_id: :provider_id,
22
- # latest_consumer_version_order: :consumer_version_order
23
- # },
24
- # {table_alias: :lp}
25
- # )
26
- # end
27
- include PactBroker::Repositories::Helpers
28
-
29
- def consumer consumer_name
30
- where(name_like(:consumer_name, consumer_name))
31
- end
32
-
33
- def provider provider_name
34
- where(name_like(:provider_name, provider_name))
35
- end
36
-
37
- def tag tag_name
38
- filter = name_like(Sequel.qualify(:tags, :name), tag_name)
39
- join(:tags, {version_id: :consumer_version_id}).where(filter)
40
- end
41
-
42
- def consumer_version_number number
43
- where(name_like(:consumer_version_number, number))
44
- end
45
-
46
- def consumer_version_order_before order
47
- where('consumer_version_order < ?', order)
48
- end
49
-
50
- def consumer_version_order_after order
51
- where('consumer_version_order > ?', order)
52
- end
53
-
54
- def latest
55
- reverse_order(:consumer_version_order).limit(1)
56
- end
57
-
58
- def earliest
59
- order(:consumer_version_order).limit(1)
60
- end
61
- end
62
-
63
- def to_domain
64
- domain = to_domain_without_tags
65
- domain.consumer_version.tags = tags
66
- domain
67
- end
68
-
69
- def to_domain_without_tags
70
- consumer = Domain::Pacticipant.new(name: consumer_name)
71
- consumer.id = consumer_id
72
- provider = Domain::Pacticipant.new(name: provider_name)
73
- provider.id = provider_id
74
- consumer_version = OpenStruct.new(
75
- number: consumer_version_number,
76
- order: consumer_version_order,
77
- pacticipant: consumer,
78
- tags: nil)
79
- Domain::Pact.new(id: id,
80
- consumer: consumer,
81
- consumer_version: consumer_version,
82
- provider: provider,
83
- consumer_version_number: consumer_version_number,
84
- created_at: created_at,
85
- updated_at: updated_at)
86
- end
87
-
88
- def to_domain_with_content
89
- to_domain.tap do | pact |
90
- pact.json_content = pact_version_content.content
91
- end
92
- end
93
-
6
+ # See /DEVELOPER_DOCUMENTATION.md for all_pacts view
7
+ class AllPacts < AllPactPublications
8
+ set_dataset(:all_pacts)
94
9
  end
95
10
 
96
11
  end
@@ -62,7 +62,7 @@ module PactBroker
62
62
  attr_reader :pact, :previous_distinct_pact, :next_pact, :base_url
63
63
 
64
64
  def change_date_in_words
65
- DateHelper.local_date_in_words next_pact.updated_at
65
+ DateHelper.local_date_in_words next_pact.created_at
66
66
  end
67
67
 
68
68
  def now
@@ -100,7 +100,7 @@ module PactBroker
100
100
  end
101
101
 
102
102
  def change_date_ago_in_words
103
- DateHelper.distance_of_time_in_words next_pact.updated_at, now
103
+ DateHelper.distance_of_time_in_words next_pact.created_at, now
104
104
  end
105
105
  end
106
106
  end
@@ -0,0 +1,11 @@
1
+ require 'pact_broker/pacts/latest_pact_publications_by_consumer_version'
2
+
3
+ module PactBroker
4
+ module Pacts
5
+
6
+ class LatestPactPublications < LatestPactPublicationsByConsumerVersion
7
+ set_dataset(:latest_pact_publications)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'pact_broker/pacts/all_pact_publications'
2
+
3
+ module PactBroker
4
+ module Pacts
5
+
6
+ class LatestPactPublicationsByConsumerVersion < AllPactPublications
7
+ set_dataset(:latest_pact_publications_by_consumer_versions)
8
+ end
9
+
10
+ end
11
+ end
@@ -3,6 +3,7 @@ require 'pact_broker/pacts/all_pacts'
3
3
  module PactBroker
4
4
  module Pacts
5
5
 
6
+ # See /DEVELOPER_DOCUMENTATION.md for latest_pacts view
6
7
  class LatestPacts < AllPacts
7
8
  set_dataset(:latest_pacts)
8
9
  end
@@ -0,0 +1,11 @@
1
+ require 'pact_broker/pacts/latest_pact_publications_by_consumer_version'
2
+
3
+ module PactBroker
4
+ module Pacts
5
+
6
+ class LatestTaggedPactPublications < LatestPactPublicationsByConsumerVersion
7
+ set_dataset(:latest_tagged_pact_publications)
8
+ end
9
+
10
+ end
11
+ end
@@ -24,7 +24,9 @@ module PactBroker
24
24
  new(
25
25
  consumer_name: path_info.fetch(:consumer_name),
26
26
  provider_name: path_info.fetch(:provider_name),
27
- consumer_version_number: path_info.fetch(:consumer_version_number),
27
+ revision_number: path_info[:revision_number],
28
+ consumer_version_number: path_info[:consumer_version_number],
29
+ pact_version_sha: path_info[:pact_version_sha],
28
30
  consumer_name_in_pact: consumer_name_in_pact,
29
31
  provider_name_in_pact: provider_name_in_pact,
30
32
  json_content: json_content
@@ -47,6 +49,14 @@ module PactBroker
47
49
  self[:consumer_version_number]
48
50
  end
49
51
 
52
+ def pact_version_sha
53
+ self[:pact_version_sha]
54
+ end
55
+
56
+ def revision_number
57
+ self[:revision_number]
58
+ end
59
+
50
60
  def json_content
51
61
  self[:json_content]
52
62
  end
@@ -0,0 +1,50 @@
1
+ require 'pact_broker/domain/pact'
2
+ require 'pact_broker/pacts/pact_version'
3
+
4
+ module PactBroker
5
+ module Pacts
6
+
7
+ class PactPublication < Sequel::Model(:pact_publications)
8
+
9
+ extend Forwardable
10
+
11
+ delegate [:consumer, :consumer_version_number, :name, :provider_name, :consumer_name] => :cached_domain_for_delegation
12
+
13
+ set_primary_key :id
14
+ associate(:many_to_one, :provider, :class => "PactBroker::Domain::Pacticipant", :key => :provider_id, :primary_key => :id)
15
+ associate(:many_to_one, :consumer_version, :class => "PactBroker::Domain::Version", :key => :consumer_version_id, :primary_key => :id)
16
+ associate(:many_to_one, :pact_version, class: "PactBroker::Pacts::PactVersion", :key => :pact_version_id, :primary_key => :id)
17
+
18
+ PactPublication.plugin :timestamps, :update_on_create=>true
19
+
20
+ def before_create
21
+ super
22
+ self.revision_number ||= 1
23
+ end
24
+
25
+ def to_domain
26
+ PactBroker::Domain::Pact.new(
27
+ id: id,
28
+ provider: provider,
29
+ consumer: consumer_version.pacticipant,
30
+ consumer_version_number: consumer_version.number,
31
+ consumer_version: to_version_domain,
32
+ revision_number: revision_number,
33
+ json_content: pact_version.content,
34
+ pact_version_sha: pact_version.sha,
35
+ created_at: created_at
36
+ )
37
+ end
38
+
39
+ def to_version_domain
40
+ OpenStruct.new(number: consumer_version.number, pacticipant: consumer_version.pacticipant, tags: consumer_version.tags, order: consumer_version.order)
41
+ end
42
+
43
+ private
44
+
45
+ def cached_domain_for_delegation
46
+ @domain_object ||= to_domain
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,39 @@
1
+ require 'sequel'
2
+
3
+ module PactBroker
4
+ module Pacts
5
+ class PactVersion < Sequel::Model(:pact_versions)
6
+ one_to_many :pact_publications, :reciprocal => :pact_version
7
+
8
+ def name
9
+ "Pact between #{consumer_name} and #{provider_name}"
10
+ end
11
+
12
+ def provider_name
13
+ pact_publications.last.provider.name
14
+ end
15
+
16
+ def consumer_name
17
+ pact_publications.last.consumer.name
18
+ end
19
+
20
+ def latest_consumer_version
21
+ consumer_versions.last
22
+ end
23
+
24
+ def latest_pact_publication
25
+ latest_consumer_version.latest_pact_publication
26
+ end
27
+
28
+ def consumer_versions
29
+ PactBroker::Domain::Version.where(id: PactBroker::Pacts::PactPublication.select(:consumer_version_id).where(pact_version_id: id)).order(:order)
30
+ end
31
+
32
+ def latest_consumer_version_number
33
+ latest_consumer_version.number
34
+ end
35
+ end
36
+
37
+ PactVersion.plugin :timestamps
38
+ end
39
+ end
@@ -2,11 +2,13 @@ require 'digest/sha1'
2
2
  require 'sequel'
3
3
  require 'ostruct'
4
4
  require 'pact_broker/logging'
5
- require 'pact_broker/pacts/database_model'
6
- require 'pact_broker/pacts/all_pacts'
7
- require 'pact_broker/pacts/latest_pacts'
8
- require 'pact_broker/pacts/latest_tagged_pacts'
5
+ require 'pact_broker/pacts/pact_publication'
6
+ require 'pact_broker/pacts/all_pact_publications'
7
+ require 'pact_broker/pacts/latest_pact_publications_by_consumer_version'
8
+ require 'pact_broker/pacts/latest_pact_publications'
9
+ require 'pact_broker/pacts/latest_tagged_pact_publications'
9
10
  require 'pact/shared/json_differ'
11
+ require 'pact_broker/domain'
10
12
 
11
13
  module PactBroker
12
14
  module Pacts
@@ -15,31 +17,39 @@ module PactBroker
15
17
  include PactBroker::Logging
16
18
 
17
19
  def create params
18
- DatabaseModel.new(
19
- version_id: params[:version_id],
20
+ PactPublication.new(
21
+ consumer_version_id: params[:version_id],
20
22
  provider_id: params[:provider_id],
21
- pact_version_content: find_or_create_pact_version_content(params[:json_content]),
23
+ pact_version: find_or_create_pact_version(params.fetch(:consumer_id), params.fetch(:provider_id), params[:json_content]),
22
24
  ).save.to_domain
23
25
  end
24
26
 
25
27
  def update id, params
26
- DatabaseModel.find(id: id).tap do | pact |
27
- pact_version_content = find_or_create_pact_version_content(params[:json_content])
28
- pact.update(pact_version_content: pact_version_content)
29
- end.to_domain
28
+ existing_model = PactPublication.find(id: id)
29
+ pact_version = find_or_create_pact_version(existing_model.consumer_version.pacticipant_id, existing_model.provider_id, params[:json_content])
30
+ if existing_model.pact_version_id != pact_version.id
31
+ PactPublication.new(
32
+ consumer_version_id: existing_model.consumer_version_id,
33
+ provider_id: existing_model.provider_id,
34
+ revision_number: (existing_model.revision_number + 1),
35
+ pact_version: pact_version,
36
+ ).save.to_domain
37
+ else
38
+ existing_model.to_domain
39
+ end
30
40
  end
31
41
 
32
42
  def delete params
33
- id = AllPacts
43
+ id = AllPactPublications
34
44
  .consumer(params.consumer_name)
35
45
  .provider(params.provider_name)
36
46
  .consumer_version_number(params.consumer_version_number)
37
- .limit(1).first.id
38
- DatabaseModel.where(id: id).delete
47
+ .select(:id)
48
+ PactPublication.where(id: id).delete
39
49
  end
40
50
 
41
51
  def find_all_pact_versions_between consumer_name, options
42
- AllPacts
52
+ LatestPactPublicationsByConsumerVersion
43
53
  .eager(:tags)
44
54
  .consumer(consumer_name)
45
55
  .provider(options.fetch(:and))
@@ -49,42 +59,43 @@ module PactBroker
49
59
 
50
60
  def find_latest_pact_versions_for_provider provider_name, tag = nil
51
61
  if tag
52
- LatestTaggedPacts.provider(provider_name).where(tag_name: tag).collect(&:to_domain)
62
+ LatestTaggedPactPublications.provider(provider_name).where(tag_name: tag).collect(&:to_domain)
53
63
  else
54
- LatestPacts.provider(provider_name).collect(&:to_domain)
64
+ LatestPactPublications.provider(provider_name).collect(&:to_domain)
55
65
  end
56
66
  end
57
67
 
58
68
  def find_by_version_and_provider version_id, provider_id
59
- AllPacts
69
+ LatestPactPublicationsByConsumerVersion
60
70
  .eager(:tags)
61
71
  .where(consumer_version_id: version_id, provider_id: provider_id)
62
72
  .limit(1).collect(&:to_domain_with_content)[0]
63
73
  end
64
74
 
65
75
  def find_latest_pacts
66
- LatestPacts.collect(&:to_domain_without_tags)
76
+ LatestPactPublications.collect(&:to_domain_without_tags)
67
77
  end
68
78
 
69
79
  def find_latest_pact(consumer_name, provider_name, tag = nil)
70
- query = AllPacts
80
+ query = LatestPactPublicationsByConsumerVersion
71
81
  .consumer(consumer_name)
72
82
  .provider(provider_name)
73
83
  query = query.tag(tag) unless tag.nil?
74
84
  query.latest.all.collect(&:to_domain_with_content)[0]
75
85
  end
76
86
 
77
- def find_pact consumer_name, consumer_version, provider_name
78
- AllPacts
87
+ def find_pact consumer_name, consumer_version, provider_name, pact_version_sha = nil
88
+ query = pact_version_sha ? AllPactPublications.pact_version_sha(pact_version_sha) : LatestPactPublicationsByConsumerVersion
89
+ query = query
79
90
  .eager(:tags)
80
91
  .consumer(consumer_name)
81
92
  .provider(provider_name)
82
- .consumer_version_number(consumer_version)
83
- .limit(1).collect(&:to_domain_with_content)[0]
93
+ query = query.consumer_version_number(consumer_version) if consumer_version
94
+ query.limit(1).collect(&:to_domain_with_content)[0]
84
95
  end
85
96
 
86
97
  def find_previous_pact pact
87
- AllPacts
98
+ LatestPactPublicationsByConsumerVersion
88
99
  .eager(:tags)
89
100
  .consumer(pact.consumer.name)
90
101
  .provider(pact.provider.name)
@@ -93,7 +104,7 @@ module PactBroker
93
104
  end
94
105
 
95
106
  def find_next_pact pact
96
- AllPacts
107
+ LatestPactPublicationsByConsumerVersion
97
108
  .eager(:tags)
98
109
  .consumer(pact.consumer.name)
99
110
  .provider(pact.provider.name)
@@ -114,18 +125,18 @@ module PactBroker
114
125
 
115
126
  def find_previous_distinct_pact_by_sha pact
116
127
  current_pact_content_sha =
117
- AllPacts.select(:pact_version_content_sha)
128
+ LatestPactPublicationsByConsumerVersion.select(:pact_version_sha)
118
129
  .consumer(pact.consumer.name)
119
130
  .provider(pact.provider.name)
120
131
  .consumer_version_number(pact.consumer_version_number)
121
132
  .limit(1)
122
133
 
123
- AllPacts
134
+ LatestPactPublicationsByConsumerVersion
124
135
  .eager(:tags)
125
136
  .consumer(pact.consumer.name)
126
137
  .provider(pact.provider.name)
127
138
  .consumer_version_order_before(pact.consumer_version.order)
128
- .where('pact_version_content_sha != ?', current_pact_content_sha)
139
+ .where('pact_version_sha != ?', current_pact_content_sha)
129
140
  .latest
130
141
  .collect(&:to_domain_with_content)[0]
131
142
  end
@@ -134,16 +145,15 @@ module PactBroker
134
145
  Pact::JsonDiffer.(pact.content_hash, other_pact.content_hash, allow_unexpected_keys: false).any?
135
146
  end
136
147
 
137
- def find_or_create_pact_version_content json_content
148
+ def find_or_create_pact_version consumer_id, provider_id, json_content
138
149
  sha = Digest::SHA1.hexdigest(json_content)
139
- PactVersionContent.find(sha: sha) || create_pact_version_content(sha, json_content)
150
+ PactVersion.find(sha: sha, consumer_id: consumer_id, provider_id: provider_id) || create_pact_version(consumer_id, provider_id, sha, json_content)
140
151
  end
141
152
 
142
- def create_pact_version_content sha, json_content
143
- PactBroker.logger.debug("Creating new PactVersionContent for sha #{sha}")
144
- pact_version_content = PactVersionContent.new(content: json_content)
145
- pact_version_content[:sha] = sha
146
- pact_version_content.save
153
+ def create_pact_version consumer_id, provider_id, sha, json_content
154
+ PactBroker.logger.debug("Creating new PactVersion for sha #{sha}")
155
+ pact_version = PactVersion.new(consumer_id: consumer_id, provider_id: provider_id, sha: sha, content: json_content)
156
+ pact_version.save
147
157
  end
148
158
 
149
159
  end