Skip to content

Instantly share code, notes, and snippets.

@wololock
Created May 14, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wololock/94e1ca3579486fdc6468f1fc9646a8b4 to your computer and use it in GitHub Desktop.
Save wololock/94e1ca3579486fdc6468f1fc9646a8b4 to your computer and use it in GitHub Desktop.
import groovy.transform.Field
@Field
def profiles = [
prod: ["master", /release\/.*/],
dev: ["develop", /PR-.*/]
]
String getProfileForBranchName(String branchName, String defaultProfile = "default") {
return profiles.findResult { profile, candidates -> candidates.any { branchName ==~ it } ? profile : null } ?: defaultProfile
}
["PR-123", "master", "release/1.0.0", "foo", "develop"].each { branchName ->
println "Profile for branch $branchName is ${getProfileForBranchName(branchName)}"
}
// Output:
// Profile for branch PR-123 is dev
// Profile for branch master is prod
// Profile for branch release/1.0.0 is prod
// Profile for branch foo is default
// Profile for branch develop is dev
@Holyhawk
Copy link

Thanks a lot! It was a super helpful example. I did not exactly use this code but I used to play around and it helped me to come up with a good idea :) lets see if it works...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment