Skip to content

Commit 85e67de

Browse files
committed
versionWithBranch respecing release tags | fixes #969
1 parent 9e65d11 commit 85e67de

File tree

5 files changed

+109
-19
lines changed

5 files changed

+109
-19
lines changed

src/main/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreator.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ enum PredefinedVersionCreator {
1010
}),
1111

1212
VERSION_WITH_BRANCH('versionWithBranch', { String versionFromTag, ScmPosition position ->
13-
if ((position.branch != 'master' && position.branch != 'main') && position.branch != 'HEAD') {
13+
if (!position.isReleaseBranch && position.branch != 'HEAD' && !position.isTagRef) {
1414
return "$versionFromTag-$position.branch".toString()
1515
}
1616
return versionFromTag
1717
}),
1818

1919
VERSION_WITH_COMMIT_HASH('versionWithCommitHash', { String versionFromTag, ScmPosition position ->
20-
if ((position.branch != 'master' && position.branch != 'main') && position.branch != 'HEAD') {
20+
if (!position.isReleaseBranch && position.branch != 'HEAD' && !position.isTagRef) {
2121
return "$versionFromTag-$position.shortRevision".toString()
2222
}
2323
return versionFromTag

src/main/java/pl/allegro/tech/build/axion/release/domain/scm/ScmPosition.java

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,31 @@ public class ScmPosition {
88
private final String shortRevision;
99
private final String branch;
1010
private final boolean isClean;
11+
private final boolean isReleaseBranch;
12+
private final boolean isTagRef;
1113

12-
public ScmPosition(String revision, String shortRevision, String branch, boolean isClean) {
14+
public ScmPosition(String revision, String shortRevision, String branch, boolean isClean, boolean isReleaseBranch, boolean isTagRef) {
1315
this.revision = revision;
1416
this.shortRevision = shortRevision;
1517
this.branch = branch;
1618
this.isClean = isClean;
19+
this.isReleaseBranch = isReleaseBranch;
20+
this.isTagRef = isTagRef;
21+
}
22+
23+
public ScmPosition(String revision, String shortRevision, String branch, boolean isClean, boolean isReleaseBranch) {
24+
this(revision, shortRevision, branch, isClean, isReleaseBranch, false);
25+
}
26+
27+
public ScmPosition(String revision, String shortRevision, String branch, boolean isClean) {
28+
this(revision, shortRevision, branch, isClean, false, false);
1729
}
1830

1931
public ScmPosition(String revision, String shortRevision, String branch) {
20-
this(revision, shortRevision, branch, true);
32+
this(revision, shortRevision, branch, true, false, false);
2133
}
2234

23-
public ScmPosition(String revision, String branch, boolean isClean) {
35+
public ScmPosition(String revision, String branch, boolean isClean, boolean isReleaseBranch, boolean isTagRef) {
2436
this.revision = revision;
2537
if (revision.length() > 7) {
2638
this.shortRevision = revision.substring(0, 7);
@@ -29,18 +41,30 @@ public ScmPosition(String revision, String branch, boolean isClean) {
2941
}
3042
this.branch = branch;
3143
this.isClean = isClean;
44+
this.isReleaseBranch = isReleaseBranch;
45+
this.isTagRef = isTagRef;
46+
}
47+
48+
public ScmPosition(String revision, String branch, boolean isClean, boolean isReleaseBranch) {
49+
this(revision, branch, isClean, isReleaseBranch, false);
50+
}
51+
52+
public ScmPosition(String revision, String branch, boolean isClean) {
53+
this(revision, branch, isClean, false, false);
3254
}
3355

3456
public ScmPosition(String revision, String branch) {
35-
this(revision, branch, true);
57+
this(revision, branch, true, false, false);
3658
}
3759

3860
@Override
3961
public String toString() {
4062
return "ScmPosition[revision = " + revision
4163
+ ", shortRevision = " + shortRevision
4264
+ ", branch = " + branch
43-
+ ", isClean = " + isClean + "]";
65+
+ ", isClean = " + isClean
66+
+ ", isReleaseBranch = " + isReleaseBranch
67+
+ ", isTagRef = " + isTagRef + "]";
4468
}
4569

4670
@Input
@@ -62,4 +86,14 @@ public String getBranch() {
6286
public boolean getIsClean() {
6387
return isClean;
6488
}
89+
90+
@Input
91+
public boolean getIsReleaseBranch() {
92+
return isReleaseBranch;
93+
}
94+
95+
@Input
96+
public boolean getIsTagRef() {
97+
return isTagRef;
98+
}
6599
}

src/main/java/pl/allegro/tech/build/axion/release/infrastructure/git/GitRepository.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
import static java.util.stream.Collectors.toList;
5858
import static pl.allegro.tech.build.axion.release.TagPrefixConf.fullLegacyPrefix;
59+
import static pl.allegro.tech.build.axion.release.TagPrefixConf.fullPrefix;
5960

6061
public class GitRepository implements ScmRepository {
6162
private static final Logger logger = Logging.getLogger(GitRepository.class);
@@ -208,7 +209,7 @@ private ScmPushResult verifyPushResults(Iterable<PushResult> pushResults) {
208209

209210
Optional<RemoteRefUpdate> failedRefUpdate = pushResult.getRemoteUpdates().stream().filter(ref ->
210211
!ref.getStatus().equals(RemoteRefUpdate.Status.OK)
211-
&& !ref.getStatus().equals(RemoteRefUpdate.Status.UP_TO_DATE)
212+
&& !ref.getStatus().equals(RemoteRefUpdate.Status.UP_TO_DATE)
212213
).findFirst();
213214

214215
boolean isSuccess = failedRefUpdate.isEmpty();
@@ -308,7 +309,9 @@ public ScmPosition positionOfLastChangeIn(String path, List<String> excludeSubFo
308309
return new ScmPosition(
309310
lastCommit.getName(),
310311
currentPosition.getBranch(),
311-
currentPosition.getIsClean()
312+
currentPosition.getIsClean(),
313+
currentPosition.getIsReleaseBranch(),
314+
currentPosition.getIsTagRef()
312315
);
313316
}
314317

@@ -350,7 +353,14 @@ public ScmPosition currentPosition() {
350353
String revision = getRevision();
351354
String branchName = branchName();
352355
boolean isClean = !checkUncommittedChanges();
353-
return new ScmPosition(revision, branchName, isClean);
356+
boolean isReleaseBranch = properties.getReleaseBranchNames() != null && properties.getReleaseBranchNames().contains(branchName);
357+
boolean isTagRef = isVersionTagRef(properties.getOverriddenBranchName() != null ? properties.getOverriddenBranchName() : branchName);
358+
return new ScmPosition(revision, branchName, isClean, isReleaseBranch, isTagRef);
359+
}
360+
361+
private boolean isVersionTagRef(String branchName) {
362+
return branchName.startsWith(GIT_TAG_PREFIX + fullPrefix())
363+
|| branchName.startsWith(GIT_TAG_PREFIX + fullLegacyPrefix());
354364
}
355365

356366
private String getRevision() {
@@ -620,7 +630,7 @@ public boolean isLegacyDefTagnameRepo() {
620630
List<Ref> call = jgitRepository.tagList().call();
621631
if (call.isEmpty()) return false;
622632

623-
return call.stream().allMatch(ref -> ref.getName().startsWith("refs/tags/" + fullLegacyPrefix()));
633+
return call.stream().allMatch(ref -> ref.getName().startsWith(GIT_TAG_PREFIX + fullLegacyPrefix()));
624634
} catch (GitAPIException e) {
625635
throw new ScmException(e);
626636
}

src/test/groovy/pl/allegro/tech/build/axion/release/domain/PredefinedVersionCreatorTest.groovy

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,61 @@ class PredefinedVersionCreatorTest extends Specification {
1212
PredefinedVersionCreator.SIMPLE.versionCreator.apply('version',scmPosition('master')) == 'version'
1313
}
1414

15-
def "versionWithBranch version creator should return simple version when on master"() {
15+
def "versionWithBranch should not append on release branch"() {
1616
expect:
17-
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition('master')) == 'version'
17+
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition().withBranch('release').asReleaseBranch().build()) == 'version'
1818
}
1919

20-
def "versionWithBranch version creator should return version with appended branch name when not on master"() {
20+
def "versionWithBranch should append branch name when not on release branch"() {
2121
expect:
22-
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition('branch')) == 'version-branch'
22+
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', scmPosition('feature/branch')) == 'version-feature/branch'
2323
}
2424

25-
def "versionWithCommitHash version creator should return simple version when on main"() {
25+
def "versionWithCommitHash should not append on release branch"() {
2626
expect:
27-
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', scmPosition('main')) == 'version'
27+
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', scmPosition().withBranch('release').asReleaseBranch().build()) == 'version'
2828
}
2929

30-
def "versionWithCommitHash version creator should return version with appended short SHA-1 hash when not on main"() {
30+
def "versionWithCommitHash should append short SHA-1 hash when not on release branch"() {
3131
expect:
3232
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', scmPosition('branch')) == 'version-c143976'
3333
}
3434

35+
def "VERSION_WITH_BRANCH should not append when ref is a version tag"() {
36+
given:
37+
def pos = scmPosition().withBranch('refs/tags/v1.0.0').build()
38+
expect:
39+
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', pos) == 'version'
40+
}
41+
42+
def "VERSION_WITH_BRANCH should not append when ref is a random tag"() {
43+
given:
44+
def pos = scmPosition().withBranch('refs/tags/random-tag').build()
45+
expect:
46+
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', pos) == 'version-refs/tags/random-tag'
47+
}
48+
49+
def "VERSION_WITH_BRANCH should append when ref is a version tag"() {
50+
given:
51+
def pos = scmPosition().withBranch('refs/tags/v1.0.0').build()
52+
expect:
53+
PredefinedVersionCreator.VERSION_WITH_BRANCH.versionCreator.apply('version', pos) == 'version'
54+
}
55+
56+
def "VERSION_WITH_COMMIT_HASH should append when ref is a random tag"() {
57+
given:
58+
def pos = scmPosition().withBranch('refs/tags/random-tag').build()
59+
expect:
60+
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', pos) == 'version-c143976'
61+
}
62+
63+
def "VERSION_WITH_COMMIT_HASH should not append when ref is a version tag"() {
64+
given:
65+
def pos = scmPosition().withBranch('refs/tags/v1.0.0').build()
66+
expect:
67+
PredefinedVersionCreator.VERSION_WITH_COMMIT_HASH.versionCreator.apply('version', pos) == 'version'
68+
}
69+
3570
def "should return version creator of given type"() {
3671
expect:
3772
PredefinedVersionCreator.versionCreatorFor('simple').apply('version', null) == 'version'

src/test/groovy/pl/allegro/tech/build/axion/release/domain/scm/ScmPositionBuilder.groovy

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package pl.allegro.tech.build.axion.release.domain.scm
22

3+
import static pl.allegro.tech.build.axion.release.TagPrefixConf.fullLegacyPrefix
4+
import static pl.allegro.tech.build.axion.release.TagPrefixConf.fullPrefix
5+
36
class ScmPositionBuilder {
47

58
private String branch = 'master'
@@ -10,6 +13,8 @@ class ScmPositionBuilder {
1013

1114
private boolean isClean = true
1215

16+
private boolean isReleaseBranch = false
17+
1318
private ScmPositionBuilder() {
1419
}
1520

@@ -22,7 +27,8 @@ class ScmPositionBuilder {
2227
}
2328

2429
ScmPosition build() {
25-
return new ScmPosition(revision, shortRevision, branch, isClean)
30+
def isTagRef = branch.startsWith("refs/tags/" + fullPrefix()) || branch.startsWith("refs/tags/" + fullLegacyPrefix());
31+
return new ScmPosition(revision, shortRevision, branch, isClean, isReleaseBranch, isTagRef)
2632
}
2733

2834
ScmPositionBuilder withBranch(String branch) {
@@ -40,4 +46,9 @@ class ScmPositionBuilder {
4046
this.isClean = false
4147
return this
4248
}
49+
50+
ScmPositionBuilder asReleaseBranch() {
51+
this.isReleaseBranch = true
52+
return this
53+
}
4354
}

0 commit comments

Comments
 (0)