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 authored and rholshausen committed Nov 24, 2022
1 parent e8bcf08 commit 49a38b6
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,70 @@ 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.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

/**
* 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 @@ -65,8 +87,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 @@ -10,6 +10,7 @@ import org.gradle.api.Project
class PactPlugin extends PactPluginBase {

@Override
@SuppressWarnings('AbcMetric')
void apply(Project project) {

// Create and install the extension object
Expand All @@ -27,9 +28,17 @@ 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,17 +95,41 @@ 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)
}

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()

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

when:
project.tasks.canIDeploy.canIDeploy()

then:
notThrown(GradleScriptException)
1 * project.tasks.canIDeploy.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'() {
given:
project.pact {
Expand All @@ -121,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 @@ -143,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 49a38b6

Please sign in to comment.