Skip to content

Commit 13b6531

Browse files
author
Ronald Holshausen
committedJan 25, 2020
feat: added a can-i-deploy Gradle task #994
1 parent b0b07b0 commit 13b6531

File tree

7 files changed

+127
-9
lines changed

7 files changed

+127
-9
lines changed
 

‎core/pact-broker/src/main/kotlin/au/com/dius/pact/core/pactbroker/PactBrokerClient.kt

+11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ sealed class TestResult {
5757
}
5858
}
5959

60+
sealed class Latest {
61+
data class UseLatest(val latest: Boolean): Latest()
62+
data class UseLatestTag(val latestTag: String): Latest()
63+
}
64+
65+
data class CanIDeployResult(val ok: Boolean, val message: String, val reason: String)
66+
6067
/**
6168
* Client for the pact broker service
6269
*/
@@ -294,6 +301,10 @@ open class PactBrokerClient(val pactBrokerUrl: String, val options: Map<String,
294301
}
295302
}
296303

304+
open fun canIDeploy(pacticipant: String, pacticipantVersion: String, latest: Latest, to: String?): CanIDeployResult {
305+
return CanIDeployResult(false, "", "")
306+
}
307+
297308
companion object : KLogging() {
298309
const val LATEST_PROVIDER_PACTS_WITH_NO_TAG = "pb:latest-untagged-pact-version"
299310
const val LATEST_PROVIDER_PACTS = "pb:latest-provider-pacts"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package au.com.dius.pact.provider.gradle
2+
3+
import groovy.transform.ToString
4+
5+
/**
6+
* Config for pact broker
7+
*/
8+
@ToString
9+
class Broker {
10+
String pactBrokerUrl
11+
String pactBrokerToken
12+
String pactBrokerUsername
13+
String pactBrokerPassword
14+
String pactBrokerAuthenticationScheme
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package au.com.dius.pact.provider.gradle
2+
3+
import au.com.dius.pact.core.pactbroker.Latest
4+
import au.com.dius.pact.core.pactbroker.PactBrokerClient
5+
import org.apache.commons.lang3.StringUtils
6+
import org.fusesource.jansi.Ansi
7+
import org.fusesource.jansi.AnsiConsole
8+
import org.gradle.api.DefaultTask
9+
import org.gradle.api.GradleScriptException
10+
import org.gradle.api.tasks.TaskAction
11+
12+
/**
13+
* Task to push pact files to a pact broker
14+
*/
15+
@SuppressWarnings('Println')
16+
class PactCanIDeployTask extends DefaultTask {
17+
18+
@TaskAction
19+
void canIDeploy() {
20+
AnsiConsole.systemInstall()
21+
if (!project.pact.broker) {
22+
throw new GradleScriptException('You must add a pact broker configuration to your build before you can ' +
23+
'use the CanIDeploy task', null)
24+
}
25+
26+
Broker config = project.pact.broker
27+
def options = [:]
28+
if (StringUtils.isNotEmpty(config.pactBrokerToken)) {
29+
options.authentication = [config.pactBrokerAuthenticationScheme ?: 'bearer',
30+
config.pactBrokerToken]
31+
} else if (StringUtils.isNotEmpty(config.pactBrokerUsername)) {
32+
options.authentication = [config.pactBrokerAuthenticationScheme ?: 'basic',
33+
config.pactBrokerUsername, config.pactBrokerPassword]
34+
}
35+
def brokerClient = new PactBrokerClient(config.pactBrokerUrl, options)
36+
if (!project.hasProperty('pacticipant')) {
37+
throw new GradleScriptException('The CanIDeploy task requires -Ppacticipant=...', null)
38+
}
39+
String pacticipant = project.property('pacticipant')
40+
if (!project.hasProperty('pacticipantVersion')) {
41+
throw new GradleScriptException('The CanIDeploy task requires -PpacticipantVersion=...', null)
42+
}
43+
String pacticipantVersion = project.property('pacticipantVersion')
44+
Latest latest = new Latest.UseLatest(false)
45+
if (project.hasProperty('latest')) {
46+
String latestProp = project.property('latest')
47+
if (latestProp == 'true') {
48+
latest = new Latest.UseLatest(true)
49+
} else if (latestProp == 'false') {
50+
latest = new Latest.UseLatest(false)
51+
} else {
52+
latest = new Latest.UseLatestTag(latestProp)
53+
}
54+
}
55+
String to = null
56+
if (project.hasProperty('to')) {
57+
to = project.property('to')
58+
}
59+
def result = brokerClient.canIDeploy(pacticipant, pacticipantVersion, latest, to)
60+
if (result.ok) {
61+
AnsiConsole.out().println(Ansi.ansi().a('Computer says yes \\o/ ').a(result.message).a('\n\n')
62+
.fg(Ansi.Color.GREEN).a(result.reason).reset())
63+
} else {
64+
AnsiConsole.out().println(Ansi.ansi().a('Computer says no ¯\\_(ツ)_/¯ ').a(result.message).a('\n\n')
65+
.fg(Ansi.Color.RED).a(result.reason).reset())
66+
}
67+
68+
AnsiConsole.systemUninstall()
69+
70+
if (!result.ok) {
71+
throw new GradleScriptException("Can you deploy? Computer says no ¯\\_(ツ)_/¯ ${result.message}", null)
72+
}
73+
}
74+
}

‎provider/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/PactPlugin.groovy

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ class PactPlugin implements Plugin<Project> {
2424
project.task(PACT_VERIFY, description: 'Verify your pacts against your providers', group: GROUP)
2525
project.task('pactPublish', description: 'Publish your pacts to a pact broker', type: PactPublishTask,
2626
group: GROUP)
27+
project.task('canIDeploy', description: 'Check if it is safe to deploy by checking whether or not the ' +
28+
'specified pacticipant versions are compatible', type: PactCanIDeployTask,
29+
group: GROUP)
2730

2831
project.afterEvaluate {
29-
3032
if (it.pact == null) {
3133
throw new GradleScriptException('No pact block was found in the project', null)
3234
} else if (!(it.pact instanceof PactPluginExtension)) {

‎provider/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/PactPluginExtension.groovy

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class PactPluginExtension {
1313
final NamedDomainObjectContainer<GradleProviderInfo> serviceProviders
1414

1515
PactPublish publish
16-
1716
VerificationReports reports
17+
Broker broker
1818

1919
PactPluginExtension(NamedDomainObjectContainer<GradleProviderInfo> serviceProviders) {
2020
this.serviceProviders = serviceProviders
@@ -36,4 +36,10 @@ class PactPluginExtension {
3636
reports = new VerificationReports()
3737
configureAction.execute(reports)
3838
}
39+
40+
@SuppressWarnings('ConfusingMethodName')
41+
void broker(Action<? extends Broker> configureAction) {
42+
broker = new Broker()
43+
configureAction.execute(broker)
44+
}
3945
}

‎provider/pact-jvm-provider-gradle/src/main/groovy/au/com/dius/pact/provider/gradle/PactPublishTask.groovy

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,21 @@ class PactPublishTask extends DefaultTask {
3434
version = version.call()
3535
}
3636

37+
def brokerConfig = project.pact.broker
38+
if (!brokerConfig) {
39+
brokerConfig = project.pact.publish
40+
}
41+
3742
def options = [:]
38-
if (StringUtils.isNotEmpty(pactPublish.pactBrokerToken)) {
39-
options.authentication = [pactPublish.pactBrokerAuthenticationScheme ?: 'bearer',
40-
pactPublish.pactBrokerToken]
43+
if (StringUtils.isNotEmpty(brokerConfig.pactBrokerToken)) {
44+
options.authentication = [brokerConfig.pactBrokerAuthenticationScheme ?: 'bearer',
45+
brokerConfig.pactBrokerToken]
4146
}
42-
else if (StringUtils.isNotEmpty(pactPublish.pactBrokerUsername)) {
43-
options.authentication = [pactPublish.pactBrokerAuthenticationScheme ?: 'basic',
44-
pactPublish.pactBrokerUsername, pactPublish.pactBrokerPassword]
47+
else if (StringUtils.isNotEmpty(brokerConfig.pactBrokerUsername)) {
48+
options.authentication = [brokerConfig.pactBrokerAuthenticationScheme ?: 'basic',
49+
brokerConfig.pactBrokerUsername, brokerConfig.pactBrokerPassword]
4550
}
46-
def brokerClient = new PactBrokerClient(pactPublish.pactBrokerUrl, options)
51+
def brokerClient = new PactBrokerClient(brokerConfig.pactBrokerUrl, options)
4752
File pactDirectory = pactPublish.pactDirectory as File
4853
boolean anyFailed = false
4954
pactDirectory.eachFileMatch(FileType.FILES, ~/.*\.json/) { pactFile ->

‎provider/pact-jvm-provider-maven/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ task generatePom(type: GenerateMavenPom, dependsOn: [":provider:pact-jvm-provide
3535
pluginNode.appendNode('artifactId', 'maven-plugin-plugin')
3636
pluginNode.appendNode('version', project.mavenPluginPluginVersion)
3737
pluginNode.appendNode('configuration').appendNode('goalPrefix', 'pact')
38+
39+
def repository = asNode().appendNode('repositories').appendNode('repository')
40+
repository.appendNode('id', 'jcenter')
41+
repository.appendNode('name', 'jcenter')
42+
repository.appendNode('url', 'https://jcenter.bintray.com')
3843
}
3944
}
4045

0 commit comments

Comments
 (0)
Please sign in to comment.