Skip to content

Commit

Permalink
Add support for providerVersionBranches #1554
Browse files Browse the repository at this point in the history
  • Loading branch information
TGNThump authored and rholshausen committed May 25, 2022
1 parent 2931d69 commit 90f821f
Show file tree
Hide file tree
Showing 12 changed files with 240 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ interface IPactBrokerClient {
providerName: String,
selectors: List<ConsumerVersionSelector>,
providerTags: List<String> = emptyList(),
providerBranches: List<String> = emptyList(),
enablePending: Boolean = false,
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception>
Expand Down Expand Up @@ -299,6 +300,7 @@ open class PactBrokerClient(
providerName: String,
selectors: List<ConsumerVersionSelector>,
providerTags: List<String>,
providerBranches: List<String>,
enablePending: Boolean,
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception> {
Expand All @@ -314,9 +316,9 @@ open class PactBrokerClient(
return if (pactsForVerification != null) {
val selectorsRawJson = System.getProperty("pactbroker.consumerversionselectors.rawjson")
if(!selectorsRawJson.isNullOrBlank()){
fetchPactsUsingNewEndpointRaw(selectorsRawJson, enablePending, providerTags, includeWipPactsSince, halClient, pactsForVerification, providerName)
fetchPactsUsingNewEndpointRaw(selectorsRawJson, enablePending, providerTags, providerBranches, includeWipPactsSince, halClient, pactsForVerification, providerName)
} else {
fetchPactsUsingNewEndpointTyped(selectors, enablePending, providerTags, includeWipPactsSince, halClient, pactsForVerification, providerName)
fetchPactsUsingNewEndpointTyped(selectors, enablePending, providerTags, providerBranches, includeWipPactsSince, halClient, pactsForVerification, providerName)
}
} else {
handleWith {
Expand All @@ -341,31 +343,34 @@ open class PactBrokerClient(
selectorsTyped: List<ConsumerVersionSelector>,
enablePending: Boolean,
providerTags: List<String>,
providerBranches: List<String>,
includeWipPactsSince: String?,
halClient: IHalClient,
pactsForVerification: String,
providerName: String
): Result<List<PactBrokerResult>, Exception> {
val selectorsJson = jsonArray(selectorsTyped.map { it.toJson() })
return fetchPactsUsingNewEndpoint(selectorsJson, enablePending, providerTags, includeWipPactsSince, halClient, pactsForVerification, providerName)
return fetchPactsUsingNewEndpoint(selectorsJson, enablePending, providerTags, providerBranches, includeWipPactsSince, halClient, pactsForVerification, providerName)
}

private fun fetchPactsUsingNewEndpointRaw(
selectorsRaw: String,
enablePending: Boolean,
providerTags: List<String>,
providerBranches: List<String>,
includeWipPactsSince: String?,
halClient: IHalClient,
pactsForVerification: String,
providerName: String
): Result<List<PactBrokerResult>, Exception> {
return fetchPactsUsingNewEndpoint(JsonParser.parseString(selectorsRaw), enablePending, providerTags, includeWipPactsSince, halClient, pactsForVerification, providerName)
return fetchPactsUsingNewEndpoint(JsonParser.parseString(selectorsRaw), enablePending, providerTags, providerBranches, includeWipPactsSince, halClient, pactsForVerification, providerName)
}

private fun fetchPactsUsingNewEndpoint(
selectorsJson: JsonValue,
enablePending: Boolean,
providerTags: List<String>,
providerBranches: List<String>,
includeWipPactsSince: String?,
halClient: IHalClient,
pactsForVerification: String,
Expand All @@ -379,6 +384,7 @@ open class PactBrokerClient(
body["includePendingStatus"] = enablePending
if (enablePending) {
body["providerVersionTags"] = jsonArray(providerTags)
body["providerVersionBranches"] = jsonArray(providerBranches)
if (includeWipPactsSince.isNotEmpty()) {
body["includeWipPactsSince"] = includeWipPactsSince
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class PactBrokerClientSpec extends Specification {
}

when:
def consumers = client.fetchConsumersWithSelectors('provider', [], [], false, '').value
def consumers = client.fetchConsumersWithSelectors('provider', [], [], [], false, '').value

then:
consumers == []
Expand All @@ -113,7 +113,7 @@ class PactBrokerClientSpec extends Specification {
}

when:
def consumers = client.fetchConsumersWithSelectors('provider', [], [], false, '').value
def consumers = client.fetchConsumersWithSelectors('provider', [], [], [], false, '').value

then:
consumers != []
Expand All @@ -134,7 +134,7 @@ class PactBrokerClientSpec extends Specification {

when:
def consumers = client.fetchConsumersWithSelectors('provider',
[ new ConsumerVersionSelector('tag', true, null, null) ], [], false, '').value
[ new ConsumerVersionSelector('tag', true, null, null) ], [], [], false, '').value

then:
consumers != []
Expand All @@ -156,7 +156,7 @@ class PactBrokerClientSpec extends Specification {
when:
def consumers = client.fetchConsumersWithSelectors('provider',
[ new ConsumerVersionSelector('tag', true, null, null),
new ConsumerVersionSelector('anotherTag', true, null, null) ], [], false, '').value
new ConsumerVersionSelector('anotherTag', true, null, null) ], [], [], false, '').value

then:
consumers.size() == 2
Expand Down Expand Up @@ -185,7 +185,7 @@ class PactBrokerClientSpec extends Specification {

when:
def consumers = client.fetchConsumersWithSelectors('provider',
[ new ConsumerVersionSelector('tag', true, null, null) ], [], false, '').value
[ new ConsumerVersionSelector('tag', true, null, null) ], [], [], false, '').value

then:
consumers != []
Expand All @@ -206,7 +206,7 @@ class PactBrokerClientSpec extends Specification {

when:
def consumers = client.fetchConsumersWithSelectors('provider',
[ new ConsumerVersionSelector('tag', true, null, null) ], [], false, '').value
[ new ConsumerVersionSelector('tag', true, null, null) ], [], [], false, '').value

then:
consumers == []
Expand Down Expand Up @@ -399,7 +399,7 @@ class PactBrokerClientSpec extends Specification {
''')

when:
def result = client.fetchConsumersWithSelectors('provider', selectors, [], false, '')
def result = client.fetchConsumersWithSelectors('provider', selectors, [], [], false, '')

then:
1 * halClient.navigate() >> halClient
Expand Down Expand Up @@ -432,7 +432,7 @@ class PactBrokerClientSpec extends Specification {
''')

when:
def result = client.fetchConsumersWithSelectors('provider', [], [], false, '')
def result = client.fetchConsumersWithSelectors('provider', [], [], [], false, '')

then:
1 * halClient.navigate() >> halClient
Expand All @@ -450,7 +450,7 @@ class PactBrokerClientSpec extends Specification {
}

when:
def result = client.fetchConsumersWithSelectors('provider', [], [], false, '')
def result = client.fetchConsumersWithSelectors('provider', [], [], [], false, '')

then:
1 * halClient.navigate() >> halClient
Expand Down Expand Up @@ -478,7 +478,7 @@ class PactBrokerClientSpec extends Specification {
}
''')
when:
def result = client.fetchConsumersWithSelectors('provider', selectors, [], false, '2020-24-06')
def result = client.fetchConsumersWithSelectors('provider', selectors, [], [], false, '2020-24-06')

then:
1 * halClient.navigate() >> halClient
Expand All @@ -495,7 +495,7 @@ class PactBrokerClientSpec extends Specification {
}
def selectors = [ new ConsumerVersionSelector('DEV', true, null, null) ]
def json = '{"consumerVersionSelectors":[{"latest":true,"tag":"DEV"}],"includePendingStatus":true,' +
'"includeWipPactsSince":"2020-24-06","providerVersionTags":[]}'
'"includeWipPactsSince":"2020-24-06","providerVersionBranches":[],"providerVersionTags":[]}'
def jsonResult = JsonParser.INSTANCE.parseString('''
{
"_embedded": {
Expand All @@ -505,7 +505,7 @@ class PactBrokerClientSpec extends Specification {
}
''')
when:
def result = client.fetchConsumersWithSelectors('provider', selectors, [], true, '2020-24-06')
def result = client.fetchConsumersWithSelectors('provider', selectors, [], [], true, '2020-24-06')

then:
1 * halClient.navigate() >> halClient
Expand All @@ -524,7 +524,7 @@ class PactBrokerClientSpec extends Specification {

when:
def result = client.fetchConsumersWithSelectors('provider',
[ new ConsumerVersionSelector(null, true, 'consumer', null) ], [], false, '')
[ new ConsumerVersionSelector(null, true, 'consumer', null) ], [], [], false, '')

then:
1 * halClient.navigate() >> halClient
Expand Down Expand Up @@ -580,7 +580,7 @@ class PactBrokerClientSpec extends Specification {
]

when:
def result = client.fetchConsumersWithSelectors('provider', selectors, [], false, '')
def result = client.fetchConsumersWithSelectors('provider', selectors, [], [], false, '')

then:
1 * halClient.navigate() >> halClient
Expand All @@ -604,7 +604,7 @@ class PactBrokerClientSpec extends Specification {
]

when:
def result = client.fetchConsumersWithSelectors('provider', selectors, [], false, '')
def result = client.fetchConsumersWithSelectors('provider', selectors, [], [], false, '')

then:
1 * halClient.navigate() >> {
Expand Down Expand Up @@ -664,7 +664,7 @@ class PactBrokerClientSpec extends Specification {
def expectedJson = '{"consumerVersionSelectors":[{"mainBranch":true}],"includePendingStatus":false}'

when:
client.fetchConsumersWithSelectors('provider', [], [], false, '')
client.fetchConsumersWithSelectors('provider', [], [], [], false, '')

then:
1 * halClient.postJson('pb:provider-pacts-for-verification', _, expectedJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import spock.lang.Specification

@SuppressWarnings(['UnnecessaryGetter', 'LineLength', 'NestedBlockDepth', 'AbcMetric', 'MethodSize', 'ClassSize'])
@SuppressWarnings(['GroovyAssignabilityCheck','UnnecessaryGetter', 'LineLength', 'NestedBlockDepth', 'AbcMetric', 'MethodSize', 'ClassSize'])
class PactBrokerClientPactSpec extends Specification {

private PactBrokerClient pactBrokerClient
Expand Down Expand Up @@ -917,7 +917,7 @@ class PactBrokerClientPactSpec extends Specification {
def result = pactBroker.runTest { server, context ->
def consumerPacts = pactBrokerClient.fetchConsumersWithSelectors('Activity Service', [
new ConsumerVersionSelector('test', true, null, null)
], [], false, '')
], [], [], false, '')
assert consumerPacts instanceof Ok
assert consumerPacts.value.size == 2
assert !consumerPacts.value[0].pending
Expand Down Expand Up @@ -960,7 +960,8 @@ class PactBrokerClientPactSpec extends Specification {
latest true
}
])
providerVersionTags(['master'])
providerVersionTags(['tag'])
providerVersionBranches(['master'])
includePendingStatus true
}
willRespondWith(status: 200)
Expand Down Expand Up @@ -1030,7 +1031,7 @@ class PactBrokerClientPactSpec extends Specification {
def result = pactBroker.runTest { server, context ->
def consumerPacts = pactBrokerClient.fetchConsumersWithSelectors('Activity Service', [
new ConsumerVersionSelector('test', true, null, null)
], ['master'], true, '')
], ['tag'], ['master'], true, '')
assert consumerPacts instanceof Ok
assert consumerPacts.value.size == 2
assert !consumerPacts.value[0].pending
Expand Down Expand Up @@ -1073,7 +1074,8 @@ class PactBrokerClientPactSpec extends Specification {
latest true
}
])
providerVersionTags(['master'])
providerVersionTags(['tag'])
providerVersionBranches(['master'])
includePendingStatus true
includeWipPactsSince '2020-06-24'
}
Expand Down Expand Up @@ -1154,7 +1156,7 @@ class PactBrokerClientPactSpec extends Specification {
def result = pactBroker.runTest { server, context ->
def consumerPacts = pactBrokerClient.fetchConsumersWithSelectors('Activity Service', [
new ConsumerVersionSelector('test', true, null, null)
], ['master'], true, '2020-06-24')
], ['tag'], ['master'], true, '2020-06-24')
assert consumerPacts instanceof Ok
assert consumerPacts.value.size == 2
assert !consumerPacts.value[0].wip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ open class GradleProviderInfo(name: String, val project: Project) : ProviderInfo

val pending = brokerConfig!!.enablePending ?: false
if (pending && (brokerConfig!!.providerTags.isNullOrEmpty() ||
brokerConfig!!.providerTags!!.any { it.trim().isEmpty() })) {
brokerConfig!!.providerTags!!.any { it.trim().isEmpty() }) && (brokerConfig!!.providerBranches.isNullOrEmpty() ||
brokerConfig!!.providerBranches!!.any { it.trim().isEmpty() })) {
throw GradleScriptException(
"""
|No providerTags: To use the pending pacts feature, you need to provide the list of provider names for the provider application version that will be published with the verification results.
|No providerTags or providerBranches: To use the pending pacts feature, you need to provide the list of provider names for the provider application version that will be published with the verification results.
|
|For instance:
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import au.com.dius.pact.core.pactbroker.ConsumerVersionSelector
data class PactBrokerConsumerConfig @JvmOverloads constructor(
var selectors: List<ConsumerVersionSelector>? = listOf(),
var enablePending: Boolean? = false,
var providerTags: List<String>? = listOf()
var providerTags: List<String>? = listOf(),
var providerBranches: List<String>? = listOf()
) {
companion object {
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,16 @@ class GradleProviderInfoSpec extends Specification {
selectors = latestTags('test')
enablePending = true
providerTags = ['master']
providerBranches = ['master']
}

then:
provider.brokerConfig == new PactBrokerConsumerConfig([new ConsumerVersionSelector('test', true, null, null)],
true, ['master'])
true, ['master'], ['master'])
}

@Unroll
def 'fromPactBroker throws an exception if pending pacts is enabled but there are no provider tags'() {
def 'fromPactBroker throws an exception if pending pacts is enabled but there are no provider tags or provider branches'() {
given:
def provider = new GradleProviderInfo('provider', Mock(Project))

Expand All @@ -49,16 +50,18 @@ class GradleProviderInfoSpec extends Specification {
selectors = latestTags('test')
enablePending = true
providerTags = tags
providerBranches = branches
}

then:
def ex = thrown(GradleScriptException)
ex.message.trim().startsWith('No providerTags: To use the pending pacts feature, you need to provide the list of ' +
ex.message.trim().startsWith('No providerTags or providerBranches: To use the pending pacts feature, you need to provide the list of ' +
'provider names')

where:

tags << [null, [], ['']]
branches << [null, [], ['']]
}

def 'supports specifying a fallback tag'() {
Expand Down
Loading

0 comments on commit 90f821f

Please sign in to comment.