Skip to content

Commit

Permalink
Merge pull request #227 from alessiofachechi/master
Browse files Browse the repository at this point in the history
Fix issues retrieval for SonarQube Community Edition
  • Loading branch information
gabrie-allaigre committed Mar 28, 2019
2 parents 2434a81 + baf0caa commit ac529d5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@
<artifactId>java-gitlab-api</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
Expand Down Expand Up @@ -205,4 +210,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
36 changes: 34 additions & 2 deletions src/main/java/com/talanlabs/sonar/plugins/gitlab/SonarFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.io.Files;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.talanlabs.sonar.plugins.gitlab.models.Issue;
import com.talanlabs.sonar.plugins.gitlab.models.QualityGate;
import com.talanlabs.sonar.plugins.gitlab.models.Rule;
Expand All @@ -41,6 +45,7 @@
import org.sonarqube.ws.client.ce.TaskRequest;
import org.sonarqube.ws.client.components.ShowRequest;
import org.sonarqube.ws.client.issues.SearchRequest;
import org.sonarqube.ws.client.plugins.InstalledRequest;
import org.sonarqube.ws.client.qualitygates.ProjectStatusRequest;

import java.io.File;
Expand Down Expand Up @@ -188,6 +193,33 @@ private void logCondition(Qualitygates.ProjectStatusResponse.Condition condition
}
}

public boolean isPluginInstalled(String pluginKey) {
String response = wsClient.plugins().installed(new InstalledRequest());
JsonElement json = new JsonParser().parse(response);
if (json.isJsonObject()) {
JsonObject root = json.getAsJsonObject();
json = root.get("plugins");
if (json.isJsonArray()) {
JsonArray plugins = json.getAsJsonArray();
Set<String> keys = new HashSet<>();
plugins.forEach(j -> {
if (j.isJsonObject()) {
j = j.getAsJsonObject().get("key");
if (j.isJsonPrimitive()) {
keys.add(j.getAsString());
}
}
});

if (keys.contains(pluginKey)) {
return true;
}
}
}

return false;
}

@VisibleForTesting
String getMetricName(String metricKey) {
try {
Expand Down Expand Up @@ -238,7 +270,7 @@ public List<Issue> getNewIssues() {

private Issues.SearchWsResponse searchIssues(String componentKey, String branch, int page) {
SearchRequest searchRequest = new SearchRequest().setComponentKeys(Collections.singletonList(componentKey)).setP(String.valueOf(page)).setResolved("false");
if (isNotBlankAndNotEmpty(branch)) {
if (isNotBlankAndNotEmpty(branch) && isPluginInstalled("branch")) {
searchRequest.setBranch(branch);
}
return wsClient.issues().search(searchRequest);
Expand Down Expand Up @@ -292,7 +324,7 @@ private List<Issue> toIssues(Issues.SearchWsResponse issuesSearchWsResponse, Str

private File toFile(Issues.Component component, String branch) {
ShowRequest showRequest = new ShowRequest().setComponent(component.getKey());
if (isNotBlankAndNotEmpty(branch)) {
if (isNotBlankAndNotEmpty(branch) && isPluginInstalled("branch")) {
showRequest.setBranch(branch);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,4 +537,18 @@ public void testMetricNameFailed() {
Assertions.assertThat(sonarFacade.getMetricName("toto")).isEqualTo("toto");
Assertions.assertThat(sonarFacade.getMetricName("security_rating")).isEqualTo("Security Rating");
}

@Test
public void testIsPluginInstalled() {
String pluginsResponse = "{\"plugins\":[{\"key\":\"my-plugin-1\"},{\"key\":\"my-plugin-2\"}]}";
for (int j = 0; j < 4; j++) {
sonar.enqueue(new MockResponse().setResponseCode(200).addHeader("Content-Type", "application/x-protobuf").setBody(pluginsResponse));
}

Assertions.assertThat(sonarFacade.isPluginInstalled("my-plugin-1")).isEqualTo(true);
Assertions.assertThat(sonarFacade.isPluginInstalled("my-plugin-2")).isEqualTo(true);
Assertions.assertThat(sonarFacade.isPluginInstalled("my-plugin-3")).isEqualTo(false);
Assertions.assertThat(sonarFacade.isPluginInstalled(null)).isEqualTo(false);
}

}

0 comments on commit ac529d5

Please sign in to comment.