Skip to content

Commit cd1be42

Browse files
authored
Merge pull request #90 from rspieldenner/travisbranches
Travisbranches
2 parents 022b2d8 + b9834c9 commit cd1be42

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

src/integTest/groovy/nebula/plugin/release/ReleasePluginIntegrationSpec.groovy

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package nebula.plugin.release
1717

18+
import com.github.zafarkhaja.semver.Version
1819
import nebula.plugin.bintray.NebulaBintrayPublishingPlugin
1920
import org.ajoberstar.grgit.Tag
2021
import org.gradle.api.plugins.JavaPlugin
@@ -237,6 +238,23 @@ class ReleasePluginIntegrationSpec extends GitVersioningIntegrationSpec {
237238
originGit.tag.list()*.name.contains('v2.0.0')
238239
}
239240

241+
def 'create release on git-flow style branch from within travis context'() {
242+
def twoX = 'release/2.x'
243+
git.tag.add(name: 'v1.0.0')
244+
git.branch.add(name: twoX)
245+
git.push(all: true, tags: true)
246+
git.branch.change(name: twoX, startPoint: "origin/${twoX}".toString())
247+
git.checkout(branch: twoX)
248+
def commit = git.head()
249+
git.checkout(branch: 'HEAD'/*commit.abbreviatedId*/, startPoint: commit.id, createBranch: true)
250+
251+
when:
252+
Version version= inferredVersionForTask('snapshot', '-Prelease.travisci=true', '-Prelease.travisBranch=2.x')
253+
254+
then:
255+
version.toString() == '2.0.0-SNAPSHOT'
256+
}
257+
240258
def 'create new major_minor release branch and have version respected'() {
241259
def oneThreeX = '1.3.x'
242260
git.tag.add(name: 'v1.2.2')

src/main/groovy/nebula/plugin/release/NetflixOssStrategies.groovy

+50
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ import org.ajoberstar.gradle.git.release.semver.ChangeScope
2020
import org.ajoberstar.gradle.git.release.semver.PartialSemVerStrategy
2121
import org.ajoberstar.gradle.git.release.semver.SemVerStrategy
2222
import org.ajoberstar.gradle.git.release.semver.StrategyUtil
23+
import org.gradle.api.GradleException
24+
import org.gradle.api.Project
25+
26+
import java.util.regex.Pattern
27+
28+
import static org.ajoberstar.gradle.git.release.semver.StrategyUtil.incrementNormalFromScope
29+
import static org.ajoberstar.gradle.git.release.semver.StrategyUtil.parseIntOrZero
2330

2431
class NetflixOssStrategies {
32+
static final PartialSemVerStrategy TRAVIS_BRANCH_MAJOR_X = fromTravisPropertyPattern(~/^(\d+)\.x$/)
33+
static final PartialSemVerStrategy TRAVIS_BRANCH_MAJOR_MINOR_X = fromTravisPropertyPattern(~/^(\d+)\.(\d+)\.x$/)
2534
private static final scopes = StrategyUtil.one(Strategies.Normal.USE_SCOPE_PROP,
35+
TRAVIS_BRANCH_MAJOR_X, TRAVIS_BRANCH_MAJOR_MINOR_X,
2636
Strategies.Normal.ENFORCE_GITFLOW_BRANCH_MAJOR_X, Strategies.Normal.ENFORCE_BRANCH_MAJOR_X,
2737
Strategies.Normal.ENFORCE_GITFLOW_BRANCH_MAJOR_MINOR_X, Strategies.Normal.ENFORCE_BRANCH_MAJOR_MINOR_X,
2838
Strategies.Normal.USE_NEAREST_ANY, Strategies.Normal.useScope(ChangeScope.MINOR))
@@ -50,4 +60,44 @@ class NetflixOssStrategies {
5060
state.copyWith(inferredBuildMetadata: metadata)
5161
}
5262
}
63+
64+
static Project project
65+
66+
static final String TRAVIS_BRANCH_PROP = 'release.travisBranch'
67+
68+
static PartialSemVerStrategy fromTravisPropertyPattern(Pattern pattern) {
69+
return StrategyUtil.closure { state ->
70+
println state
71+
if (project.hasProperty(TRAVIS_BRANCH_PROP)) {
72+
def branch = project.property(TRAVIS_BRANCH_PROP)
73+
def m = branch =~ pattern
74+
if (m) {
75+
def major = m.groupCount() >= 1 ? parseIntOrZero(m[0][1]) : -1
76+
def minor = m.groupCount() >= 2 ? parseIntOrZero(m[0][2]) : -1
77+
78+
def normal = state.nearestVersion.normal
79+
def majorDiff = major - normal.majorVersion
80+
def minorDiff = minor - normal.minorVersion
81+
82+
if (majorDiff == 1 && minor <= 0) {
83+
// major is off by one and minor is either 0 or not in the branch name
84+
return incrementNormalFromScope(state, ChangeScope.MAJOR)
85+
} else if (minorDiff == 1 && minor > 0) {
86+
// minor is off by one and specified in the branch name
87+
return incrementNormalFromScope(state, ChangeScope.MINOR)
88+
} else if (majorDiff == 0 && minorDiff == 0 && minor >= 0) {
89+
// major and minor match, both are specified in branch name
90+
return incrementNormalFromScope(state, ChangeScope.PATCH)
91+
} else if (majorDiff == 0 && minor < 0) {
92+
// only major specified in branch name and already matches
93+
return state
94+
} else {
95+
throw new GradleException("Invalid branch (${state.currentBranch.name}) for nearest normal (${normal}).")
96+
}
97+
}
98+
}
99+
100+
return state
101+
}
102+
}
53103
}

src/main/groovy/nebula/plugin/release/ReleasePlugin.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ReleasePlugin implements Plugin<Project> {
5757
@Override
5858
void apply(Project project) {
5959
this.project = project
60+
NetflixOssStrategies.project = project
6061

6162
def gitRoot = project.hasProperty('git.root') ? project.property('git.root') : project.rootProject.projectDir
6263

0 commit comments

Comments
 (0)