Skip to content

Commit 8f7f01c

Browse files
authored
Merge pull request #93 from ADI10HERO/version-ambiguity
feat: add version mismatch handler
2 parents 499767b + d54543a commit 8f7f01c

File tree

3 files changed

+83
-15
lines changed

3 files changed

+83
-15
lines changed

src/main/java/io/jenkins/plugins/conventionalcommits/NextVersionStep.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,10 @@ protected String run() throws Exception {
233233
}
234234
}
235235

236-
Version currentVersion = new CurrentVersion().getCurrentVersion(dir, latestTag);
236+
Version currentVersion =
237+
new CurrentVersion()
238+
.getCurrentVersion(
239+
dir, latestTag, getContext().get(TaskListener.class).getLogger());
237240

238241
String commitMessagesString = null;
239242
if (latestTag.isEmpty()) {

src/main/java/io/jenkins/plugins/conventionalcommits/utils/CurrentVersion.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.jenkins.plugins.conventionalcommits.process.ProcessHelper;
66
import java.io.File;
77
import java.io.IOException;
8+
import java.io.PrintStream;
89

910
/** This class focus on getting the current version (latest release version) of a project. */
1011
public class CurrentVersion {
@@ -19,6 +20,32 @@ private Version getCurrentVersionTag(String latestTag) {
1920
return Version.valueOf(latestTag.isEmpty() ? "0.0.0" : latestTag);
2021
}
2122

23+
/**
24+
* Compares read version with .
25+
*
26+
* @param currentVersion Project's current version read from the configuration file.
27+
* @param latestTag The last tagged version.
28+
* @param logger Jenkins logger.
29+
* @return The latest version as the new current version (based on Semver).
30+
*/
31+
private Version checkCurrentVersion(
32+
Version currentVersion, String latestTag, PrintStream logger) {
33+
34+
Version tagVersion = Version.valueOf(latestTag);
35+
36+
if (tagVersion.greaterThan(currentVersion)) {
37+
String message =
38+
"[WARNING]: Version mismatch found between the configuration file and the latest tag. "
39+
+ "Using the later version: "
40+
+ "%1$s as the base version to calculate the next version.%n";
41+
logger.format(message, latestTag);
42+
logger.flush();
43+
currentVersion = tagVersion;
44+
}
45+
46+
return currentVersion;
47+
}
48+
2249
/**
2350
* Return the next version of the version attribute.
2451
*
@@ -28,7 +55,7 @@ private Version getCurrentVersionTag(String latestTag) {
2855
* @throws IOException If an error occur reading files.
2956
* @throws InterruptedException If an error occurs while executing command using processHelper.
3057
*/
31-
public Version getCurrentVersion(File directory, String latestTag)
58+
public Version getCurrentVersion(File directory, String latestTag, PrintStream logger)
3259
throws IOException, InterruptedException {
3360

3461
Version currentVersion;
@@ -43,6 +70,10 @@ public Version getCurrentVersion(File directory, String latestTag)
4370
currentVersion = getCurrentVersionTag(latestTag);
4471
}
4572

73+
if (!latestTag.isEmpty()) {
74+
currentVersion = checkCurrentVersion(currentVersion, latestTag, logger);
75+
}
76+
4677
return currentVersion;
4778
}
4879
}

src/test/java/io/jenkins/plugins/conventionalcommits/utils/CurrentVersionTest.java

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99
import org.mockito.Mock;
1010
import org.mockito.junit.MockitoJUnitRunner;
1111

12-
import java.io.File;
13-
import java.io.FileWriter;
14-
import java.io.IOException;
12+
import java.io.*;
1513
import java.util.Arrays;
1614
import java.util.List;
1715

