spring-cloud-contract icon indicating copy to clipboard operation
spring-cloud-contract copied to clipboard

Enhancement: when doing Git checkout, fallback on master/main if branch not found

Open Sam-Kruglov opened this issue 5 years ago • 6 comments

I am trying to set up the Consumer Driven Contracts workflow while storing stubs in Git. I think it works best if branch name in the pull request of the contracts repo matches exactly the branch name in the consumer/producer pull request. So, I can just use the current branch name (inside consumer/producer repository) as the branch needed in the contracts repository. That works great, but when consumer/producer wants to develop something not related to API changes, then their CI will fail because of this:

Execution default of goal org.springframework.cloud:spring-cloud-contract-maven-plugin:2.1.2.RELEASE:generateTests failed: org.eclipse.jgit.api.errors.RefNotFoundException: Ref feature/BUG-123_null_pointer cannot be resolved

It would be very nice if in that case Spring Cloud Contract could just print a warning and use "master" instead of failing the build.

Sam-Kruglov avatar Sep 13 '19 12:09 Sam-Kruglov

Can you describe your producer / consumer / external repo setup cause I don't understand your flow yet.

marcingrzejszczak avatar Sep 13 '19 12:09 marcingrzejszczak

In contracts Git repo:

  1. Create a branch new_endpoint and push new_endpoint.yml and new_endpoint_stub.json (contracts and mappings directories).

In producer Git repo:

  1. Create a branch new_endpoint and run the spring-cloud-contract-maven-plugin:generateTests maven goal with contractsProperties.git.branch = new_endpoint (with maven configuration). The goal will generate the test which I can run.
  2. Create a branch feature/BUG-123_null_pointer and run the spring-cloud-contract-maven-plugin:generateTests maven goal with contractsProperties.git.branch = feature/BUG-123_null_pointer (with maven configuration). The goal will fail because such a branch is not found in the contracts repository.

I would like in step 3 to just fallback to master branch.

P.S. I am using git-commit-id-plugin to get the git.branch property, which I can then feed into the contract plugin.

Sam-Kruglov avatar Sep 13 '19 12:09 Sam-Kruglov

As a workaround, I am now specifying the branch manually. Hopefully, I don't forget to set it back to master when the endpoint is done

Sam-Kruglov avatar Sep 13 '19 14:09 Sam-Kruglov

I want to try to work on this! gracehopperOSD 👩‍💻

Bondcoder avatar Oct 01 '20 15:10 Bondcoder

Hi, I am Shreya, a contributor to OpenForce 2022. I would like to work on this issue. I would be making a PR as soon as I am done with resolving the issue. Thank you

shreya024 avatar Mar 19 '22 05:03 shreya024

A bit of a drive-by because I'm just randomly ended up here but could this help?

([ -n "$(git rev-parse --verify --quiet main)" ] && echo 'main') || ([ -n "$(git rev-parse --verify --quiet master)" ] && echo 'master')

Will print main if the main branch exists, otherwise master if the master branch exists.

Apologies if that is not along the lines of what you are looking for.

joelpittet avatar Jun 01 '22 06:06 joelpittet