Skip to content

Commit

Permalink
fix: PactBrokerClient throws index-out-of-bounds when can-i-deploy is…
Browse files Browse the repository at this point in the history
… called for a new tag #1814
  • Loading branch information
rholshausen committed Jul 12, 2024
1 parent 65ae785 commit 877f21b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1037,13 +1037,18 @@ open class PactBrokerClient(
when (val result = halClient.getJson(path, false)) {
is Result.Ok<JsonValue> -> {
val summary: JsonValue.Object = result.value["summary"].downcast()
val verificationResultUrl = result.value["matrix"].asArray()
?.get(0)?.asObject()
?.get("verificationResult")?.asObject()
?.get("_links")?.asObject()
?.get("self")?.asObject()
?.get("href")
?.let{ url -> Json.toString(url) }
val matrix = result.value["matrix"]
val verificationResultUrl = if (matrix.isArray && matrix.size() > 0) {
result.value["matrix"].asArray()
?.get(0)?.asObject()
?.get("verificationResult")?.asObject()
?.get("_links")?.asObject()
?.get("self")?.asObject()
?.get("href")
?.let{ url -> Json.toString(url) }
} else {
null
}
CanIDeployResult(Json.toBoolean(summary["deployable"]), "", Json.toString(summary["reason"]),
Json.toInteger(summary["unknown"]), verificationResultUrl)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -886,4 +886,39 @@ class PactBrokerClientSpec extends Specification {
1 * halClient.postJson('pb:provider-pacts-for-verification', [provider: 'provider'], expectedJson) >> new Result.Ok(jsonResult)
result instanceof Result.Ok
}

@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), null)

then:
1 * halClient.getJson(_, _) >> new Result.Ok(json)
result.ok
!result.verificationResultUrl
}
}

0 comments on commit 877f21b

Please sign in to comment.