From c8cf3314fd25a9d6855f9cebdf4d8e31292129b2 Mon Sep 17 00:00:00 2001 From: Ronald Holshausen Date: Fri, 12 Jul 2024 11:26:04 +1000 Subject: [PATCH] fix: PactBrokerClient throws index-out-of-bounds when can-i-deploy is called for a new tag #1814 --- .../pact/core/pactbroker/PactBrokerClient.kt | 2 +- .../pactbroker/PactBrokerClientSpec.groovy | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt index f8acec1e5d..62cafc4183 100644 --- a/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt +++ b/core/pactbroker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt @@ -1008,7 +1008,7 @@ open class PactBrokerClient( is Ok -> { val summary = result.value["summary"].asObject()!! val matrix = result.value["matrix"] - val verificationResultUrl = if (matrix.isArray) { + val verificationResultUrl = if (matrix.isArray && matrix.size() > 0) { matrix.asArray() ?.get(0)?.asObject() ?.get("verificationResult")?.asObject() diff --git a/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy b/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy index 059b40035b..15079ba713 100644 --- a/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy +++ b/core/pactbroker/src/test/groovy/au/com/dius/pact/core/pactbroker/PactBrokerClientSpec.groovy @@ -792,4 +792,39 @@ class PactBrokerClientSpec extends Specification { result.ok result.verificationResultUrl == 'verificationResultUrl' } + + @Issue('#1814') + def 'can-i-deploy - handles an empty matrix response'() { + given: + def halClient = Mock(IHalClient) + def config = new PactBrokerClientConfig(10, 0) + PactBrokerClient client = Spy(PactBrokerClient, constructorArgs: ['baseUrl', [:], config]) { + newHalClient() >> halClient + } + def json = JsonParser.parseString(''' + |{ + | "summary": { + | "deployable": true, + | "reason": "There are no missing dependencies", + | "success": 0, + | "failed": 0, + | "unknown": 0 + | }, + | "notices": [ + | { + | "type": "success", + | "text": "There are no missing dependencies" + | } + | ], + | "matrix": [] + |}'''.stripMargin()) + + when: + def result = client.canIDeploy('test', '1.2.3', new Latest.UseLatest(true), '') + + then: + 1 * halClient.getJson(_, _) >> new Ok(json) + result.ok + !result.verificationResultUrl + } }