Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include verification results url output from can i deploy task #1527

Merged
merged 3 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ sealed class Latest {
/**
* Model for a CanIDeploy result
*/
data class CanIDeployResult(val ok: Boolean, val message: String, val reason: String, val unknown: Int? = null)
data class CanIDeployResult(
val ok: Boolean,
val message: String,
val reason: String,
val unknown: Int? = null,
val verificationResultUrl: String? = null
)

/**
* Consumer version selector. See https://docs.pact.io/pact_broker/advanced_topics/selectors
Expand Down Expand Up @@ -806,8 +812,15 @@ open class PactBrokerClient(
when (val result = halClient.getJson(path, false)) {
is 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) }
CanIDeployResult(Json.toBoolean(summary["deployable"]), "", Json.toString(summary["reason"]),
Json.toInteger(summary["unknown"]))
Json.toInteger(summary["unknown"]), verificationResultUrl)
}
is Err<Exception> -> {
logger.error(result.error) { "Pact broker matrix query failed: ${result.error.message}" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,4 +736,39 @@ class PactBrokerClientSpec extends Specification {
then:
1 * mockHalClient.postJson(PactBrokerClient.PUBLISH_CONTRACTS_LINK, [:], jsonBody) >> new Ok(new JsonValue.Object([:]))
}

@Issue('#1525')
def 'can-i-deploy - should return verificationResultUrl when there is one'() {
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": "some text",
| "unknown": 0
| },
| "matrix": [{
| "verificationResult": {
| "_links": {
| "self": {
| "href": "verificationResultUrl"
| }
| }
| }
| }]
|}'''.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 == 'verificationResultUrl'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ class PactCanIDeployTask extends PactCanIDeployBaseTask {
println("Computer says no ¯\\_(ツ)_/¯ ${result.message}\n\n${t.red.invoke(result.reason)}")
}

if (result.verificationResultUrl != null) {
println("VERIFICATION RESULTS\n--------------------\n1. ${result.verificationResultUrl}\n")
}

if (!result.ok) {
throw new GradleScriptException("Can you deploy? Computer says no ¯\\_(ツ)_/¯ ${result.message}", null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class PactCanIDeployTaskSpec extends Specification {
project.evaluate()

task.brokerClient = Mock(PactBrokerClient) {
canIDeploy(_, _, _, _, _) >> new CanIDeployResult(true, '', '', null)
canIDeploy(_, _, _, _, _) >> new CanIDeployResult(true, '', '', null, null)
}

when:
Expand Down Expand Up @@ -105,7 +105,31 @@ class PactCanIDeployTaskSpec extends Specification {
then:
notThrown(GradleScriptException)
1 * task.brokerClient.canIDeploy('pacticipant', '1.0.0', _, _, _) >>
new CanIDeployResult(true, '', '', null)
new CanIDeployResult(true, '', '', null, null)
}

def 'prints verification results url when pact broker client returns one'() {
given:
project.pact {
broker {
pactBrokerUrl = 'pactBrokerUrl'
}
}
project.ext.pacticipant = 'pacticipant'
project.ext.pacticipantVersion = '1.0.0'
project.ext.latest = 'true'
project.ext.toTag = 'prod'
project.evaluate()

task.brokerClient = Mock(PactBrokerClient)

when:
task.canIDeploy()

then:
notThrown(GradleScriptException)
1 * task.brokerClient.canIDeploy('pacticipant', '1.0.0',
new Latest.UseLatest(true), 'prod', _) >> new CanIDeployResult(true, '', '', null, 'verificationResultUrl')
}

def 'passes optional parameters to the pact broker client'() {
Expand All @@ -129,7 +153,7 @@ class PactCanIDeployTaskSpec extends Specification {
then:
notThrown(GradleScriptException)
1 * task.brokerClient.canIDeploy('pacticipant', '1.0.0',
new Latest.UseLatest(true), 'prod', _) >> new CanIDeployResult(true, '', '', null)
new Latest.UseLatest(true), 'prod', _) >> new CanIDeployResult(true, '', '', null, null)
}

def 'throws an exception if the pact broker client says no'() {
Expand All @@ -150,7 +174,7 @@ class PactCanIDeployTaskSpec extends Specification {

then:
1 * task.brokerClient.canIDeploy('pacticipant', '1.0.0', _, _, _) >>
new CanIDeployResult(false, 'Bad version', 'Bad version', null)
new CanIDeployResult(false, 'Bad version', 'Bad version', null, null)
def ex = thrown(GradleScriptException)
ex.message == 'Can you deploy? Computer says no ¯\\_(ツ)_/¯ Bad version'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ open class PactCanIDeployMojo : PactBaseMojo() {
println("Computer says no ¯\\_(ツ)_/¯ ${result.message}\n\n${t.red(result.reason)}")
}

if (result.verificationResultUrl != null) {
println("VERIFICATION RESULTS\n--------------------\n1. ${result.verificationResultUrl}\n")
}

if (!result.ok) {
throw MojoExecutionException("Can you deploy? Computer says no ¯\\_(ツ)_/¯ ${result.message}", null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PactCanIDeployMojoSpec extends Specification {
mojo.pacticipantVersion = null
mojo.latest = 'true'
mojo.brokerClient = Mock(PactBrokerClient) {
canIDeploy(_, _, _, _, _) >> new CanIDeployResult(true, '', '', null)
canIDeploy(_, _, _, _, _) >> new CanIDeployResult(true, '', '', null, null)
}

when:
Expand All @@ -79,7 +79,7 @@ class PactCanIDeployMojoSpec extends Specification {
then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.canIDeploy('test', '1234', _, _, _) >>
new CanIDeployResult(true, '', '', null)
new CanIDeployResult(true, '', '', null, null)
}

def 'passes optional parameters to the pact broker client'() {
Expand All @@ -94,7 +94,7 @@ class PactCanIDeployMojoSpec extends Specification {
then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.canIDeploy('test', '1234',
new Latest.UseLatest(true), 'prod', _) >> new CanIDeployResult(true, '', '', null)
new Latest.UseLatest(true), 'prod', _) >> new CanIDeployResult(true, '', '', null, null)
}

def 'passes ignore parameters to the pact broker client'() {
Expand All @@ -110,7 +110,23 @@ class PactCanIDeployMojoSpec extends Specification {
then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.canIDeploy('test', '1234',
new Latest.UseLatest(true), '', selectors) >> new CanIDeployResult(true, '', '', null)
new Latest.UseLatest(true), '', selectors) >> new CanIDeployResult(true, '', '', null, null)
}

def 'prints verification results url when pact broker client returns one'() {
given:
IgnoreSelector[] selectors = [new IgnoreSelector('bob')] as IgnoreSelector[]
mojo.latest = 'true'
mojo.ignore = selectors
mojo.brokerClient = Mock(PactBrokerClient)

when:
mojo.execute()

then:
notThrown(MojoExecutionException)
1 * mojo.brokerClient.canIDeploy('test', '1234',
new Latest.UseLatest(true), '', selectors) >> new CanIDeployResult(true, '', '', null, "verificationResultUrl")
}

def 'throws an exception if the pact broker client says no'() {
Expand All @@ -122,7 +138,7 @@ class PactCanIDeployMojoSpec extends Specification {

then:
1 * mojo.brokerClient.canIDeploy('test', '1234', _, _, _) >>
new CanIDeployResult(false, 'Bad version', 'Bad version', null)
new CanIDeployResult(false, 'Bad version', 'Bad version', null, null)
def ex = thrown(MojoExecutionException)
ex.message == 'Can you deploy? Computer says no ¯\\_(ツ)_/¯ Bad version'
}
Expand Down