Skip to content
This repository was archived by the owner on Aug 12, 2020. It is now read-only.

Commit 768e6b3

Browse files
committed
Fix Bitbucket server version comparison
Fixes #2
1 parent d21fb67 commit 768e6b3

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ dependencies {
2525

2626
compileOnly 'org.projectlombok:lombok:1.18.8'
2727
annotationProcessor 'org.projectlombok:lombok:1.18.8'
28+
29+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
30+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
2831
}
2932

3033
assemble.dependsOn(

src/main/java/com/github/goober/sonarqube/plugin/decorator/bitbucket/model/ServerProperties.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,33 @@
33
import lombok.AllArgsConstructor;
44
import lombok.Value;
55

6-
import static java.lang.String.format;
7-
86
@Value
97
@AllArgsConstructor
108
public class ServerProperties {
11-
private static final int CODE_INSIGHT_MAJOR_VERSION = 5;
12-
private static final int CODE_INSIGHT_MINOR_VERSION = 15;
13-
14-
public static String CODE_INSIGHT_VERSION = format("%d.%d",
15-
CODE_INSIGHT_MAJOR_VERSION,
16-
CODE_INSIGHT_MINOR_VERSION);
9+
public static final String CODE_INSIGHT_VERSION = "5.15";
1710

1811
String version;
1912

2013
public boolean hasCodeInsightsApi() {
21-
String[] semver = semver(version);
22-
return Integer.parseInt(semver[0]) >= CODE_INSIGHT_MAJOR_VERSION &&
23-
Integer.parseInt(semver[1]) >= CODE_INSIGHT_MINOR_VERSION;
14+
return compareTo(CODE_INSIGHT_VERSION) >= 0;
15+
}
16+
17+
private int compareTo(String other) {
18+
String[] current = semver(version);
19+
String[] minimum = semver(other);
20+
21+
int length = Math.max(current.length, minimum.length);
22+
for(int i = 0; i < length; i++) {
23+
int thisPart = i < current.length ?
24+
Integer.parseInt(current[i]) : 0;
25+
int thatPart = i < minimum.length ?
26+
Integer.parseInt(minimum[i]) : 0;
27+
if(thisPart < thatPart)
28+
return -1;
29+
if(thisPart > thatPart)
30+
return 1;
31+
}
32+
return 0;
2433
}
2534

2635
private String[] semver(String v) {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.goober.sonarqube.plugin.decorator.bitbucket.model;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
public class ServerPropertiesTest {
9+
10+
@Test
11+
public void server_with_version_without_code_insights_returns_false() {
12+
ServerProperties underTest = new ServerProperties("2.1");
13+
assertFalse(underTest.hasCodeInsightsApi());
14+
}
15+
16+
@Test
17+
public void server_version_greater_than_minimum_for_code_insights_returns_true() {
18+
ServerProperties underTest = new ServerProperties("6.7.1");
19+
assertTrue(underTest.hasCodeInsightsApi());
20+
}
21+
22+
@Test
23+
public void server_version_equals_to_minimum_for_code_insights_return_true() {
24+
ServerProperties underTest = new ServerProperties(ServerProperties.CODE_INSIGHT_VERSION);
25+
assertTrue(underTest.hasCodeInsightsApi());
26+
}
27+
}

0 commit comments

Comments
 (0)