3
3
require 'pact_broker/domain/index_item'
4
4
require 'pact_broker/matrix/latest_row'
5
5
require 'pact_broker/matrix/actual_latest_row'
6
+ require 'pact_broker/matrix/head_row'
6
7
7
8
module PactBroker
8
9
@@ -13,63 +14,61 @@ class Service
13
14
extend PactBroker ::Services
14
15
extend PactBroker ::Logging
15
16
17
+ # Used when using table_print to output query results
18
+ TP_COLS = [ :consumer_name , :consumer_version_number , :consumer_version_id , :provider_name , :provider_id , :provider_version_number ]
19
+
16
20
def self . find_index_items options = { }
17
21
rows = [ ]
22
+ overall_latest_publication_ids = nil
18
23
19
- if !options [ :tags ]
20
- rows = PactBroker ::Matrix ::ActualLatestRow
24
+ rows = PactBroker ::Matrix ::HeadRow
21
25
. select_all_qualified
22
26
. eager ( :latest_triggered_webhooks )
23
27
. eager ( :webhooks )
24
28
. order ( :consumer_name , :provider_name )
25
29
. eager ( :consumer_version_tags )
26
30
. eager ( :provider_version_tags )
27
- . all
31
+
32
+ if !options [ :tags ]
33
+ rows = rows . where ( consumer_tag_name : nil ) . all
34
+ overall_latest_publication_ids = rows . collect ( &:pact_publication_id )
28
35
end
29
36
30
37
if options [ :tags ]
31
- tagged_rows = PactBroker ::Matrix ::Row
32
- . select_all_qualified
33
- . select_append ( Sequel [ :head_pact_publications ] [ :tag_name ] )
34
- . join ( :head_pact_publications , { consumer_id : :consumer_id , provider_id : :provider_id , consumer_version_order : :consumer_version_order } )
35
- . eager ( :latest_triggered_webhooks )
36
- . eager ( :webhooks )
37
- . order ( :consumer_name , :provider_name )
38
- . eager ( :consumer_version_tags )
39
- . eager ( :provider_version_tags )
40
-
41
- if options [ :tags ] . is_a? ( Array )
42
- tagged_rows = tagged_rows . where ( Sequel [ :head_pact_publications ] [ :tag_name ] => options [ :tags ] ) . or ( Sequel [ :head_pact_publications ] [ :tag_name ] => nil )
43
- end
44
-
45
- tagged_rows = tagged_rows . all
46
- . group_by ( &:pact_publication_id )
47
- . values
48
- . collect { |group | [ group . last , group . collect { |r | r [ :tag_name ] } . compact ] }
49
- . collect { |( row , tag_names ) | row . consumer_head_tag_names = tag_names ; row }
50
-
51
- rows = tagged_rows
38
+ if options [ :tags ] . is_a? ( Array )
39
+ rows = rows . where ( consumer_tag_name : options [ :tags ] ) . or ( consumer_tag_name : nil )
40
+ end
41
+
42
+ rows = rows . all
43
+ overall_latest_publication_ids = rows . select { |r | !r [ :consumer_tag_name ] } . collect ( &:pact_publication_id ) . uniq
44
+
45
+ # Smoosh all the rows with matching pact publications together
46
+ # and collect their consumer_head_tag_names
47
+ rows = rows
48
+ . group_by ( &:pact_publication_id )
49
+ . values
50
+ . collect { |group | [ group . last , group . collect { |r | r [ :consumer_tag_name ] } . compact ] }
51
+ . collect { |( row , tag_names ) | row . consumer_head_tag_names = tag_names ; row }
52
52
end
53
53
54
54
index_items = [ ]
55
55
rows . sort . each do | row |
56
56
tag_names = [ ]
57
57
if options [ :tags ]
58
58
tag_names = row . consumer_version_tags . collect ( &:name )
59
- if options [ :tags ] . is_a? ( Array )
60
- tag_names = tag_names & options [ :tags ]
61
- end
62
59
end
63
- previous_index_item_for_same_consumer_and_provider = index_items . last && index_items . last . consumer_name == row . consumer_name && index_items . last . provider_name == row . provider_name
64
- index_items << PactBroker ::Domain ::IndexItem . create ( row . consumer , row . provider ,
60
+
61
+ index_items << PactBroker ::Domain ::IndexItem . create (
62
+ row . consumer ,
63
+ row . provider ,
65
64
row . pact ,
66
- ! previous_index_item_for_same_consumer_and_provider ,
65
+ overall_latest_publication_ids . include? ( row . pact_publication_id ) ,
67
66
row . latest_verification ,
68
67
row . webhooks ,
69
68
row . latest_triggered_webhooks ,
70
- tag_names ,
69
+ row . consumer_head_tag_names ,
71
70
row . provider_version_tags . select ( &:latest? )
72
- )
71
+ )
73
72
end
74
73
75
74
index_items
0 commit comments