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

Fix: Issue with the next development version logic. #67

Open
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
4 changes: 4 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,9 @@
<groupId>org.apache.ivy</groupId>
<artifactId>ivy</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import org.jetbrains.annotations.Nullable;
import org.jfrog.teamcity.common.RunnerParameterKeys;
import org.jfrog.teamcity.server.global.DeployableArtifactoryServers;
import org.jfrog.teamcity.server.project.strategy.NextDevelopmentVersion;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.jfrog.teamcity.common.ConstantValues.NAME;
import static org.jfrog.teamcity.server.project.strategy.NextDevelopmentVersion.StrategyEnum.valueOf;

/**
* @author Noam Y. Tenne
Expand Down Expand Up @@ -78,6 +80,13 @@ public void fillModel(@NotNull Map<String, Object> model, @NotNull HttpServletRe
// fill default values to the model
ReleaseManagementConfigModel managementConfig = getReleaseManagementConfigModel();

final Map<String, String> configParameters = buildType.getConfigParameters();
final String nextDevelopmentVersionStrategy = configParameters.get(NextDevelopmentVersion.PARAMETER_NAME);

try {
managementConfig.setNextDevelopmentVersionStrategy(valueOf(nextDevelopmentVersionStrategy.toUpperCase()));
} catch (IllegalArgumentException ignored) {}

Map<String, String> parameters = buildRunner.get(0).getParameters();
if (parameters.containsKey(RunnerParameterKeys.GIT_RELEASE_BRANCH_NAME_PREFIX)) {
managementConfig.setGitReleaseBranchNamePrefix(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.common.collect.Lists;
import jetbrains.buildServer.serverSide.BranchEx;
import org.apache.commons.lang.StringUtils;
import org.jfrog.teamcity.server.project.strategy.NextDevelopmentVersion.StrategyEnum;
import org.jfrog.teamcity.server.project.strategy.NextDevelopmentVersionStrategy;

import java.util.List;

Expand All @@ -41,6 +43,7 @@ public abstract class ReleaseManagementConfigModel {
private boolean selectedArtifactoryServerHasAddons = false;
private List<String> deployableRepoKeys = Lists.newArrayList();
private BranchEx defaultCheckoutBranch;
private NextDevelopmentVersionStrategy nextDevelopmentVersionStrategy;

public void setRootArtifactId(String rootArtifactId) {
this.rootArtifactId = rootArtifactId;
Expand All @@ -59,33 +62,29 @@ public String getReleaseVersion() {
}

public String getNextDevelopmentVersion() {
String fromVersion = getReleaseVersion();
String nextVersion;
int lastDotIndex = fromVersion.lastIndexOf('.');
try {
if (lastDotIndex != -1) {
// probably a major minor version e.g., 2.1.1
String minorVersionToken = fromVersion.substring(lastDotIndex + 1);
String nextMinorVersion;
int lastDashIndex = minorVersionToken.lastIndexOf('-');
if (lastDashIndex != -1) {
// probably a minor-buildNum e.g., 2.1.1-4 (should change to 2.1.1-5)
String buildNumber = minorVersionToken.substring(lastDashIndex + 1);
int nextBuildNumber = Integer.parseInt(buildNumber) + 1;
nextMinorVersion = minorVersionToken.substring(0, lastDashIndex + 1) + nextBuildNumber;
} else {
nextMinorVersion = Integer.toString(Integer.parseInt(minorVersionToken) + 1);
}
nextVersion = fromVersion.substring(0, lastDotIndex + 1) + nextMinorVersion;
} else {
// maybe it's just a major version; try to parse as an int
int nextMajorVersion = Integer.parseInt(fromVersion) + 1;
nextVersion = Integer.toString(nextMajorVersion);
}
} catch (NumberFormatException e) {
return fromVersion;
if (nextDevelopmentVersionStrategy == null) {
nextDevelopmentVersionStrategy = StrategyEnum.DEFAULT;
}
return nextVersion + "-SNAPSHOT";
final List<Integer> versionParts = nextDevelopmentVersionStrategy.apply(getReleaseVersion());
final StringBuilder nextVersion = new StringBuilder();

int buildNumber = versionParts.get(0);
int patch = versionParts.get(1);
int minor = versionParts.get(2);
int major = versionParts.get(3);

nextVersion.append( major );
nextVersion.append(".").append(minor);
nextVersion.append(".").append(patch);

if ( buildNumber > 0 )
{
nextVersion.append("-").append(buildNumber);
}

nextVersion.append( "-SNAPSHOT" );

return nextVersion.toString();
}

public abstract String getDefaultTagUrl();
Expand Down Expand Up @@ -120,10 +119,6 @@ public String getTagComment() {
return COMMIT_COMMENT_PREFIX + "Release version " + getReleaseVersion();
}

public String getDefaultNextDevelopmentVersionComment() {
return COMMIT_COMMENT_PREFIX + "Next development version";
}

public boolean isGitVcs() {
return gitVcs;
}
Expand Down Expand Up @@ -160,6 +155,10 @@ public void setDefaultCheckoutBranch(BranchEx defaultCheckoutBranch) {
this.defaultCheckoutBranch = defaultCheckoutBranch;
}

public void setNextDevelopmentVersionStrategy(NextDevelopmentVersionStrategy nextDevelopmentVersionStrategy) {
this.nextDevelopmentVersionStrategy = nextDevelopmentVersionStrategy;
}

protected String getVcsSpecificTagBaseUrlOrName() {
if (StringUtils.isBlank(vcsTagBaseUrlOrName)) {
return "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.jfrog.teamcity.server.project.strategy;

import org.codehaus.mojo.buildhelper.versioning.VersionInformation;
import org.jfrog.teamcity.common.ConstantValues;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class NextDevelopmentVersion {
private final static String PREFIX = ConstantValues.PLUGIN_PREFIX + "releaseManagement.";
public final static String PARAMETER_NAME = PREFIX + "nextDevelopmentVersionStrategy";

public enum StrategyEnum implements NextDevelopmentVersionStrategy {
DEFAULT(),
IGNORE_ZEROS();

public List<Integer> apply(String releaseVersion) {
final VersionInformation versionInformation = new VersionInformation(releaseVersion);
final List<Integer> versionParts = new ArrayList<Integer>();

versionParts.add(versionInformation.getBuildNumber());
versionParts.add(versionInformation.getPatch());
versionParts.add(versionInformation.getMinor());
versionParts.add(versionInformation.getMajor());

ListIterator<Integer> listIterator = versionParts.listIterator();
boolean done = false;

while (!done && listIterator.hasNext()) {
switch (this) {

case IGNORE_ZEROS:
done = applyIgnoreZero(listIterator);
break;

case DEFAULT:
default:
done = applyDefault(listIterator);
break;
}
}

return versionParts;
}
}

private static boolean applyDefault(ListIterator<Integer> i) {
int versionPart = i.next();
boolean done = false;

if (i.previousIndex() + versionPart > 0) {
i.set(versionPart + 1);
done = true;
}

return done;
}

private static boolean applyIgnoreZero(ListIterator<Integer> i) {
int versionPart = i.next();
boolean done = false;

if (versionPart > 0) {
i.set(versionPart + 1);
done = true;
}

return done;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jfrog.teamcity.server.project.strategy;

import java.util.List;

public interface NextDevelopmentVersionStrategy {
List<Integer> apply(String releaseVersion);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.jfrog.teamcity.server.project;

import org.jfrog.teamcity.server.project.strategy.NextDevelopmentVersion.StrategyEnum;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import static org.jfrog.teamcity.server.project.strategy.NextDevelopmentVersion.StrategyEnum.DEFAULT;
import static org.jfrog.teamcity.server.project.strategy.NextDevelopmentVersion.StrategyEnum.IGNORE_ZEROS;

public class ReleaseManagementConfigModelTest {
private final static StrategyEnum UNKNOWN = null;

private ReleaseManagementConfigModel instance = new ReleaseManagementConfigModel() {
@Override
public String getDefaultTagUrl() {
return null;
}

@Override
public String getDefaultReleaseBranch() {
return null;
}
};

@DataProvider(name = "data-provider")
public Object[][] dataProviderMethod() {
return new Object[][]{
{UNKNOWN, "6.0.0", "6.0.1-SNAPSHOT"},
{UNKNOWN, "6.1.0", "6.1.1-SNAPSHOT"},
{UNKNOWN, "6.0.1", "6.0.2-SNAPSHOT"},
{UNKNOWN, "6.1.0-0", "6.1.1-SNAPSHOT"},
{UNKNOWN, "6.1.0-22", "6.1.0-23-SNAPSHOT"},
{UNKNOWN, "6.1", "6.1.1-SNAPSHOT"},
{UNKNOWN, "6", "6.0.1-SNAPSHOT"},

{DEFAULT, "6.0.0", "6.0.1-SNAPSHOT"},
{DEFAULT, "6.1.0", "6.1.1-SNAPSHOT"},
{DEFAULT, "6.0.1", "6.0.2-SNAPSHOT"},
{DEFAULT, "6.1.0-0", "6.1.1-SNAPSHOT"},
{DEFAULT, "6.1.0-22", "6.1.0-23-SNAPSHOT"},
{DEFAULT, "6.1", "6.1.1-SNAPSHOT"},
{DEFAULT, "6", "6.0.1-SNAPSHOT"},

{IGNORE_ZEROS, "6.0.0", "7.0.0-SNAPSHOT"},
{IGNORE_ZEROS, "6.1.0", "6.2.0-SNAPSHOT"},
{IGNORE_ZEROS, "6.0.1", "6.0.2-SNAPSHOT"},
{IGNORE_ZEROS, "6.1.0-0", "6.2.0-SNAPSHOT"},
{IGNORE_ZEROS, "6.1.0-22", "6.1.0-23-SNAPSHOT"},
{IGNORE_ZEROS, "6.1", "6.2.0-SNAPSHOT"},
{IGNORE_ZEROS, "6", "7.0.0-SNAPSHOT"}};
}

@Test(dataProvider = "data-provider")
public void testGetNextDevelopmentVersion(StrategyEnum strategy, String releaseVersion, String expected) {
instance.setNextDevelopmentVersionStrategy(strategy);
instance.setCurrentVersion(releaseVersion);

final String current = instance.getNextDevelopmentVersion();

Assert.assertEquals(current, expected);
}
}