@@ -27,6 +25,8 @@ public class CurrentVersionTest {
2725

2826
@Mock private ProcessHelper processHelper;
2927

28+
@Mock private PrintStream mockedLogger;
29+
3030
@Test
3131
public void testMavenProjectVersion() throws IOException, InterruptedException {
3232

@@ -63,7 +63,7 @@ public void testMavenProjectVersion() throws IOException, InterruptedException {
6363
CurrentVersion currentVersion = new CurrentVersion();
6464
currentVersion.setProcessHelper(processHelper);
6565

66-
Version testCurrentVersion = currentVersion.getCurrentVersion(mavenDir, "");
66+
Version testCurrentVersion = currentVersion.getCurrentVersion(mavenDir, "", mockedLogger);
6767

6868
assertThat(testCurrentVersion, is(notNullValue()));
6969
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -105,7 +105,7 @@ public void testGradleProjectVersion() throws IOException, InterruptedException
105105
CurrentVersion currentVersion = new CurrentVersion();
106106
currentVersion.setProcessHelper(processHelper);
107107

108-
Version testCurrentVersion = currentVersion.getCurrentVersion(gradleDir, "");
108+
Version testCurrentVersion = currentVersion.getCurrentVersion(gradleDir, "", mockedLogger);
109109

110110
assertThat(testCurrentVersion, is(notNullValue()));
111111
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -126,7 +126,7 @@ public void testMakeProjectVersion() throws IOException, InterruptedException {
126126

127127
Version actualCurrentVersion = Version.valueOf("1.10.0");
128128
CurrentVersion currentVersion = new CurrentVersion();
129-
Version testCurrentVersion = currentVersion.getCurrentVersion(makeDir, "");
129+
Version testCurrentVersion = currentVersion.getCurrentVersion(makeDir, "", mockedLogger);
130130

131131
assertThat(testCurrentVersion, is(notNullValue()));
132132
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -149,7 +149,7 @@ public void testNpmProjectVersion() throws IOException, InterruptedException {
149149

150150
Version actualCurrentVersion = Version.valueOf("1.0.0");
151151
CurrentVersion currentVersion = new CurrentVersion();
152-
Version testCurrentVersion = currentVersion.getCurrentVersion(npmDir, "");
152+
Version testCurrentVersion = currentVersion.getCurrentVersion(npmDir, "", mockedLogger);
153153

154154
assertThat(testCurrentVersion, is(notNullValue()));
155155
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -191,7 +191,7 @@ public void testPythonProjectVersionSetupPyExists() throws IOException, Interrup
191191
CurrentVersion currentVersion = new CurrentVersion();
192192
currentVersion.setProcessHelper(processHelper);
193193

194-
Version testCurrentVersion = currentVersion.getCurrentVersion(pyDir, "");
194+
Version testCurrentVersion = currentVersion.getCurrentVersion(pyDir, "", mockedLogger);
195195

196196
assertThat(testCurrentVersion, is(notNullValue()));
197197
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -212,7 +212,7 @@ public void testPythonProjectVersionSetupCfgExists() throws IOException, Interru
212212

213213
Version actualCurrentVersion = Version.valueOf("0.1.0");
214214
CurrentVersion currentVersion = new CurrentVersion();
215-
Version testCurrentVersion = currentVersion.getCurrentVersion(pyDir, "");
215+
Version testCurrentVersion = currentVersion.getCurrentVersion(pyDir, "", mockedLogger);
216216

217217
assertThat(testCurrentVersion, is(notNullValue()));
218218
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -241,7 +241,7 @@ public void shouldReadHelmChartCurrentVersion() throws IOException, InterruptedE
241241

242242
Version actualCurrentVersion = Version.valueOf("1.0.0");
243243
CurrentVersion currentVersion = new CurrentVersion();
244-
Version testCurrentVersion = currentVersion.getCurrentVersion(helmDir, "");
244+
Version testCurrentVersion = currentVersion.getCurrentVersion(helmDir, "", mockedLogger);
245245
assertThat(testCurrentVersion, is(notNullValue()));
246246
assertThat(actualCurrentVersion, is(testCurrentVersion));
247247
}
@@ -266,7 +266,7 @@ public void testPythonProjectVersionPyProjectTOMLExists()
266266

267267
Version actualCurrentVersion = Version.valueOf("1.0.0");
268268
CurrentVersion currentVersion = new CurrentVersion();
269-
Version testCurrentVersion = currentVersion.getCurrentVersion(pyDir, "");
269+
Version testCurrentVersion = currentVersion.getCurrentVersion(pyDir, "", mockedLogger);
270270

271271
assertThat(testCurrentVersion, is(notNullValue()));
272272
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -279,7 +279,7 @@ public void CurrentVersion_NoProjectWithTag() throws IOException, InterruptedExc
279279
Version actualCurrentVersion = Version.valueOf("0.1.0");
280280

281281
CurrentVersion currentVersion = new CurrentVersion();
282-
Version testCurrentVersion = currentVersion.getCurrentVersion(testDir, "0.1.0");
282+
Version testCurrentVersion = currentVersion.getCurrentVersion(testDir, "0.1.0", mockedLogger);
283283

284284
assertThat(testCurrentVersion, is(notNullValue()));
285285
assertThat(actualCurrentVersion, is(testCurrentVersion));
@@ -292,9 +292,43 @@ public void CurrentVersion_NoProjectNoTag() throws IOException, InterruptedExcep
292292
Version actualCurrentVersion = Version.valueOf("0.0.0");
293293

294294
CurrentVersion currentVersion = new CurrentVersion();
295-
Version testCurrentVersion = currentVersion.getCurrentVersion(testDir, "");
295+
Version testCurrentVersion = currentVersion.getCurrentVersion(testDir, "", mockedLogger);
296296

297297
assertThat(testCurrentVersion, is(notNullValue()));
298298
assertThat(actualCurrentVersion, is(testCurrentVersion));
299299
}
300+
301+
@Test
302+
public void CurrentVersionMismatchWithTag() throws IOException, InterruptedException {
303+
304+
File testDir = rootFolder.newFolder("SampleProject");
305+
Version actualCurrentVersion = Version.valueOf("0.3.0");
306+
307+
CurrentVersion currentVersion = new CurrentVersion();
308+
Version testCurrentVersion = currentVersion.getCurrentVersion(testDir, "0.3.0", mockedLogger);
309+
310+
assertThat(testCurrentVersion, is(notNullValue()));
311+
assertThat(testCurrentVersion, is(actualCurrentVersion));
312+
}
313+
314+
@Test
315+
public void testPythonProjectVersionMismatchWithTag() throws IOException, InterruptedException {
316+
317+
File pyDir = rootFolder.newFolder("SamplePythonProject");
318+
File setupCfg = rootFolder.newFile(pyDir.getName() + File.separator + "setup.cfg");
319+
320+
String setupCfgContent =
321+
"[metadata]\n" + "name = sample\n" + "version = 0.1.0\n" + "author = Sample Author";
322+
323+
FileWriter setupCfgWriter = new FileWriter(setupCfg);
324+
setupCfgWriter.write(setupCfgContent);
325+
setupCfgWriter.close();
326+
327+
Version actualCurrentVersion = Version.valueOf("0.4.0");
328+
CurrentVersion currentVersion = new CurrentVersion();
329+
Version testCurrentVersion = currentVersion.getCurrentVersion(pyDir, "0.4.0", mockedLogger);
330+
331+
assertThat(testCurrentVersion, is(notNullValue()));
332+
assertThat(testCurrentVersion, is(actualCurrentVersion));
333+
}
300334
}

0 commit comments

Comments
 (0)