pact-jvm icon indicating copy to clipboard operation
pact-jvm copied to clipboard

Provider branch not sent to Pact Broker in consumer version selectors if enablePending=false

Open misko321 opened this issue 4 months ago • 1 comments

If enablePending = false, the current provider branch name is NOT sent to the Pact Broker when fetching pacts to verify using consumer version selectors.

Because of that e.g. the matchingBranch() selector doesn't work, because the request sent to the Pact Broker doesn't contain which branch is the one to match:

Java code:

@PactBrokerConsumerVersionSelectors
public static SelectorBuilder consumerVersionSelectors() {
    return new SelectorBuilder()
            .matchingBranch()
            .mainBranch();
}

Request made to the Pact Broker by the Pact JVM library:

{
  "consumerVersionSelectors": [
    { "matchingBranch": true },
    { "mainBranch": true }
  ],
  "includePendingStatus": false
}

This results in the Pact Broker returning 400 Bad Request:

{
  "errors": {
    "consumerVersionSelectors": [
      "the providerVersionBranch must be specified when the selector matchingBranch=true is used (at index 0)"
    ]
  }
}

Cause of the issue

The branch name is sent only if enablePending = true. The piece of code responsible for that in PactBrokerClient.kt:

body["includePendingStatus"] = enablePending
if (enablePending) {
    body["providerVersionTags"] = jsonArray(providerTags)
    if (providerBranch.isNotEmpty()) {
        body["providerVersionBranch"] = providerBranch
    }
    if (includeWipPactsSince.isNotEmpty()) {
        body["includeWipPactsSince"] = includeWipPactsSince
    }
}

Why do I consider it a bug?

Using matchingBranch() with enablePending = false seems completely reasonable and the docs seem to confirm that:

matchingBranch() - 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.

Enabling the pending pacts feature stops changed pacts breaking the main provider build. If you are automatically bringing in a pact using the "matching feature branch names" approach, you might want to disable this feature on your feature branches, so that a feature pact correctly fails the branch build until it is fully implemented, and then passes to let you know you can merge.

misko321 avatar Feb 14 '24 08:02 misko321