Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle no-op cases in DEVELOPMENT_METADATA_STRATEGY #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/main/groovy/nebula/plugin/release/NetflixOssStrategies.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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}")
)
}
}