Skip to content

Commit

Permalink
Make PactCanIDeployTask compatible with Gradle Configuration Cache
Browse files Browse the repository at this point in the history
  • Loading branch information
prof18 committed Oct 10, 2022
1 parent e3dd8bf commit 0a23cab
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,73 @@ import au.com.dius.pact.core.pactbroker.Latest
import au.com.dius.pact.core.pactbroker.PactBrokerClient
import com.github.ajalt.mordant.TermColors
import org.gradle.api.GradleScriptException
import org.gradle.api.provider.Property
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

import javax.inject.Inject

/**
* Task to verify the deployment state using a pact broker
*/
@SuppressWarnings(['Println', 'DuplicateStringLiteral'])
class PactCanIDeployTask extends PactCanIDeployBaseTask {
abstract class PactCanIDeployTask extends PactCanIDeployBaseTask {

private static final String PACTICIPANT = 'pacticipant'
private static final String PACTICIPANT_VERSION = 'pacticipantVersion'
private static final String TO = 'toTag'
private static final String LATEST = 'latest'
static final String PACTICIPANT = 'pacticipant'
static final String PACTICIPANT_VERSION = 'pacticipantVersion'
static final String TO = 'toTag'
static final String LATEST = 'latest'

@Internal
PactBrokerClient brokerClient
abstract PactBrokerClient brokerClient

@Input
@Optional
abstract Property<Broker> getBroker()

@Input
@Optional
abstract Property<Object> getPacticipant()

@Input
@Optional
abstract Property<Object> getPacticipantVersion()

@Input
@Optional
abstract Property<Object> getToProp()

@Input
@Optional
abstract Property<Object> getLatestProp()

@TaskAction
void canIDeploy() {
if (!project.pact.broker) {
if (!broker.present) {
throw new GradleScriptException('You must add a pact broker configuration to your build before you can ' +
'use the CanIDeploy task', null)
}

if (brokerClient == null) {
Broker config = project.pact.broker
Broker config = broker.get()
brokerClient = setupBrokerClient(config)
}

if (!project.hasProperty(PACTICIPANT)) {
if (!pacticipant.present) {
throw new GradleScriptException('The CanIDeploy task requires -Ppacticipant=...', null)
}
String pacticipant = project.property(PACTICIPANT)
String pacticipant = pacticipant.get()
Latest latest = setupLatestParam()
if ((latest instanceof Latest.UseLatestTag || latest.latest == false) &&
!project.hasProperty(PACTICIPANT_VERSION)) {
!pacticipantVersion.present) {
throw new GradleScriptException('The CanIDeploy task requires -PpacticipantVersion=... or -Platest=true', null)
}
String pacticipantVersion = project.hasProperty(PACTICIPANT_VERSION) ? project.property(PACTICIPANT_VERSION) : ''
String pacticipantVersion = pacticipantVersion.orElse('').get()
String to = null
if (project.hasProperty(TO)) {
to = project.property(TO)
if (toProp.present) {
to = toProp.get()
}
def t = new TermColors()
logger.debug(
Expand All @@ -69,8 +94,8 @@ class PactCanIDeployTask extends PactCanIDeployBaseTask {

private Latest setupLatestParam() {
Latest latest = new Latest.UseLatest(false)
if (project.hasProperty(LATEST)) {
String latestProp = project.property(LATEST)
if (latestProp.present) {
String latestProp = latestProp.get()
if (latestProp == 'true') {
latest = new Latest.UseLatest(true)
} else if (latestProp == 'false') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,16 @@ class PactPlugin extends PactPluginBase {
pactDir.set(project.file("${project.buildDir}/pacts"))
}

project.task('canIDeploy', description: 'Check if it is safe to deploy by checking whether or not the ' +
'specified pacticipant versions are compatible', type: PactCanIDeployTask,
group: GROUP)
project.tasks.register('canIDeploy', PactCanIDeployTask) {
group = GROUP
description = 'Check if it is safe to deploy by checking whether or not the ' +
'specified pacticipant versions are compatible'
broker.set(extension.broker)
pacticipant.set(project.hasProperty(PACTICIPANT) ? project.property(PACTICIPANT) : null)
pacticipantVersion.set(project.hasProperty(PACTICIPANT_VERSION) ? project.property(PACTICIPANT_VERSION) : null)
toProp.set(project.hasProperty(TO) ? project.property(TO) : null)
latestProp.set(project.hasProperty(LATEST) ? project.property(LATEST) : null)
}

project.afterEvaluate {
if (it.pact == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ import spock.lang.Specification

class PactCanIDeployTaskSpec extends Specification {

private PactCanIDeployTask task
private PactPlugin plugin
private Project project

def setup() {
project = ProjectBuilder.builder().build()
plugin = new PactPlugin()
plugin.apply(project)
task = project.tasks.canIDeploy
}

def 'raises an exception if no pact broker configuration is found'() {
when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

then:
thrown(GradleScriptException)
Expand All @@ -39,7 +37,7 @@ class PactCanIDeployTaskSpec extends Specification {
project.evaluate()

when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

then:
def ex = thrown(GradleScriptException)
Expand All @@ -57,7 +55,7 @@ class PactCanIDeployTaskSpec extends Specification {
project.evaluate()

when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

then:
def ex = thrown(GradleScriptException)
Expand All @@ -75,12 +73,12 @@ class PactCanIDeployTaskSpec extends Specification {
project.ext.latest = 'true'
project.evaluate()

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

when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

then:
notThrown(GradleScriptException)
Expand All @@ -97,14 +95,14 @@ class PactCanIDeployTaskSpec extends Specification {
project.ext.pacticipantVersion = '1.0.0'
project.evaluate()

task.brokerClient = Mock(PactBrokerClient)
project.tasks.canIDeploy.brokerClient = Mock(PactBrokerClient)

when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

then:
notThrown(GradleScriptException)
1 * task.brokerClient.canIDeploy('pacticipant', '1.0.0', _, _, _) >>
1 * project.tasks.canIDeploy.brokerClient.canIDeploy('pacticipant', '1.0.0', _, _, _) >>
new CanIDeployResult(true, '', '', null, null)
}

Expand All @@ -121,14 +119,14 @@ class PactCanIDeployTaskSpec extends Specification {
project.ext.toTag = 'prod'
project.evaluate()

task.brokerClient = Mock(PactBrokerClient)
project.tasks.canIDeploy.brokerClient = Mock(PactBrokerClient)

when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

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

Expand All @@ -145,14 +143,14 @@ class PactCanIDeployTaskSpec extends Specification {
project.ext.toTag = 'prod'
project.evaluate()

task.brokerClient = Mock(PactBrokerClient)
project.tasks.canIDeploy.brokerClient = Mock(PactBrokerClient)

when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

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

Expand All @@ -167,13 +165,13 @@ class PactCanIDeployTaskSpec extends Specification {
project.ext.pacticipantVersion = '1.0.0'
project.evaluate()

task.brokerClient = Mock(PactBrokerClient)
project.tasks.canIDeploy.brokerClient = Mock(PactBrokerClient)

when:
task.canIDeploy()
project.tasks.canIDeploy.canIDeploy()

then:
1 * task.brokerClient.canIDeploy('pacticipant', '1.0.0', _, _, _) >>
1 * project.tasks.canIDeploy.brokerClient.canIDeploy('pacticipant', '1.0.0', _, _, _) >>
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

0 comments on commit 0a23cab

Please sign in to comment.