@@ -60,7 +60,9 @@ def find specified_selectors, options = {}
60
60
lines = lines . select { |l | options [ :success ] . include? ( l . success ) }
61
61
end
62
62
63
- QueryResults . new ( lines . sort , specified_selectors , options , resolved_selectors )
63
+ integrations = find_integrations_for_specified_selectors ( resolved_selectors . select ( &:specified? ) )
64
+
65
+ QueryResults . new ( lines . sort , specified_selectors , options , resolved_selectors , integrations )
64
66
end
65
67
66
68
def find_for_consumer_and_provider pacticipant_1_name , pacticipant_2_name
@@ -73,24 +75,35 @@ def find_compatible_pacticipant_versions selectors
73
75
find ( selectors , latestby : 'cvpv' ) . select { |line | line . success }
74
76
end
75
77
76
- def find_integrations ( pacticipant_names )
77
- selectors = pacticipant_names . collect { | pacticipant_name | add_ids_to_selector ( pacticipant_name : pacticipant_name ) }
78
+ # If one pacticipant is specified, find all the integrations that involve that pacticipant
79
+ # If two or more are specified, just return the integrations that involve the specified pacticipants
80
+ def find_integrations_for_specified_selectors ( resolved_specified_selectors )
81
+ specified_pacticipant_names = resolved_specified_selectors . collect ( &:pacticipant_name )
78
82
Row
79
83
. select ( :consumer_name , :consumer_id , :provider_name , :provider_id )
80
- . matching_selectors ( selectors )
84
+ . matching_selectors ( resolved_specified_selectors )
81
85
. distinct
82
86
. all
83
87
. collect do |row |
84
88
row . to_hash
85
89
end
86
90
. uniq
87
91
. collect do | hash |
88
- Integration . from_hash ( hash . merge ( required : pacticipant_names . include? ( hash [ :consumer_name ] ) ) )
92
+ required = is_a_row_for_this_integration_required? ( specified_pacticipant_names , hash [ :consumer_name ] )
93
+ Integration . from_hash ( hash . merge ( required : required ) )
89
94
end
90
95
end
91
96
92
97
private
93
98
99
+ # If one of the specified pacticipants is a consumer, then that provider is required to be deployed
100
+ # to the same environment before the consumer can be deployed.
101
+ # If one of the specified pacticipants is a provider, then the provider may be deployed
102
+ # without the consumer being present.
103
+ def is_a_row_for_this_integration_required? ( specified_pacticipant_names , consumer_name )
104
+ specified_pacticipant_names . include? ( consumer_name )
105
+ end
106
+
94
107
def apply_latestby options , selectors , lines
95
108
return lines unless options [ :latestby ]
96
109
group_by_columns = case options [ :latestby ]
@@ -121,7 +134,7 @@ def remove_overwritten_revisions lines
121
134
end
122
135
123
136
def query_matrix selectors , options
124
- query = view_for ( options ) . select_all . matching_selectors ( selectors )
137
+ query = Row . select_all . matching_selectors ( selectors )
125
138
query = query . limit ( options [ :limit ] ) if options [ :limit ]
126
139
query
127
140
. order_by_names_ascending_most_recent_first
@@ -130,12 +143,8 @@ def query_matrix selectors, options
130
143
. all
131
144
end
132
145
133
- def view_for ( options )
134
- Row
135
- end
136
-
137
146
def resolve_selectors ( specified_selectors , options )
138
- resolved_specified_selectors = resolve_versions_and_add_ids ( specified_selectors , options )
147
+ resolved_specified_selectors = resolve_versions_and_add_ids ( specified_selectors , :specified )
139
148
if options [ :latest ] || options [ :tag ]
140
149
add_inferred_selectors ( resolved_specified_selectors , options )
141
150
else
@@ -144,42 +153,40 @@ def resolve_selectors(specified_selectors, options)
144
153
end
145
154
146
155
# Find the version number for selectors with the latest and/or tag specified
147
- def resolve_versions_and_add_ids ( selectors , options , required = true )
156
+ def resolve_versions_and_add_ids ( selectors , selector_type )
148
157
selectors . collect do | selector |
149
158
pacticipant = PactBroker ::Domain ::Pacticipant . find ( name : selector [ :pacticipant_name ] )
159
+ versions = find_versions_for_selector ( selector )
160
+ build_selectors_for_pacticipant_and_versions ( pacticipant , versions , selector , selector_type )
161
+ end . flatten
162
+ end
150
163
151
- versions = find_versions_for_selector ( selector , required )
152
-
153
- if versions
154
- versions . collect do | version |
155
- if version
156
- selector_for_version ( pacticipant , version )
157
- else
158
- selector_for_non_existing_version ( pacticipant )
159
- end
164
+ def build_selectors_for_pacticipant_and_versions ( pacticipant , versions , original_selector , selector_type )
165
+ if versions
166
+ versions . collect do | version |
167
+ if version
168
+ selector_for_version ( pacticipant , version , original_selector , selector_type )
169
+ else
170
+ selector_for_non_existing_version ( pacticipant , original_selector , selector_type )
160
171
end
161
- else
162
- selector_without_version ( pacticipant )
163
172
end
164
- end . flatten
173
+ else
174
+ selector_without_version ( pacticipant , selector_type )
175
+ end
165
176
end
166
177
167
- def find_versions_for_selector ( selector , required )
178
+ def find_versions_for_selector ( selector )
168
179
if selector [ :tag ] && selector [ :latest ]
169
180
version = version_repository . find_by_pacticipant_name_and_latest_tag ( selector [ :pacticipant_name ] , selector [ :tag ] )
170
- # raise Error.new("No version of #{selector[:pacticipant_name]} found with tag #{selector[:tag]}") if required && !version
171
181
[ version ]
172
182
elsif selector [ :latest ]
173
183
version = version_repository . find_latest_by_pacticpant_name ( selector [ :pacticipant_name ] )
174
- # raise Error.new("No version of #{selector[:pacticipant_name]} found") if required && !version
175
184
[ version ]
176
185
elsif selector [ :tag ]
177
186
versions = version_repository . find_by_pacticipant_name_and_tag ( selector [ :pacticipant_name ] , selector [ :tag ] )
178
- # raise Error.new("No version of #{selector[:pacticipant_name]} found with tag #{selector[:tag]}") if required && versions.empty?
179
187
versions . any? ? versions : [ nil ]
180
188
elsif selector [ :pacticipant_version_number ]
181
189
version = version_repository . find_by_pacticipant_name_and_number ( selector [ :pacticipant_name ] , selector [ :pacticipant_version_number ] )
182
- # raise Error.new("No version #{selector[:pacticipant_version_number]} of #{selector[:pacticipant_name]} found") if required && !version
183
190
[ version ]
184
191
else
185
192
nil
@@ -203,24 +210,17 @@ def add_ids_to_selector(selector)
203
210
selector
204
211
end
205
212
206
- # eg. when checking to see if Foo version 2 can be deployed to prod,
207
- # need to look up all the 'partner' pacticipants, and determine their latest prod versions
213
+ # When only one selector is specified, ( eg. checking to see if Foo version 2 can be deployed to prod) ,
214
+ # need to look up all integrated pacticipants, and determine their relevant (eg. latest prod) versions
208
215
def add_inferred_selectors ( resolved_specified_selectors , options )
209
- integrations = find_integrations ( resolved_specified_selectors . collect { | s | s [ :pacticipant_name ] } )
216
+ integrations = find_integrations_for_specified_selectors ( resolved_specified_selectors )
210
217
all_pacticipant_names = integrations . collect ( &:pacticipant_names ) . flatten . uniq
211
218
specified_names = resolved_specified_selectors . collect { |s | s [ :pacticipant_name ] }
212
219
inferred_pacticipant_names = all_pacticipant_names - specified_names
213
- # Inferred providers are required for a consumer to be deployed
214
- required_inferred_pacticipant_names = inferred_pacticipant_names . select { | n | integrations . any? { |i | i . involves_provider_with_name? ( n ) } }
215
- # Inferred consumers are NOT required for a provider to be deployed
216
- optional_inferred_pacticipant_names = inferred_pacticipant_names - required_inferred_pacticipant_names
217
-
218
- resolved_specified_selectors +
219
- build_inferred_selectors ( required_inferred_pacticipant_names , options , true ) +
220
- build_inferred_selectors ( optional_inferred_pacticipant_names , options , false )
220
+ resolved_specified_selectors + build_inferred_selectors ( inferred_pacticipant_names , options )
221
221
end
222
222
223
- def build_inferred_selectors ( inferred_pacticipant_names , options , required )
223
+ def build_inferred_selectors ( inferred_pacticipant_names , options )
224
224
selectors = inferred_pacticipant_names . collect do | pacticipant_name |
225
225
selector = {
226
226
pacticipant_name : pacticipant_name
@@ -229,26 +229,26 @@ def build_inferred_selectors(inferred_pacticipant_names, options, required)
229
229
selector [ :latest ] = options [ :latest ] if options [ :latest ]
230
230
selector
231
231
end
232
- resolve_versions_and_add_ids ( selectors , options , required )
232
+ resolve_versions_and_add_ids ( selectors , :inferred )
233
233
end
234
234
235
235
def all_pacticipant_names_in_specified_matrix ( selectors )
236
- find_integrations ( selectors . collect { | s | s [ :pacticipant_name ] } )
236
+ find_integrations_for_specified_selectors ( selectors )
237
237
. collect ( &:pacticipant_names )
238
238
. flatten
239
239
. uniq
240
240
end
241
241
242
- def selector_for_non_existing_version ( pacticipant )
243
- ResolvedSelector . for_pacticipant_and_non_existing_version ( pacticipant )
242
+ def selector_for_non_existing_version ( pacticipant , original_selector , selector_type )
243
+ ResolvedSelector . for_pacticipant_and_non_existing_version ( pacticipant , original_selector , selector_type )
244
244
end
245
245
246
- def selector_for_version ( pacticipant , version )
247
- ResolvedSelector . for_pacticipant_and_version ( pacticipant , version )
246
+ def selector_for_version ( pacticipant , version , original_selector , selector_type )
247
+ ResolvedSelector . for_pacticipant_and_version ( pacticipant , version , original_selector , selector_type )
248
248
end
249
249
250
- def selector_without_version ( pacticipant )
251
- ResolvedSelector . for_pacticipant ( pacticipant )
250
+ def selector_without_version ( pacticipant , selector_type )
251
+ ResolvedSelector . for_pacticipant ( pacticipant , selector_type )
252
252
end
253
253
end
254
254
end
0 commit comments