Permalink
Browse files

feat: add exclusion lists to the Gradle and Maven publish tasks #711

  • Loading branch information...
uglyog committed Jul 1, 2018
1 parent 34d328d commit c3ec5f637aa237b39d5281d9b099ce3527269126
@@ -642,6 +642,24 @@ pact {
}
```

## Excluding pacts from being published [version 3.5.19+]

You can exclude some of the pact files from being published by providing a list of regular expressions that match
against the base names of the pact files.

For example:

```groovy
pact {
publish {
pactBrokerUrl = 'https://mypactbroker.com'
excludes = [ '.*\\-\\d+$' ] // exclude all pact files that end with a dash followed by a number in the name
}
}
```

# Verifying a message provider [version 2.2.12+]

The Gradle plugin has been updated to allow invoking test methods that can return the message contents from a message
@@ -14,4 +14,5 @@ class PactPublish {
String pactBrokerPassword
String pactBrokerAuthenticationScheme = 'basic'
List<String> tags = []
List<String> excludes = []
}
@@ -2,6 +2,7 @@ package au.com.dius.pact.provider.gradle

import au.com.dius.pact.provider.broker.PactBrokerClient
import groovy.io.FileType
import org.apache.commons.io.FilenameUtils
import org.apache.commons.lang3.StringUtils
import org.fusesource.jansi.AnsiConsole
import org.gradle.api.DefaultTask
@@ -39,16 +40,20 @@ class PactPublishTask extends DefaultTask {
File pactDirectory = pactPublish.pactDirectory as File
boolean anyFailed = false
pactDirectory.eachFileMatch(FileType.FILES, ~/.*\.json/) { pactFile ->
def result
if (pactPublish.tags) {
print "Publishing ${pactFile.name} with tags ${pactPublish.tags.join(', ')} ... "
if (pactFileIsExcluded(pactPublish, pactFile)) {
println("Not publishing '${pactFile.name}' as it matches an item in the excluded list")
} else {
print "Publishing ${pactFile.name} ... "
}
result = brokerClient.uploadPactFile(pactFile, pactPublish.version, pactPublish.tags)
println result
if (!anyFailed && result.startsWith('FAILED!')) {
anyFailed = true
def result
if (pactPublish.tags) {
print "Publishing '${pactFile.name}' with tags ${pactPublish.tags.join(', ')} ... "
} else {
print "Publishing '${pactFile.name}' ... "
}
result = brokerClient.uploadPactFile(pactFile, pactPublish.version, pactPublish.tags)
println result
if (!anyFailed && result.startsWith('FAILED!')) {
anyFailed = true
}
}
}

@@ -59,4 +64,9 @@ class PactPublishTask extends DefaultTask {
}
}

static boolean pactFileIsExcluded(PactPublish pactPublish, File pactFile) {
pactPublish.excludes.any {
FilenameUtils.getBaseName(pactFile.name) ==~ it
}
}
}
@@ -15,6 +15,7 @@ class PactPublishTaskSpec extends Specification {
private PactPlugin plugin
private Project project
private PactBrokerClient brokerClient
private File pactFile

def setup() {
project = ProjectBuilder.builder().build()
@@ -23,14 +24,15 @@ class PactPublishTaskSpec extends Specification {
task = project.tasks.pactPublish

project.file("${project.buildDir}/pacts").mkdirs()
project.file("${project.buildDir}/pacts/test_pact.json").withWriter {
pactFile = project.file("${project.buildDir}/pacts/test_pact.json")
pactFile.withWriter {
IOUtils.copy(PactPublishTaskSpec.getResourceAsStream('/pacts/foo_pact.json'), it, Charset.forName('UTF-8'))
}

brokerClient = GroovySpy(PactBrokerClient, global: true, constructorArgs: ['baseUrl'])
}

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

@@ -106,4 +108,32 @@ class PactPublishTaskSpec extends Specification {
1 * brokerClient.uploadPactFile(_, _, ['tag1']) >> 'HTTP/1.1 200 OK'
}

def 'allows pact files to be excluded from publishing'() {
given:
project.pact {
publish {
excludes = ['other-pact', 'pact\\-\\d+']
pactBrokerUrl = 'pactBrokerUrl'
}
}
project.evaluate()

List<File> excluded = ['pact-1', 'pact-2', 'other-pact'].collect { pactName ->
def file = project.file("${project.buildDir}/pacts/${pactName}.json")
file.withWriter {
IOUtils.copy(PactPublishTaskSpec.getResourceAsStream('/pacts/foo_pact.json'), it, Charset.forName('UTF-8'))
}
file
}

when:
task.publishPacts()

then:
1 * brokerClient.uploadPactFile(pactFile, _, []) >> 'HTTP/1.1 200 OK'
0 * brokerClient.uploadPactFile(excluded[0], _, [])
0 * brokerClient.uploadPactFile(excluded[1], _, [])
0 * brokerClient.uploadPactFile(excluded[2], _, [])
}

}
@@ -629,14 +629,46 @@ configuration in your POM.
<plugin>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-maven_2.11</artifactId>
<version>3.3.9</version>
<version>3.5.19</version>
<configuration>
<pactBrokerUrl>http://pactbroker:1234</pactBrokerUrl>
<pactBrokerServerId>test-pact-broker</pactBrokerServerId> <!-- This must match the server id in the maven settings -->
</configuration>
</plugin>
```

