diff --git a/src/main/groovy/nebula/plugin/release/NetflixOssStrategies.groovy b/src/main/groovy/nebula/plugin/release/NetflixOssStrategies.groovy index fc5fa0a..e8239de 100644 --- a/src/main/groovy/nebula/plugin/release/NetflixOssStrategies.groovy +++ b/src/main/groovy/nebula/plugin/release/NetflixOssStrategies.groovy @@ -25,6 +25,7 @@ import org.gradle.api.Project import java.util.regex.Pattern +import static org.ajoberstar.gradle.git.release.semver.StrategyUtil.all import static org.ajoberstar.gradle.git.release.semver.StrategyUtil.incrementNormalFromScope import static org.ajoberstar.gradle.git.release.semver.StrategyUtil.parseIntOrZero @@ -47,18 +48,36 @@ class NetflixOssStrategies { static final class BuildMetadata { static ReleaseExtension nebulaReleaseExtension - static final PartialSemVerStrategy DEVELOPMENT_METADATA_STRATEGY = { state -> + static final PartialSemVerStrategy BRANCH_METADATA_STRATEGY = { state -> boolean needsBranchMetadata = true nebulaReleaseExtension.releaseBranchPatterns.each { if (state.currentBranch.name =~ it) { needsBranchMetadata = false } } - String shortenedBranch = (state.currentBranch.name =~ nebulaReleaseExtension.shortenedBranchPattern)[0][1] + + if (!needsBranchMetadata) { + return state + } + + def match = state.currentBranch.name =~ + nebulaReleaseExtension.shortenedBranchPattern + if (!match) { + return state + } + String shortenedBranch = match[0][1] shortenedBranch = shortenedBranch.replaceAll(/[_\/-]/, '.') - def metadata = needsBranchMetadata ? "${shortenedBranch}.${state.currentHead.abbreviatedId}" : state.currentHead.abbreviatedId - state.copyWith(inferredBuildMetadata: metadata) + + def inferred = state.inferredBuildMetadata ? + "${shortenedBranch}.${state.inferredBuildMetadata}" : + shortenedBranch + state.copyWith(inferredBuildMetadata: inferred) } + + static final PartialSemVerStrategy DEVELOPMENT_METADATA_STRATEGY = all( + Strategies.BuildMetadata.COMMIT_ABBREVIATED_ID, + BRANCH_METADATA_STRATEGY + ) } static Project project diff --git a/src/test/groovy/nebula/plugin/release/NetflixOssStrategiesSpec.groovy b/src/test/groovy/nebula/plugin/release/NetflixOssStrategiesSpec.groovy new file mode 100644 index 0000000..c248422 --- /dev/null +++ b/src/test/groovy/nebula/plugin/release/NetflixOssStrategiesSpec.groovy @@ -0,0 +1,69 @@ +package nebula.plugin.release + +import org.ajoberstar.gradle.git.release.opinion.Strategies +import org.ajoberstar.gradle.git.release.semver.SemVerStrategyState +import org.ajoberstar.grgit.Branch +import org.ajoberstar.grgit.Commit +import spock.lang.Specification + +import static org.ajoberstar.gradle.git.release.semver.StrategyUtil.all + +class NetflixOssStrategiesSpec extends Specification { + def setup() { + NetflixOssStrategies.BuildMetadata.nebulaReleaseExtension = new ReleaseExtension() + } + + def 'BRANCH_METADATA_STRATEGY adds info extracted by shortenedBranchPattern'() { + given: + def initialState = new SemVerStrategyState( + currentBranch: new Branch(fullName: 'refs/heads/feature/some-cool-feat') + ) + + expect: + NetflixOssStrategies.BuildMetadata.BRANCH_METADATA_STRATEGY.infer(initialState) == + initialState.copyWith(inferredBuildMetadata: 'some.cool.feat') + } + + def 'BRANCH_METADATA_STRATEGY composes with COMMIT_ABBREVIATED_ID'() { + given: + def initialState = new SemVerStrategyState( + currentBranch: new Branch(fullName: 'refs/heads/feature/some-cool-feat'), + currentHead: new Commit(id: 'ac6a4b15e1a9b02970937f327cc6890226d80210') + ) + def strategy = all( + Strategies.BuildMetadata.COMMIT_ABBREVIATED_ID, + NetflixOssStrategies.BuildMetadata.BRANCH_METADATA_STRATEGY + ) + + expect: + strategy.infer(initialState) == initialState.copyWith( + inferredBuildMetadata: 'some.cool.feat.ac6a4b1' + ) + } + + def 'BRANCH_METADATA_STRATEGY does nothing if branch excluded by releaseBranchPatterns'() { + given: + def initialState = stateWithBranch('master') + + expect: + NetflixOssStrategies.BuildMetadata.BRANCH_METADATA_STRATEGY. + infer(initialState) == initialState + } + + def 'BRANCH_METADATA_STRATEGY does nothing if branch does not match shortenedBranchPattern'() { + given: + NetflixOssStrategies.BuildMetadata.nebulaReleaseExtension. + shortenedBranchPattern = /(?:(?:bugfix|feature|hotfix|release)(?:-|\/))?(\d+)/ + def initialState = stateWithBranch('feature/some-cool-feat') + + expect: + NetflixOssStrategies.BuildMetadata.BRANCH_METADATA_STRATEGY. + infer(initialState) == initialState + } + + private static SemVerStrategyState stateWithBranch(String name) { + new SemVerStrategyState( + currentBranch: new Branch(fullName: "refs/heads/${name}") + ) + } +}