@@ -15,6 +15,8 @@ class Repository
15
15
16
16
# TODO move latest verification logic in to database
17
17
18
+ TP_COLS = PactBroker ::Matrix ::Row ::TP_COLS
19
+
18
20
GROUP_BY_PROVIDER_VERSION_NUMBER = [ :consumer_name , :consumer_version_number , :provider_name , :provider_version_number ]
19
21
GROUP_BY_PROVIDER = [ :consumer_name , :consumer_version_number , :provider_name ]
20
22
GROUP_BY_PACT = [ :consumer_name , :provider_name ]
@@ -38,26 +40,41 @@ def find selectors, options = {}
38
40
lines . sort
39
41
end
40
42
41
- def apply_latestby options , selectors , lines
42
- return lines unless options [ :latestby ] == 'cvp' || options [ :latestby ] == 'cp'
43
43
44
+
45
+ def apply_latestby options , selectors , lines
46
+ return lines unless options [ :latestby ]
44
47
group_by_columns = case options [ :latestby ]
48
+ when 'cvpv' then GROUP_BY_PROVIDER_VERSION_NUMBER
45
49
when 'cvp' then GROUP_BY_PROVIDER
46
50
when 'cp' then GROUP_BY_PACT
47
51
end
48
52
49
53
# The group with the nil provider_version_numbers will be the results of the left outer join
50
54
# that don't have verifications, so we need to include them all.
51
- lines . group_by { |line | group_by_columns . collect { |key | line . send ( key ) } }
55
+ remove_overwritten_revisions ( lines ) . group_by { |line | group_by_columns . collect { |key | line . send ( key ) } }
52
56
. values
53
57
. collect { | lines | lines . first . provider_version_number . nil? ? lines : lines . first }
54
58
. flatten
55
59
end
56
60
61
+ def remove_overwritten_revisions lines
62
+ latest_revisions_keys = { }
63
+ latest_revisions = [ ]
64
+ lines . each do | line |
65
+ key = "#{ line . consumer_name } -#{ line . provider_name } -#{ line . consumer_version_number } "
66
+ if !latest_revisions_keys . key? ( key ) || latest_revisions_keys [ key ] == line . pact_revision_number
67
+ latest_revisions << line
68
+ latest_revisions_keys [ key ] ||= line . pact_revision_number
69
+ end
70
+ end
71
+ latest_revisions
72
+ end
73
+
57
74
def find_for_consumer_and_provider pacticipant_1_name , pacticipant_2_name
58
75
selectors = [ { pacticipant_name : pacticipant_1_name } , { pacticipant_name : pacticipant_2_name } ]
59
76
options = { latestby : 'cvpv' }
60
- query_matrix ( resolve_selectors ( selectors , options ) , options ) . sort
77
+ find ( selectors , options )
61
78
end
62
79
63
80
def find_compatible_pacticipant_versions selectors
@@ -75,7 +92,7 @@ def query_matrix selectors, options
75
92
end
76
93
77
94
def view_for ( options )
78
- options [ :latestby ] ? LatestRow : Row
95
+ Row
79
96
end
80
97
81
98
def resolve_selectors ( selectors , options )
0 commit comments