Skip to content

Commit

Permalink
Make PactPublishTask 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 7258f43 commit e3dd8bf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ class PactPlugin extends PactPluginBase {
void apply(Project project) {

// Create and install the extension object
project.extensions.create('pact', PactPluginExtension, project.container(GradleProviderInfo,
def extension = project.extensions.create('pact', PactPluginExtension, project.container(GradleProviderInfo,
new ProviderInfoFactory(project)))

project.task(PACT_VERIFY, description: 'Verify your pacts against your providers', group: GROUP)
project.task('pactPublish', description: 'Publish your pacts to a pact broker', type: PactPublishTask,
group: GROUP)

project.tasks.register('pactPublish', PactPublishTask) {
group = GROUP
description = 'Publish your pacts to a pact broker'
pactPublish.set(extension.publish)
broker.set(extension.broker)
projectVersion.set(project.version)
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,45 @@ import org.apache.commons.io.FilenameUtils
import org.apache.commons.lang3.StringUtils
import org.gradle.api.DefaultTask
import org.gradle.api.GradleScriptException
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction

/**
* Task to push pact files to a pact broker
*/
@SuppressWarnings(['Println', 'AbcMetric'])
class PactPublishTask extends DefaultTask {
abstract class PactPublishTask extends DefaultTask {

@Input
@Optional
abstract Property<PactPublish> getPactPublish()

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

@Input
abstract Property<Object> getProjectVersion()

@Input
abstract Property<File> getPactDir()

@TaskAction
void publishPacts() {
if (!project.pact.publish) {
PactPublish pactPublish = pactPublish.getOrElse(null)
if (pactPublish == null) {
throw new GradleScriptException('You must add a pact publish configuration to your build before you can ' +
'use the pactPublish task', null)
}

PactPublish pactPublish = project.pact.publish
if (pactPublish.pactDirectory == null) {
pactPublish.pactDirectory = project.file("${project.buildDir}/pacts")
pactPublish.pactDirectory = pactDir.get()
}
def version = pactPublish.consumerVersion
if (version == null) {
version = project.version
version = projectVersion.get()
} else if (version instanceof Closure) {
version = version.call()
}
Expand All @@ -40,7 +57,8 @@ class PactPublishTask extends DefaultTask {
throw new GradleScriptException('The consumer version is required to publish Pact files', null)
}

def brokerConfig = project.pact.broker ?: project.pact.publish
Broker broker = broker.getOrElse(null)
def brokerConfig = broker ?: pactPublish
def options = [:]
if (StringUtils.isNotEmpty(brokerConfig.pactBrokerToken)) {
options.authentication = [brokerConfig.pactBrokerAuthenticationScheme ?: 'bearer',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package au.com.dius.pact.provider.gradle

import au.com.dius.pact.core.pactbroker.PactBrokerClient
import au.com.dius.pact.core.pactbroker.PublishConfiguration
import au.com.dius.pact.core.support.Version
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import au.com.dius.pact.core.support.Version
import org.apache.commons.io.IOUtils
import org.gradle.api.GradleScriptException
import org.gradle.api.Project
Expand All @@ -15,7 +15,6 @@ import java.nio.charset.Charset

class PactPublishTaskSpec extends Specification {

private PactPublishTask task
private PactPlugin plugin
private Project project
private PactBrokerClient brokerClient
Expand All @@ -25,7 +24,6 @@ class PactPublishTaskSpec extends Specification {
project = ProjectBuilder.builder().build()
plugin = new PactPlugin()
plugin.apply(project)
task = project.tasks.pactPublish

project.file("${project.buildDir}/pacts").mkdirs()
pactFile = project.file("${project.buildDir}/pacts/test_pact.json")
Expand All @@ -38,7 +36,7 @@ class PactPublishTaskSpec extends Specification {

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

then:
thrown(GradleScriptException)
Expand All @@ -54,7 +52,7 @@ class PactPublishTaskSpec extends Specification {
project.evaluate()

when:
task.publishPacts()
project.tasks.pactPublish.publishPacts()

then:
1 * brokerClient.uploadPactFile(_, _) >> new Ok(null)
Expand All @@ -70,7 +68,7 @@ class PactPublishTaskSpec extends Specification {
project.evaluate()

when:
task.publishPacts()
project.tasks.pactPublish.publishPacts()

then:
1 * brokerClient.uploadPactFile(_, _) >> new Err(new RuntimeException('Boom'))
Expand All @@ -88,7 +86,7 @@ class PactPublishTaskSpec extends Specification {
project.evaluate()

when:
task.publishPacts()
project.tasks.pactPublish.publishPacts()

then:
1 * new PactBrokerClient(_, ['authentication': ['basic', 'my user name', null]], _) >> brokerClient
Expand All @@ -106,7 +104,7 @@ class PactPublishTaskSpec extends Specification {
project.evaluate()

when:
task.publishPacts()
project.tasks.pactPublish.publishPacts()

then:
1 * new PactBrokerClient(_, ['authentication': ['bearer', 'token1234']], _) >> brokerClient
Expand All @@ -125,7 +123,7 @@ class PactPublishTaskSpec extends Specification {
project.evaluate()

when:
task.publishPacts()
project.tasks.pactPublish.publishPacts()

then:
1 * brokerClient.uploadPactFile(_, new PublishConfiguration('1', ['tag1'])) >> new Ok(null)
Expand All @@ -150,7 +148,7 @@ class PactPublishTaskSpec extends Specification {
}

when:
task.publishPacts()
project.tasks.pactPublish.publishPacts()

then:
1 * brokerClient.uploadPactFile(pactFile, _) >> new Ok(null)
Expand All @@ -170,7 +168,7 @@ class PactPublishTaskSpec extends Specification {
project.evaluate()

when:
task.publishPacts()
project.tasks.pactPublish.publishPacts()

then:
1 * brokerClient.uploadPactFile(_, new PublishConfiguration('1.2.3')) >> new Ok(null)
Expand Down

0 comments on commit e3dd8bf

Please sign in to comment.