/
PactBrokerClient.kt
1175 lines (1074 loc) · 41.5 KB
/
PactBrokerClient.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package au.com.dius.pact.core.pactbroker
import au.com.dius.pact.core.support.Json
import au.com.dius.pact.core.support.Result
import au.com.dius.pact.core.support.Utils
import au.com.dius.pact.core.support.Utils.lookupEnvironmentValue
import au.com.dius.pact.core.support.handleWith
import au.com.dius.pact.core.support.ifNullOrEmpty
import au.com.dius.pact.core.support.isNotEmpty
import au.com.dius.pact.core.support.json.JsonParser
import au.com.dius.pact.core.support.json.JsonValue
import au.com.dius.pact.core.support.json.map
import au.com.dius.pact.core.support.jsonArray
import au.com.dius.pact.core.support.jsonObject
import au.com.dius.pact.core.support.mapOk
import au.com.dius.pact.core.support.mapError
import au.com.dius.pact.core.support.toJson
import com.google.common.net.UrlEscapers.urlFormParameterEscaper
import io.github.oshai.kotlinlogging.KLogging
import java.io.File
import java.io.IOException
import java.net.URLDecoder
import java.util.Base64
import java.util.function.Consumer
/**
* Wraps the response for a Pact from the broker with the link data associated with the Pact document.
*/
data class PactResponse(val pactFile: JsonValue.Object, val links: Map<String, Any?>)
/**
* Test result that is sent to the Pact broker
*/
sealed class TestResult {
/**
* Success result
*/
data class Ok(val interactionIds: Set<String> = emptySet()) : TestResult() {
constructor(interactionId: String?) : this(if (interactionId.isNullOrEmpty())
emptySet() else setOf(interactionId))
override fun toBoolean() = true
override fun merge(result: TestResult) = when (result) {
is Ok -> this.copy(interactionIds = interactionIds + result.interactionIds)
is Failed -> result.merge(this)
}
}
/**
* Failed result
*/
data class Failed(var results: List<Map<String, Any?>> = emptyList(), val description: String = "") : TestResult() {
override fun toBoolean() = false
override fun merge(result: TestResult) = when (result) {
is Ok -> if (result.interactionIds.isEmpty()) {
this
} else {
val allResults = results + result.interactionIds.map { mapOf("interactionId" to it) }
val grouped = allResults.groupBy { it["interactionId"] }
val filtered = grouped.mapValues { entry ->
val interactionId = entry.key as String?
if (entry.value.size == 1) {
entry.value
} else {
entry.value.map { map -> map.filterKeys { it != "interactionId" } }
.filter { it.isNotEmpty() }
.map { map ->
if (interactionId.isNullOrEmpty()) {
map
} else {
map + ("interactionId" to interactionId)
}
}
}
}
this.copy(results = filtered.values.flatten())
}
is Failed -> Failed(results + result.results, when {
description.isNotEmpty() && result.description.isNotEmpty() && description != result.description ->
"$description, ${result.description}"
description.isNotEmpty() -> description
else -> result.description
})
}
}
abstract fun toBoolean(): Boolean
abstract fun merge(result: TestResult): TestResult
}
/**
* Represents a request for the latest pact, or the latest pact for a particular tag
*/
sealed class Latest {
data class UseLatest(val latest: Boolean) : Latest()
data class UseLatestTag(val latestTag: String) : Latest()
}
/**
* Specifies the target for the can-i-deploy check (tag or environment)
*/
data class To @JvmOverloads constructor(val tag: String? = null, val environment: String? = null)
/**
* Model for a CanIDeploy result
*/
data class CanIDeployResult(
val ok: Boolean,
val message: String,
val reason: String,
val unknown: Int? = null,
val verificationResultUrl: String? = null
)
/**
* Consumer version selector. See https://docs.pact.io/pact_broker/advanced_topics/selectors
*/
@Deprecated(message = "Has been replaced with ConsumerVersionSelectors sealed classes")
data class ConsumerVersionSelector(
val tag: String? = null,
val latest: Boolean = true,
val consumer: String? = null,
val fallbackTag: String? = null
) {
fun toJson(): JsonValue {
val obj = JsonValue.Object("latest" to Json.toJson(latest))
if (tag.isNotEmpty()) {
obj.add("tag", Json.toJson(tag))
}
if (consumer.isNotEmpty()) {
obj.add("consumer", Json.toJson(consumer))
}
if (fallbackTag.isNotEmpty()) {
obj.add("fallbackTag", Json.toJson(fallbackTag))
}
return obj
}
/**
* Converts this deprecated version to a ConsumerVersionSelectors
*/
fun toSelector(): ConsumerVersionSelectors {
return ConsumerVersionSelectors.Selector(tag, latest, consumer, fallbackTag)
}
}
/**
* Consumer version selectors. See https://docs.pact.io/pact_broker/advanced_topics/selectors
*/
sealed class ConsumerVersionSelectors {
/**
* The latest version from the main branch of each consumer, as specified by the consumer's mainBranch property.
*/
object MainBranch: ConsumerVersionSelectors()
/**
* The latest version from a particular branch of each consumer, or for a particular consumer if the second
* parameter is provided. If fallback is provided, falling back to the fallback branch if none is found from the
* specified branch.
*/
data class Branch @JvmOverloads constructor(
val name: String,
val consumer: String? = null,
val fallback: String? = null
): ConsumerVersionSelectors()
/**
* All the currently deployed and currently released and supported versions of each consumer.
*/
object DeployedOrReleased: ConsumerVersionSelectors()
/**
* The latest version from any branch of the consumer that has the same name as the current branch of the provider.
* Used for coordinated development between consumer and provider teams using matching feature branch names.
*/
object MatchingBranch: ConsumerVersionSelectors()
/**
* Any versions currently deployed to the specified environment
*/
data class DeployedTo(val environment: String): ConsumerVersionSelectors()
/**
* Any versions currently released and supported in the specified environment
*/
data class ReleasedTo(val environment: String): ConsumerVersionSelectors()
/**
* Any versions currently deployed or released and supported in the specified environment
*/
data class Environment(val environment: String): ConsumerVersionSelectors()
/**
* All versions with the specified tag
*/
data class Tag(val tag: String): ConsumerVersionSelectors()
/**
* The latest version for each consumer with the specified tag. If fallback is provided, will fall back to the
* fallback tag if none is found with the specified tag
*/
data class LatestTag @JvmOverloads constructor(
val tag: String,
val fallback: String? = null
): ConsumerVersionSelectors()
/**
* Corresponds to the old consumer version selectors
*/
@Deprecated(message = "Old form of consumer version selectors have been deprecated in favor of the newer forms " +
"(Branches, Tags, etc.)")
data class Selector @JvmOverloads constructor(
val tag: String? = null,
val latest: Boolean? = null,
val consumer: String? = null,
val fallbackTag: String? = null
): ConsumerVersionSelectors()
/**
* Raw JSON form of a selector.
*/
data class RawSelector(val selector: JsonValue): ConsumerVersionSelectors()
fun toJson(): JsonValue {
return when (this) {
is Branch -> {
val entries = mutableMapOf<String, JsonValue>("branch" to JsonValue.StringValue(this.name))
if (this.consumer.isNotEmpty()) {
entries["consumer"] = JsonValue.StringValue(this.consumer!!)
}
if (this.fallback.isNotEmpty()) {
entries["fallbackBranch"] = JsonValue.StringValue(this.fallback!!)
}
JsonValue.Object(entries)
}
DeployedOrReleased -> JsonValue.Object("deployedOrReleased" to JsonValue.True)
is DeployedTo -> JsonValue.Object(
"deployed" to JsonValue.True,
"environment" to JsonValue.StringValue(this.environment)
)
is Environment -> JsonValue.Object("environment" to JsonValue.StringValue(this.environment))
is LatestTag -> {
val entries = mutableMapOf(
"tag" to JsonValue.StringValue(this.tag),
"latest" to JsonValue.True
)
if (this.fallback.isNotEmpty()) {
entries["fallbackTag"] = JsonValue.StringValue(this.fallback!!)
}
JsonValue.Object(entries)
}
MainBranch -> JsonValue.Object("mainBranch" to JsonValue.True)
MatchingBranch -> JsonValue.Object("matchingBranch" to JsonValue.True)
is ReleasedTo -> JsonValue.Object(
"released" to JsonValue.True,
"environment" to JsonValue.StringValue(this.environment)
)
is Selector -> {
val entries = mutableMapOf<String, JsonValue>()
if (this.tag.isNotEmpty()) {
entries["tag"] = JsonValue.StringValue(this.tag!!)
}
if (this.consumer.isNotEmpty()) {
entries["consumer"] = JsonValue.StringValue(this.consumer!!)
}
if (this.latest == true) {
entries["latest"] = JsonValue.True
} else if (this.latest == false) {
entries["latest"] = JsonValue.False
}
if (this.fallbackTag.isNotEmpty()) {
entries["fallbackTag"] = JsonValue.StringValue(this.fallbackTag!!)
}
JsonValue.Object(entries)
}
is Tag -> JsonValue.Object("tag" to JsonValue.StringValue(this.tag))
is RawSelector -> this.selector
}
}
}
/**
* Selectors to ignore with the can-i-deploy check
*/
data class IgnoreSelector @JvmOverloads constructor(var name: String = "", var version: String? = null) {
fun set(value: String) {
val vals = value.split(":", limit = 2)
if (vals.size == 2) {
name = vals[0]
version = vals[1]
} else {
name = vals[0]
}
}
}
/**
* Interface to a Pact Broker client
*/
interface IPactBrokerClient {
/**
* Fetches all consumers for the given provider and selectors. If `pactbroker.consumerversionselectors.rawjson` is set
* as a system property or environment variable, that will override the selectors provided to this method.
*/
@Throws(IOException::class)
@Deprecated("use version that takes a list of ConsumerVersionSelectors",
replaceWith = ReplaceWith("fetchConsumersWithSelectorsV2"))
fun fetchConsumersWithSelectors(
providerName: String,
selectors: List<ConsumerVersionSelector>,
providerTags: List<String> = emptyList(),
providerBranch: String?,
enablePending: Boolean = false,
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception>
/**
* Fetches all consumers for the given provider and selectors. If `pactbroker.consumerversionselectors.rawjson` is set
* as a system property or environment variable, that will override the selectors provided to this method.
*/
@Throws(IOException::class)
fun fetchConsumersWithSelectorsV2(
providerName: String,
selectors: List<ConsumerVersionSelectors>,
providerTags: List<String> = emptyList(),
providerBranch: String?,
enablePending: Boolean = false,
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception>
fun getUrlForProvider(providerName: String, tag: String): String?
val options: Map<String, Any>
/**
* Publish all the tags for the provider to the Pact broker
* @param docAttributes Attributes associated with the fetched Pact file
* @param name Provider name
* @param tags Provider tags to tag the provider with
* @param version Provider version
*/
fun publishProviderTags(
docAttributes: Map<String, Any?>,
name: String,
tags: List<String>,
version: String
): Result<Boolean, List<String>>
/**
* Publish provider branch to the Pact broker
* @param docAttributes Attributes associated with the fetched Pact file
* @param name Provider name
* @param branch Provider branch
* @param version Provider version
*/
fun publishProviderBranch(
docAttributes: Map<String, Any?>,
name: String,
branch: String,
version: String
): Result<Boolean, String>
/**
* Publishes the result to the "pb:publish-verification-results" link in the document attributes.
*/
fun publishVerificationResults(
docAttributes: Map<String, Any?>,
result: TestResult,
version: String,
buildUrl: String?
): Result<Boolean, String>
/**
* Publishes the result to the "pb:publish-verification-results" link in the document attributes.
*/
fun publishVerificationResults(
docAttributes: Map<String, Any?>,
result: TestResult,
version: String
): Result<Boolean, String>
/**
* Uploads the given pact file to the broker and applies any tags
*/
@Deprecated("Replaced with version that takes a configuration object")
fun uploadPactFile(pactFile: File, version: String): Result<String?, Exception>
/**
* Uploads the given pact file to the broker and applies any tags
*/
@Deprecated("Replaced with version that takes a configuration object")
fun uploadPactFile(pactFile: File, version: String, tags: List<String>): Result<String?, Exception>
/**
* Uploads the given pact file to the broker and applies any tags/Branch
*/
fun uploadPactFile(pactFile: File, config: PublishConfiguration): Result<String?, Exception>
}
/**
* Client configuration.
*/
data class PactBrokerClientConfig @JvmOverloads constructor(
val retryCountWhileUnknown: Int = 0,
val retryWhileUnknownInterval: Int = 10,
val insecureTLS: Boolean = false
)
/**
* Client for the pact broker service
*/
open class PactBrokerClient(
val pactBrokerUrl: String,
@Deprecated("Move use of options to PactBrokerClientConfig")
override val options: MutableMap<String, Any>,
val config: PactBrokerClientConfig
) : IPactBrokerClient {
@Deprecated("Use the version that takes PactBrokerClientConfig")
constructor(pactBrokerUrl: String) : this(pactBrokerUrl, mutableMapOf(), PactBrokerClientConfig())
/**
* Fetches all consumers for the given provider
*/
@Deprecated(message = "Use the version that takes selectors instead",
replaceWith = ReplaceWith("fetchConsumersWithSelectors"))
open fun fetchConsumers(provider: String): List<PactBrokerResult> {
return try {
val halClient = newHalClient()
val consumers = mutableListOf<PactBrokerResult>()
halClient.navigate(mapOf("provider" to provider), LATEST_PROVIDER_PACTS).forAll(PACTS, Consumer { pact ->
val href = pact["href"].toString()
val name = pact["name"].toString()
consumers.add(PactBrokerResult(name, href, pactBrokerUrl))
})
consumers
} catch (e: NotFoundHalResponse) {
// This means the provider is not defined in the broker, so fail gracefully.
emptyList()
}
}
/**
* Fetches all consumers for the given provider and tag
*/
@Deprecated(message = "Use fetchConsumersWithSelectors")
open fun fetchConsumersWithTag(provider: String, tag: String): List<PactBrokerResult> {
return try {
val halClient = newHalClient()
val consumers = mutableListOf<PactBrokerResult>()
halClient.navigate(mapOf("provider" to provider, "tag" to tag), LATEST_PROVIDER_PACTS_WITH_TAG)
.forAll(PACTS, Consumer { pact ->
val href = pact["href"].toString()
val name = pact["name"].toString()
consumers.add(PactBrokerResult(name, href, pactBrokerUrl, emptyList(), tag = tag))
})
consumers
} catch (e: NotFoundHalResponse) {
// This means the provider is not defined in the broker, so fail gracefully.
emptyList()
}
}
override fun fetchConsumersWithSelectors(
providerName: String,
selectors: List<ConsumerVersionSelector>,
providerTags: List<String>,
providerBranch: String?,
enablePending: Boolean,
includeWipPactsSince: String?
) = fetchConsumersWithSelectorsV2(providerName, selectors.map { it.toSelector() }, providerTags, providerBranch,
enablePending, includeWipPactsSince)
override fun fetchConsumersWithSelectorsV2(
providerName: String,
selectors: List<ConsumerVersionSelectors>,
providerTags: List<String>,
providerBranch: String?,
enablePending: Boolean,
includeWipPactsSince: String?
): Result<List<PactBrokerResult>, Exception> {
val halClient = when (val navigateResult = handleWith<IHalClient> { newHalClient().navigate() }) {
is Result.Err<Exception> -> return navigateResult
is Result.Ok<IHalClient> -> navigateResult.value
}
halClient.logContext()
val pactsForVerification = when {
halClient.linkUrl(PROVIDER_PACTS_FOR_VERIFICATION) != null -> PROVIDER_PACTS_FOR_VERIFICATION
halClient.linkUrl(BETA_PROVIDER_PACTS_FOR_VERIFICATION) != null -> BETA_PROVIDER_PACTS_FOR_VERIFICATION
else -> null
}
return if (pactsForVerification != null) {
val selectorsRawJson = lookupEnvironmentValue("pactbroker.consumerversionselectors.rawjson")
if (selectorsRawJson.isNotEmpty()) {
fetchPactsUsingNewEndpointRaw(selectorsRawJson!!, enablePending, providerTags, providerBranch,
includeWipPactsSince, halClient, pactsForVerification, providerName)
} else {
fetchPactsUsingNewEndpointTyped(selectors, enablePending, providerTags, providerBranch, includeWipPactsSince,
halClient, pactsForVerification, providerName)
}
} else {
handleWith {
val tags = selectors
.filterIsInstance(ConsumerVersionSelectors.Selector::class.java)
.filter { it.tag.isNotEmpty() }
.map { it.tag to it.fallbackTag }
if (tags.isEmpty()) {
fetchConsumers(providerName)
} else {
tags.flatMap { (tag, fallbacktag) ->
val tagResult = fetchConsumersWithTag(providerName, tag!!)
if (tagResult.isEmpty() && fallbacktag != null) {
fetchConsumersWithTag(providerName, fallbacktag)
} else {
tagResult
}
}
}
}
}
}
private fun fetchPactsUsingNewEndpointTyped(
selectorsTyped: List<ConsumerVersionSelectors>,
enablePending: Boolean,
providerTags: 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, providerBranch, includeWipPactsSince,
halClient, pactsForVerification, providerName)
}
private fun fetchPactsUsingNewEndpointRaw(
selectorsRaw: String,
enablePending: Boolean,
providerTags: List<String>,
providerBranch: String?,
includeWipPactsSince: String?,
halClient: IHalClient,
pactsForVerification: String,
providerName: String
): Result<List<PactBrokerResult>, Exception> {
return fetchPactsUsingNewEndpoint(JsonParser.parseString(selectorsRaw), enablePending, providerTags,
providerBranch, includeWipPactsSince, halClient, pactsForVerification, providerName)
}
private fun fetchPactsUsingNewEndpoint(
selectorsJson: JsonValue,
enablePending: Boolean,
providerTags: List<String>,
providerBranch: String?,
includeWipPactsSince: String?,
halClient: IHalClient,
pactsForVerification: String,
providerName: String
): Result<List<PactBrokerResult>, Exception> {
logger.debug { "Fetching pacts using the pactsForVerification endpoint" }
val body = JsonValue.Object(
"consumerVersionSelectors" to selectorsJson
)
body["includePendingStatus"] = enablePending
if (enablePending) {
body["providerVersionTags"] = jsonArray(providerTags)
if (providerBranch.isNotEmpty()) {
body["providerVersionBranch"] = providerBranch
}
if (includeWipPactsSince.isNotEmpty()) {
body["includeWipPactsSince"] = includeWipPactsSince
}
}
return handleWith {
halClient.postJson(pactsForVerification, mapOf("provider" to providerName), body.serialise()).mapOk { result ->
result["_embedded"]["pacts"].asArray().map { pactJson ->
val selfLink = pactJson["_links"]["self"]
val href = Json.toString(selfLink["href"])
val name = Json.toString(selfLink["name"])
val properties = pactJson["verificationProperties"]
val notices = properties["notices"].asArray()?.map { VerificationNotice.fromJson(it) }?.filterNotNull() ?:
emptyList()
var pending = false
if (properties is JsonValue.Object && properties.has("pending") && properties["pending"].isBoolean) {
pending = properties["pending"].asBoolean()!!
}
val wip = if (properties.has("wip") && properties["wip"].isBoolean)
properties["wip"].asBoolean()!!
else false
PactBrokerResult(name, href, pactBrokerUrl, halClient.getAuth()?.legacyForm() ?: emptyList(),
notices, pending, wip = wip, usedNewEndpoint = true, auth = halClient.getAuth())
}
}
}
}
/**
* Uploads the given pact file to the broker, and optionally applies any tags
*/
override fun uploadPactFile(pactFile: File, version: String) = uploadPactFile(pactFile, version, emptyList())
/**
* Uploads the given pact file to the broker, and optionally applies any tags
*/
override fun uploadPactFile(pactFile: File, version: String, tags: List<String>) =
uploadPactFile(pactFile, PublishConfiguration(version, tags))
override fun uploadPactFile(pactFile: File, config: PublishConfiguration): Result<String?, Exception> {
val pactText = pactFile.readText()
val pact = JsonParser.parseString(pactText)
val halClient = newHalClient().navigate()
val providerName = Json.toString(pact["provider"]["name"])
val consumerName = Json.toString(pact["consumer"]["name"])
val publishContractsLink = halClient.linkUrl(PUBLISH_CONTRACTS_LINK)
return if (publishContractsLink != null) {
when (val result = publishContract(halClient, providerName, consumerName, config, pactText)) {
is Result.Ok -> Result.Ok("OK")
is Result.Err -> result
}
} else {
if (config.tags.isNotEmpty()) {
uploadTags(halClient, consumerName, config.consumerVersion, config.tags)
}
halClient.putJson(
"pb:publish-pact", mapOf(
"provider" to providerName,
"consumer" to consumerName,
"consumerApplicationVersion" to config.consumerVersion
), pactText
)
}
}
/**
* Publish the contract using the "Publish Contracts" endpoint
*/
fun publishContract(
halClient: IHalClient,
providerName: String,
consumerName: String,
config: PublishConfiguration,
pactText: String
): Result<JsonValue.Object, Exception> {
val branchName = branchName(config)
val consumerBuildUrl = consumerBuildUrl(config)
val bodyValues = mutableMapOf(
"pacticipantName" to consumerName.toJson(),
"pacticipantVersionNumber" to consumerVersion(config),
"tags" to JsonValue.Array(config.tags.map { it.toJson() }.toMutableList()),
"contracts" to JsonValue.Array(
mutableListOf(
JsonValue.Object(
"consumerName" to consumerName.toJson(),
"providerName" to providerName.toJson(),
"specification" to "pact".toJson(),
"contentType" to "application/json".toJson(),
"content" to Base64.getEncoder().encodeToString(pactText.toByteArray()).toJson()
)
)
)
)
if (branchName != JsonValue.Null) {
bodyValues["branch"] = branchName
}
if (consumerBuildUrl != JsonValue.Null) {
bodyValues["buildUrl"] = consumerBuildUrl
}
val body = JsonValue.Object(bodyValues)
return when (val result = halClient.postJson(PUBLISH_CONTRACTS_LINK, mapOf(), body.serialise())) {
is Result.Ok -> {
displayNotices(result.value)
result
}
is Result.Err -> {
val error = result.error
if (error is RequestFailedException && error.body != null) {
when (val json = handleWith<JsonValue> { JsonParser.parseString(error.body) }) {
is Result.Ok -> if (json.value is JsonValue.Object) {
val body: JsonValue.Object = json.value.downcast()
displayNotices(body)
if (error.status == 400) {
displayErrors(body)
}
} else {
logger.error { "Response from Pact Broker was not in correct JSON format: got ${json.value}" }
}
is Result.Err -> {
logger.error { "Response from Pact Broker was not in JSON format: ${json.error}" }
}
}
}
result
}
}
}
private fun consumerBuildUrl(config: PublishConfiguration): JsonValue {
return config.consumerBuildUrl.ifNullOrEmpty {
lookupEnvironmentValue("pact.publish.consumer.buildUrl")
}.toJson()
}
private fun branchName(config: PublishConfiguration): JsonValue {
return config.branchName.ifNullOrEmpty {
lookupEnvironmentValue("pact.publish.consumer.branchName")
}.toJson()
}
private fun consumerVersion(config: PublishConfiguration): JsonValue {
return lookupEnvironmentValue("pact.publish.consumer.version").ifNullOrEmpty {
config.consumerVersion
}.toJson()
}
private fun displayNotices(result: JsonValue.Object) {
val notices = result["notices"]
if (notices is JsonValue.Array) {
for (noticeJson in notices.values) {
if (noticeJson.isObject) {
val notice: JsonValue.Object = noticeJson.downcast()
val level = notice["level"].asString()
val text = notice["text"].asString()
when (level) {
"info", "prompt" -> logger.info { "notice: $text" }
"warning", "danger" -> logger.warn { "notice: $text" }
"error" -> logger.error { "notice: $text" }
else -> logger.debug { "notice: $text" }
}
} else {
logger.error("Got an invalid notice value from the Pact Broker: Expected an object, got ${notices.name}")
}
}
} else {
logger.error("Got an invalid notices value from the Pact Broker: Expected an array, got ${notices.name}")
}
}
private fun displayErrors(result: JsonValue.Object) {
val errors = result["errors"]
if (errors is JsonValue.Object) {
for ((key, errorJson) in errors.entries) {
if (errorJson.isArray) {
for (error in errorJson.asArray()!!.values) {
logger.error("$key: $error")
}
} else {
logger.error("$key: $errorJson")
}
}
}
}
override fun getUrlForProvider(providerName: String, tag: String): String? {
val halClient = newHalClient()
if (tag.isEmpty() || tag == "latest") {
halClient.navigate(mapOf("provider" to providerName), LATEST_PROVIDER_PACTS)
} else {
halClient.navigate(mapOf("provider" to providerName, "tag" to tag), LATEST_PROVIDER_PACTS_WITH_TAG)
}
return halClient.linkUrl(PACTS)
}
open fun fetchPact(url: String, encodePath: Boolean = true): PactResponse {
val halDoc = newHalClient().fetch(url, encodePath).unwrap()
return PactResponse(halDoc, HalClient.asMap(halDoc["_links"].asObject()))
}
open fun newHalClient(): IHalClient = HalClient(pactBrokerUrl, options, config)
override fun publishVerificationResults(
docAttributes: Map<String, Any?>,
result: TestResult,
version: String
) = publishVerificationResults(docAttributes, result, version, null)
override fun publishVerificationResults(
docAttributes: Map<String, Any?>,
result: TestResult,
version: String,
buildUrl: String?
): Result<Boolean, String> {
val halClient = newHalClient()
val publishLink = docAttributes.mapKeys { it.key.lowercase() } ["pb:publish-verification-results"]
return if (publishLink is Map<*, *>) {
val jsonObject = buildPayload(result, version, buildUrl)
val lowercaseMap = publishLink.mapKeys { it.key.toString().lowercase() }
if (lowercaseMap.containsKey("href")) {
halClient.postJson(lowercaseMap["href"].toString(), jsonObject.serialise()).mapError {
logger.error(it) { "Publishing verification results failed with an exception" }
"Publishing verification results failed with an exception: ${it.message}"
}
} else {
Result.Err("Unable to publish verification results as there is no pb:publish-verification-results link")
}
} else {
Result.Err("Unable to publish verification results as there is no pb:publish-verification-results link")
}
}
fun buildPayload(result: TestResult, version: String, buildUrl: String?): JsonValue.Object {
val jsonObject = jsonObject("success" to result.toBoolean(),
"providerApplicationVersion" to version,
"verifiedBy" to mapOf(
"implementation" to "Pact-JVM", "version" to Utils.lookupVersion(PactBrokerClient::class.java)
)
)
if (buildUrl != null) {
jsonObject["buildUrl"] = buildUrl
}
logger.debug { "Test result = $result" }
when (result) {
is TestResult.Failed -> if (result.results.isNotEmpty()) {
val values = result.results
.groupBy { it["interactionId"] }
.map { mismatches ->
val values = mismatches.value
.filter { !it.containsKey("exception") }
.map { mismatch ->
val remainingAttributes = mismatch.filterNot { it.key == "interactionId" }
when (mismatch["attribute"]) {
"body-content-type" -> listOf("attribute" to "body", "description" to mismatch["description"])
else -> remainingAttributes.map { it.toPair() }
}
}.filter { it.isNotEmpty() }
.map { jsonObject(it) }
val exceptionDetails = mismatches.value.find { it.containsKey("exception") }
val exceptions = if (exceptionDetails != null) {
val exception = exceptionDetails["exception"]
val description = exceptionDetails["description"]
if (exception is Throwable) {
if (description != null) {
jsonArray(jsonObject("message" to description.toString() + ": " + exception.message,
"exceptionClass" to exception.javaClass.name))
} else {
jsonArray(jsonObject("message" to exception.message,
"exceptionClass" to exception.javaClass.name))
}
} else {
jsonArray(jsonObject("message" to exception.toString()))
}
} else {
null
}
val interactionJson = if (values.isEmpty() && exceptions == null) {
jsonObject("interactionId" to mismatches.key, "success" to true)
} else {
val json = jsonObject(
"interactionId" to mismatches.key, "success" to false,
"mismatches" to jsonArray(values)
)
if (exceptions != null) {
json["exceptions"] = exceptions
}
json
}
interactionJson
}
jsonObject["testResults"] = jsonArray(values)
}
is TestResult.Ok -> if (result.interactionIds.isNotEmpty()) {
val values = result.interactionIds.map {
jsonObject(
"interactionId" to it,
"success" to true
)
}
jsonObject["testResults"] = jsonArray(values)
}
}
return jsonObject
}
/**
* Fetches the consumers of the provider that have no associated tag
*/
@Deprecated(message = "Use the version that takes selectors instead",
replaceWith = ReplaceWith("fetchConsumersWithSelectors"))
open fun fetchLatestConsumersWithNoTag(provider: String): List<PactBrokerResult> {
return try {
val halClient = newHalClient()
val consumers = mutableListOf<PactBrokerResult>()
halClient.navigate(mapOf("provider" to provider), LATEST_PROVIDER_PACTS_WITH_NO_TAG)
.forAll(PACTS, Consumer { pact ->
val href = URLDecoder.decode(pact["href"].toString(), UTF8)
val name = pact["name"].toString()
if (options.containsKey("authentication")) {
consumers.add(PactBrokerResult(name, href, pactBrokerUrl, options["authentication"] as List<String>))
} else {
consumers.add(PactBrokerResult(name, href, pactBrokerUrl, emptyList()))
}
})
consumers
} catch (_: NotFoundHalResponse) {
// This means the provider is not defined in the broker, so fail gracefully.
emptyList()
}
}
@Deprecated("Use publishProviderTags", replaceWith = ReplaceWith("publishProviderTags"))
fun publishProviderTag(docAttributes: Map<String, Any?>, name: String, tag: String, version: String) {
try {
val halClient = newHalClient()
.withDocContext(docAttributes)
.navigate(PROVIDER)
logPublishingResults(halClient, version, tag, name)
} catch (e: NotFoundHalResponse) {
logger.error(e) { "Could not tag provider $name, link was missing" }
}
}
private fun logPublishingResults(halClient: IHalClient, version: String, tag: String, name: String) {
when (val result = halClient.putJson(PROVIDER_TAG_VERSION, mapOf("version" to version, "tag" to tag), "{}")) {
is Result.Ok -> logger.debug { "Pushed tag $tag for provider $name and version $version" }
is Result.Err -> logger.error(result.error) {
"Failed to push tag $tag for provider $name and version $version"
}
}
}
override fun publishProviderTags(
docAttributes: Map<String, Any?>,
name: String,
tags: List<String>,
version: String
): Result<Boolean, List<String>> {
try {
val halClient = newHalClient()
.withDocContext(docAttributes)
.navigate(PROVIDER)
val initial: Result<Boolean, List<String>> = Result.Ok(true)
return tags.map { tagName ->
val result = halClient.putJson(PROVIDER_TAG_VERSION, mapOf("version" to version, "tag" to tagName), "{}")
when (result) {
is Result.Ok -> logger.debug { "Pushed tag $tagName for provider $name and version $version" }
is Result.Err -> logger.error(result.error) {
"Failed to push tag $tagName for provider $name and version $version"
}
}
result.mapError { err -> "Publishing tag '$tagName' failed: ${err.message ?: err.toString()}" }
}.fold(initial) { result, v ->
when {
result is Result.Ok && v is Result.Ok -> result
result is Result.Ok && v is Result.Err -> Result.Err(listOf(v.error))
result is Result.Err && v is Result.Ok -> result
result is Result.Err && v is Result.Err -> Result.Err(result.error + v.error)
else -> result
}
}
} catch (e: NotFoundHalResponse) {
logger.error(e) { "Could not tag provider $name, link was missing" }
return Result.Err(listOf("Could not tag provider $name, link was missing"))
}
}
override fun publishProviderBranch(
docAttributes: Map<String, Any?>,
name: String,
branch: String,
version: String
): Result<Boolean, String> {
try {
val halClient = newHalClient()
.withDocContext(docAttributes)
.navigate(PROVIDER)
val result = halClient.putJson(PROVIDER_BRANCH_VERSION,
mapOf("version" to version, "branch" to branch), "{}")
return when (result) {
is Result.Ok<*> -> {
logger.debug { "Pushed branch $branch for provider $name and version $version" }
Result.Ok(true)
}
is Result.Err<Exception> -> {
logger.error(result.error) { "Failed to push branch $branch for provider $name and version $version" }
Result.Err("Publishing branch '$branch' failed: ${result.error.message ?: result.error.toString()}")
}
}