2
2
require 'pact_broker/logging'
3
3
require 'pact_broker/domain/index_item'
4
4
require 'pact_broker/matrix/head_row'
5
+ require 'pact_broker/matrix/aggregated_row'
5
6
6
7
module PactBroker
7
8
@@ -13,107 +14,38 @@ class Service
13
14
extend PactBroker ::Logging
14
15
15
16
def self . find_index_items options = { }
16
- rows = [ ]
17
- overall_latest_publication_ids = nil
18
-
19
17
rows = PactBroker ::Matrix ::HeadRow
20
18
. select_all_qualified
21
19
. eager ( :latest_triggered_webhooks )
22
20
. eager ( :webhooks )
23
21
. order ( :consumer_name , :provider_name )
24
- . eager ( :consumer_version_tags )
25
- . eager ( :provider_version_tags )
22
+ . eager ( :verification )
26
23
27
24
if !options [ :tags ]
28
- rows = rows . where ( consumer_version_tag_name : nil ) . all
29
- overall_latest_publication_ids = rows . collect ( &:pact_publication_id )
30
- end
31
-
32
- if options [ :tags ]
25
+ rows = rows . where ( consumer_version_tag_name : nil )
26
+ else
33
27
if options [ :tags ] . is_a? ( Array )
34
28
rows = rows . where ( consumer_version_tag_name : options [ :tags ] ) . or ( consumer_version_tag_name : nil )
35
29
end
36
-
37
- rows = rows . all
38
- overall_latest_publication_ids = rows . select { |r | !r [ :consumer_version_tag_name ] } . collect ( &:pact_publication_id ) . uniq
39
-
40
- # Smoosh all the rows with matching pact publications together
41
- # and collect their consumer_head_tag_names
42
- rows = rows
43
- . group_by ( &:pact_publication_id )
44
- . values
45
- . collect { |group | [ group . last , group . collect { |r | r [ :consumer_version_tag_name ] } . compact ] }
46
- . collect { |( row , tag_names ) | row . consumer_head_tag_names = tag_names ; row }
30
+ rows = rows . eager ( :consumer_version_tags )
31
+ . eager ( :provider_version_tags )
32
+ . eager ( :latest_verification_for_consumer_version_tag )
47
33
end
34
+ rows = rows . all . group_by ( &:pact_publication_id ) . values . collect { | rows | Matrix ::AggregatedRow . new ( rows ) }
48
35
49
- index_items = [ ]
50
- rows . sort . each do | row |
51
- tag_names = [ ]
52
- if options [ :tags ]
53
- tag_names = row . consumer_version_tags . collect ( &:name )
54
- end
55
-
56
- overall_latest = overall_latest_publication_ids . include? ( row . pact_publication_id )
57
- latest_verification = if overall_latest
58
- verification_repository . find_latest_verification_for row . consumer_name , row . provider_name
59
- else
60
- tag_names . collect do | tag_name |
61
- verification_repository . find_latest_verification_for row . consumer_name , row . provider_name , tag_name
62
- end . compact . sort do | v1 , v2 |
63
- # Some provider versions have nil orders, not sure why
64
- # Sort by execution_date instead of order
65
- v1 . execution_date <=> v2 . execution_date
66
- end . last
67
- end
68
-
69
- index_items << PactBroker ::Domain ::IndexItem . create (
36
+ rows . sort . collect do | row |
37
+ PactBroker ::Domain ::IndexItem . create (
70
38
row . consumer ,
71
39
row . provider ,
72
40
row . pact ,
73
- overall_latest ,
74
- latest_verification ,
41
+ row . overall_latest? ,
42
+ options [ :tags ] ? row . latest_verification : row . verification ,
75
43
row . webhooks ,
76
44
row . latest_triggered_webhooks ,
77
- row . consumer_head_tag_names ,
78
- row . provider_version_tags . select ( &:latest? )
45
+ options [ :tags ] ? row . consumer_head_tag_names : [ ] ,
46
+ options [ :tags ] ? row . provider_version_tags . select ( &:latest? ) : [ ]
79
47
)
80
48
end
81
-
82
- index_items
83
- end
84
-
85
- def self . tags_for ( pact , options )
86
- if options [ :tags ] == true
87
- tag_service . find_all_tag_names_for_pacticipant ( pact . consumer_name )
88
- elsif options [ :tags ] . is_a? ( Array )
89
- options [ :tags ]
90
- else
91
- [ ]
92
- end
93
- end
94
-
95
- def self . build_index_item_rows ( pact , tags )
96
- index_items = [ build_latest_pact_index_item ( pact , tags ) ]
97
- tags . each do | tag |
98
- index_items << build_index_item_for_tagged_pact ( pact , tag )
99
- end
100
- index_items . compact
101
- end
102
-
103
- def self . build_latest_pact_index_item pact , tags
104
- latest_verification = verification_service . find_latest_verification_for ( pact . consumer , pact . provider )
105
- webhooks = webhook_service . find_by_consumer_and_provider pact . consumer , pact . provider
106
- triggered_webhooks = webhook_service . find_latest_triggered_webhooks pact . consumer , pact . provider
107
- tag_names = pact . consumer_version_tag_names . select { |name | tags . include? ( name ) }
108
- PactBroker ::Domain ::IndexItem . create pact . consumer , pact . provider , pact , true , latest_verification , webhooks , triggered_webhooks , tag_names
109
- end
110
-
111
- def self . build_index_item_for_tagged_pact latest_pact , tag
112
- pact = pact_service . find_latest_pact consumer_name : latest_pact . consumer_name , provider_name : latest_pact . provider_name , tag : tag
113
- return nil unless pact
114
- return nil if pact . id == latest_pact . id
115
- verification = verification_repository . find_latest_verification_for pact . consumer_name , pact . provider_name , tag
116
- PactBroker ::Domain ::IndexItem . create pact . consumer , pact . provider , pact , false , verification , [ ] , [ ] , [ tag ]
117
49
end
118
50
end
119
51
end
0 commit comments