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

Commit 271cb5a

Browse files
committed
Validate Bitbucket Server version before trying to decorate PR
1 parent fe9e0c4 commit 271cb5a

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonInclude.Include;
44
import com.fasterxml.jackson.databind.DeserializationFeature;
55
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.ServerProperties;
67
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.CreateAnnotationsRequest;
78
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.CreateReportRequest;
89
import okhttp3.MediaType;
@@ -38,6 +39,19 @@ boolean isConfigured() {
3839
configuration.hasKey(BitbucketProperties.TOKEN.getKey());
3940
}
4041

42+
ServerProperties getServerProperties() throws IOException {
43+
Request req = new Request.Builder()
44+
.get()
45+
.url(format("%s/rest/api/1.0/application-properties", baseUrl()))
46+
.build();
47+
try(Response response = getClient().newCall(req).execute()) {
48+
validate(response);
49+
50+
return getObjectMapper().reader().forType(ServerProperties.class)
51+
.readValue(response.body().string());
52+
}
53+
}
54+
4155
void createReport(String project, String repository, String commit, CreateReportRequest request) throws IOException {
4256
String body = getObjectMapper().writeValueAsString(request);
4357
Request req = new Request.Builder()

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import com.github.goober.sonarqube.plugin.decorator.PullRequestDecorator;
44
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.CreateAnnotationsRequest;
55
import com.github.goober.sonarqube.plugin.decorator.sonarqube.SonarQubeClient;
6+
import com.github.goober.sonarqube.plugin.decorator.bitbucket.model.ServerProperties;
67
import org.sonar.api.utils.log.Logger;
78
import org.sonar.api.utils.log.Loggers;
89

910
import java.io.IOException;
1011

12+
import static java.lang.String.format;
1113
public class BitbucketPullRequestDecorator implements PullRequestDecorator {
1214

1315
private static final Logger LOGGER = Loggers.get(BitbucketPullRequestDecorator.class);
@@ -22,7 +24,7 @@ public BitbucketPullRequestDecorator(SonarQubeClient sonarqubeClient, BitbucketC
2224

2325
@Override
2426
public boolean isActivated() {
25-
return bitbucketClient.isConfigured();
27+
return bitbucketClient.isConfigured() && hasApiSupport();
2628
}
2729

2830
@Override
@@ -56,6 +58,21 @@ public void decorate(ProjectAnalysis analysis) {
5658

5759
} catch (IOException e) {
5860
LOGGER.error("Could not decorate pull request {}, in project {}", report.getPullRequestId(), analysis.getProject().getKey(), e);
61+
private boolean hasApiSupport() {
62+
try {
63+
ServerProperties server = bitbucketClient.getServerProperties();
64+
LOGGER.debug(format("Your Bitbucket Server installation is version %s", server.getVersion()));
65+
if (server.hasCodeInsightsApi()) {
66+
return true;
67+
} else {
68+
LOGGER.info("Bitbucket Server version is to old. %s is the minimum version that supports code insights",
69+
ServerProperties.CODE_INSIGHT_VERSION);
70+
}
71+
} catch (IOException e) {
72+
LOGGER.error("Could not determine Bitbucket Server version", e);
73+
return false;
5974
}
75+
return false;
6076
}
77+
6178
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.github.goober.sonarqube.plugin.decorator.bitbucket.model;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Value;
5+
6+
import static java.lang.String.format;
7+
8+
@Value
9+
@AllArgsConstructor
10+
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);
17+
18+
String version;
19+
20+
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;
24+
}
25+
26+
private String[] semver(String v) {
27+
return v.split("\\.");
28+
}
29+
}

0 commit comments

Comments
 (0)