@@ -3,6 +3,7 @@ package au.com.dius.pact.core.pactbroker
3
3
import au.com.dius.pact.com.github.michaelbull.result.Err
4
4
import au.com.dius.pact.com.github.michaelbull.result.Ok
5
5
import au.com.dius.pact.com.github.michaelbull.result.Result
6
+ import au.com.dius.pact.core.support.Json
6
7
import com.github.salomonbrys.kotson.get
7
8
import com.github.salomonbrys.kotson.jsonArray
8
9
import com.github.salomonbrys.kotson.jsonObject
@@ -124,7 +125,7 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
124
125
@JvmOverloads
125
126
open fun uploadPactFile (pactFile : File , unescapedVersion : String , tags : List <String > = emptyList()): Any? {
126
127
val pactText = pactFile.readText()
127
- val pact = JsonParser ().parse (pactText)
128
+ val pact = JsonParser .parseString (pactText)
128
129
val halClient = newHalClient()
129
130
val providerName = urlPathSegmentEscaper().escape(pact[" provider" ][" name" ].string)
130
131
val consumerName = urlPathSegmentEscaper().escape(pact[" consumer" ][" name" ].string)
@@ -228,8 +229,8 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
228
229
" metadata" -> {
229
230
listOf (jsonObject(mismatch.filter { it.key != " interactionId" }
230
231
.flatMap {
231
- when {
232
- it.key == " type" -> listOf (" attribute" to it.value)
232
+ when (it.key) {
233
+ " type" -> listOf (" attribute" to it.value)
233
234
else -> listOf (" identifier" to it.key, " description" to it.value)
234
235
}
235
236
}))
@@ -302,7 +303,36 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
302
303
}
303
304
304
305
open fun canIDeploy (pacticipant : String , pacticipantVersion : String , latest : Latest , to : String? ): CanIDeployResult {
305
- return CanIDeployResult (false , " " , " " )
306
+ val halClient = newHalClient()
307
+ val result = halClient.getJson(" /matrix" + buildMatrixQuery(pacticipant, pacticipantVersion, latest, to),
308
+ false )
309
+ return when (result) {
310
+ is Ok -> {
311
+ val summary = result.value.asJsonObject[" summary" ].asJsonObject
312
+ CanIDeployResult (Json .toBoolean(summary[" deployable" ]), " " , Json .toString(summary[" reason" ]))
313
+ }
314
+ is Err -> {
315
+ logger.error(result.error) { " Pact broker matrix query failed: ${result.error.message} " }
316
+ CanIDeployResult (false , result.error.message.toString(), " " )
317
+ }
318
+ }
319
+ }
320
+
321
+ private fun buildMatrixQuery (pacticipant : String , pacticipantVersion : String , latest : Latest , to : String? ): String {
322
+ val escaper = urlPathSegmentEscaper()
323
+ var base = " ?q[][pacticipant]=${escaper.escape(pacticipant)} "
324
+ base + = when (latest) {
325
+ is Latest .UseLatest -> if (latest.latest) {
326
+ " q[][latest]=true"
327
+ } else {
328
+ " &q[][version]=${escaper.escape(pacticipantVersion)} "
329
+ }
330
+ is Latest .UseLatestTag -> " q[][tag]=${escaper.escape(latest.latestTag)} "
331
+ }
332
+ if (! to.isNullOrEmpty()) {
333
+ base + = " &latest=true&tag=${escaper.escape(to)} "
334
+ }
335
+ return base
306
336
}
307
337
308
338
companion object : KLogging () {
0 commit comments