Skip to content

Commit

Permalink
Handle repos with no commits
Browse files Browse the repository at this point in the history
Repos with no commits caused an IllegalArgumentException when trying to
set the build metadata. This change models repos with no commits by
having the commit ID be an Optional and falls back to "uncommitted" if
no commit ID is found when settting the build metadata.

This fixes #68.
  • Loading branch information
ajoberstar committed Apr 7, 2018
1 parent 8bae205 commit b3531c3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public VcsInventory(
.orElse(Collections.emptySet());
}

public String getCommitId() {
return commitId;
public Optional<String> getCommitId() {
return Optional.ofNullable(commitId);
}

public Optional<Version> getCurrentVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Version reckonTargetVersion(VcsInventory inventory, Version targetNormal)
return targetBase
.setPreReleaseVersion(
baseStageName + "." + baseStageNum + "." + inventory.getCommitsSinceBase())
.setBuildMetadata(inventory.getCommitId());
.setBuildMetadata(inventory.getCommitId().orElse("uncommitted"));
} else if (stage.equals(baseStageName)) {
int num = baseStageNum > 0 ? baseStageNum + 1 : 1;
return targetBase.setPreReleaseVersion(stage + "." + num);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ class StagePreReleaseStrategyTest extends Specification {
strategy('rc').reckonTargetVersion(inventory, Version.valueOf('2.0.0')) == Version.valueOf('2.0.0-rc.1')
}

def 'if repo has no commits, show build metadata as uncommitted'() {
given:
def inventoryUncommitted = new VcsInventory(
null,
null,
Version.valueOf('1.2.3-milestone.2'),
Version.valueOf('1.2.2'),
5,
[] as Set,
[] as Set
)
expect:
strategy(null).reckonTargetVersion(inventoryUncommitted, Version.valueOf('1.2.3')) == Version.valueOf('1.2.3-milestone.2.5+uncommitted')

}

private StagePreReleaseStrategy strategy(String stage) {
return new StagePreReleaseStrategy(['initial', 'milestone', 'rc', 'final'] as Set, { i, v -> Optional.ofNullable(stage) })
}
Expand Down

0 comments on commit b3531c3

Please sign in to comment.