## Excluding pacts from being published [version 3.5.19+]

You can exclude some of the pact files from being published by providing a list of regular expressions that match
against the base names of the pact files.

For example:

```groovy
pact {
publish {
pactBrokerUrl = 'https://mypactbroker.com'
excludes = [ '.*\\-\\d+$' ] // exclude all pact files that end with a dash followed by a number in the name
}
}
```

```xml
<plugin>
<groupId>au.com.dius</groupId>
<artifactId>pact-jvm-provider-maven_2.12</artifactId>
<version>3.5.19</version>
<configuration>
<pactBrokerUrl>http://pactbroker:1234</pactBrokerUrl>
<excludes>
<exclude>.*\\-\\d+$</exclude> <!-- exclude pact files where the name ends in a dash followed by a number -->
</excludes>
</configuration>
</plugin>
```

# Publishing verification results to a Pact Broker [version 3.5.4+]

For pacts that are loaded from a Pact Broker, the results of running the verification can be published back to the
@@ -53,6 +53,9 @@ open class PactPublishMojo : AbstractMojo() {
@Parameter
private var tags: MutableList<String> = mutableListOf()

@Parameter
private var excludes: MutableList<String> = mutableListOf()

override fun execute() {
AnsiConsole.systemInstall()

@@ -80,15 +83,19 @@ open class PactPublishMojo : AbstractMojo() {
if (!pactDirectory.exists()) {
println("Pact directory $pactDirectory does not exist, skipping uploading of pacts")
} else {

val excludedList = this.excludes.map { Regex(it) }
try {
var anyFailed = false
pactDirectory.walkTopDown().filter { it.isFile && it.extension == "json" }.forEach { pactFile ->
print("Publishing ${pactFile.name} ... ")
val result = brokerClient!!.uploadPactFile(pactFile, projectVersion, tags).toString()
println(result)
if (!anyFailed && result.startsWith("FAILED!")) {
anyFailed = true
if (pactFileIsExcluded(excludedList, pactFile)) {
println("Not publishing '${pactFile.name}' as it matches an item in the excluded list")
} else {
print("Publishing '${pactFile.name}' ... ")
val result = brokerClient!!.uploadPactFile(pactFile, projectVersion, tags).toString()
println(result)
if (!anyFailed && result.startsWith("FAILED!")) {
anyFailed = true
}
}
}

@@ -100,4 +107,7 @@ open class PactPublishMojo : AbstractMojo() {
}
}
}

private fun pactFileIsExcluded(exclusions: List<Regex>, pactFile: File) =
exclusions.any { it.matches(pactFile.nameWithoutExtension) }
}
@@ -1,6 +1,7 @@
package au.com.dius.pact.provider.maven

import au.com.dius.pact.provider.broker.PactBrokerClient
import kotlin.text.Regex
import org.apache.maven.plugin.MojoExecutionException
import spock.lang.Specification

@@ -131,4 +132,32 @@ class PactPublishMojoSpec extends Specification {
dir.deleteDir()
}

def 'Allows some files to be excluded from being published'() {
given:
def dir = Files.createTempDirectory('pacts').toFile()
def pact = PactPublishMojoSpec.classLoader.getResourceAsStream('pacts/contract.json').text
def file1 = new File(dir, 'pact.json')
file1.write(pact)
def file2 = new File(dir, 'pact-2.json')
file2.write(pact)
def file3 = new File(dir, 'pact-3.json')
file3.write(pact)
def file4 = new File(dir, 'other-pact.json')
file4.write(pact)
mojo.pactDirectory = dir.toString()
mojo.excludes = [ 'other\\-pact', 'pact\\-\\d+' ]

when:
mojo.execute()

then:
1 * brokerClient.uploadPactFile(file1, _, []) >> 'OK'
0 * brokerClient.uploadPactFile(file2, _, [])
0 * brokerClient.uploadPactFile(file3, _, [])
0 * brokerClient.uploadPactFile(file4, _, [])

cleanup:
dir.deleteDir()
}

}

0 comments on commit c3ec5f6

Please sign in to comment.