Skip to content

Commit

Permalink
fix: providerVersionBranch for pending pacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveen Erode Mohanasundaram authored and rholshausen committed May 31, 2022
1 parent d686907 commit a37409b
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ interface IPactBrokerClient {
providerName: String,
selectors: List<ConsumerVersionSelector>,
providerTags: List<String> = emptyList(),
providerBranches: List<String> = emptyList(),
providerBranch: String?,
enablePending: Boolean = false,
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception>
Expand Down Expand Up @@ -227,7 +227,7 @@ interface IPactBrokerClient {
fun uploadPactFile(pactFile: File, version: String, tags: List<String>): Result<String?, Exception>

/**
* Uploads the given pact file to the broker and applies any tags/branches
* Uploads the given pact file to the broker and applies any tags/Branch
*/
fun uploadPactFile(pactFile: File, config: PublishConfiguration): Result<String?, Exception>
}
Expand Down Expand Up @@ -300,7 +300,7 @@ open class PactBrokerClient(
providerName: String,
selectors: List<ConsumerVersionSelector>,
providerTags: List<String>,
providerBranches: List<String>,
providerBranch: String?,
enablePending: Boolean,
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception> {
Expand All @@ -316,9 +316,9 @@ open class PactBrokerClient(
return if (pactsForVerification != null) {
val selectorsRawJson = System.getProperty("pactbroker.consumerversionselectors.rawjson")
if(!selectorsRawJson.isNullOrBlank()){
fetchPactsUsingNewEndpointRaw(selectorsRawJson, enablePending, providerTags, providerBranches, includeWipPactsSince, halClient, pactsForVerification, providerName)
fetchPactsUsingNewEndpointRaw(selectorsRawJson, enablePending, providerTags, providerBranch, includeWipPactsSince, halClient, pactsForVerification, providerName)
} else {
fetchPactsUsingNewEndpointTyped(selectors, enablePending, providerTags, providerBranches, includeWipPactsSince, halClient, pactsForVerification, providerName)
fetchPactsUsingNewEndpointTyped(selectors, enablePending, providerTags, providerBranch, includeWipPactsSince, halClient, pactsForVerification, providerName)
}
} else {
handleWith {
Expand All @@ -343,34 +343,34 @@ open class PactBrokerClient(
selectorsTyped: List<ConsumerVersionSelector>,
enablePending: Boolean,
providerTags: List<String>,
providerBranches: List<String>,
providerBranch: 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, providerBranches, includeWipPactsSince, halClient, pactsForVerification, providerName)
return fetchPactsUsingNewEndpoint(selectorsJson, enablePending, providerTags, providerBranch, includeWipPactsSince, halClient, pactsForVerification, providerName)
}

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

private fun fetchPactsUsingNewEndpoint(
selectorsJson: JsonValue,
enablePending: Boolean,
providerTags: List<String>,
providerBranches: List<String>,
providerBranch: String?,
includeWipPactsSince: String?,
halClient: IHalClient,
pactsForVerification: String,
Expand All @@ -384,7 +384,9 @@ open class PactBrokerClient(
body["includePendingStatus"] = enablePending
if (enablePending) {
body["providerVersionTags"] = jsonArray(providerTags)
body["providerVersionBranches"] = jsonArray(providerBranches)
if (providerBranch.isNotEmpty()) {
body["providerVersionBranch"] = providerBranch
}
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","providerVersionBranches":[],"providerVersionTags":[]}'
'"includeWipPactsSince":"2020-24-06","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 @@ -918,7 +918,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 @@ -962,7 +962,7 @@ class PactBrokerClientPactSpec extends Specification {
}
])
providerVersionTags(['tag'])
providerVersionBranches(['master'])
providerVersionBranch 'master'
includePendingStatus true
}
willRespondWith(status: 200)
Expand Down Expand Up @@ -1032,7 +1032,7 @@ class PactBrokerClientPactSpec extends Specification {
def result = pactBroker.runTest { server, context ->
def consumerPacts = pactBrokerClient.fetchConsumersWithSelectors('Activity Service', [
new ConsumerVersionSelector('test', true, null, null)
], ['tag'], ['master'], true, '')
], ['tag'], 'master', true, '')
assert consumerPacts instanceof Ok
assert consumerPacts.value.size == 2
assert !consumerPacts.value[0].pending
Expand Down Expand Up @@ -1076,7 +1076,7 @@ class PactBrokerClientPactSpec extends Specification {
}
])
providerVersionTags(['tag'])
providerVersionBranches(['master'])
providerVersionBranch 'master'
includePendingStatus true
includeWipPactsSince '2020-06-24'
}
Expand Down Expand Up @@ -1157,7 +1157,7 @@ class PactBrokerClientPactSpec extends Specification {
def result = pactBroker.runTest { server, context ->
def consumerPacts = pactBrokerClient.fetchConsumersWithSelectors('Activity Service', [
new ConsumerVersionSelector('test', true, null, null)
], ['tag'], ['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 @@ -104,12 +104,13 @@ open class GradleProviderInfo(name: String, val project: Project) : ProviderInfo
ConfigureUtil.configure(closure, brokerConfig!!)

val pending = brokerConfig!!.enablePending ?: false
if (pending && (brokerConfig!!.providerTags.isNullOrEmpty() ||
brokerConfig!!.providerTags!!.any { it.trim().isEmpty() }) && (brokerConfig!!.providerBranches.isNullOrEmpty() ||
brokerConfig!!.providerBranches!!.any { it.trim().isEmpty() })) {
if (pending
&& (brokerConfig!!.providerTags.isNullOrEmpty() || brokerConfig!!.providerTags!!.any { it.trim().isEmpty() })
&& (brokerConfig!!.providerBranch.isNullOrBlank())
) {
throw GradleScriptException(
"""
|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.
|No providerTags or providerBranch: 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 @@ -9,7 +9,7 @@ data class PactBrokerConsumerConfig @JvmOverloads constructor(
var selectors: List<ConsumerVersionSelector>? = listOf(),
var enablePending: Boolean? = false,
var providerTags: List<String>? = listOf(),
var providerBranches: List<String>? = listOf()
var providerBranch: String? = ""
) {
companion object {
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ class GradleProviderInfoSpec extends Specification {
selectors = latestTags('test')
enablePending = true
providerTags = ['master']
providerBranches = ['master']
providerBranch = 'master'
}

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

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

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

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

where:

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

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

0 comments on commit a37409b

Please sign in to comment.