aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorJacek <52388493+jacek-poreda-sonarsource@users.noreply.github.com>2019-07-31 16:48:16 +0200
committerSonarTech <sonartech@sonarsource.com>2019-08-07 20:21:22 +0200
commit24b51a56601c053a0faa14a789415007eaafd81e (patch)
treedd8305720c5f5363de15beb547fc6770e84bedf7 /sonar-core
parent72ce45299de3dacbebe6b10cba25d6e973b9801a (diff)
downloadsonarqube-24b51a56601c053a0faa14a789415007eaafd81e.tar.gz
sonarqube-24b51a56601c053a0faa14a789415007eaafd81e.zip
SONAR-12351 list plugins with documentation (#1963)
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java32
-rw-r--r--sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java20
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/platform/jar_with_documentation.jarbin0 -> 49113 bytes
3 files changed, 46 insertions, 6 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java b/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java
index 5cbefd71c65..0abe8adc81f 100644
--- a/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java
+++ b/sonar-core/src/main/java/org/sonar/core/platform/PluginInfo.java
@@ -27,12 +27,16 @@ import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
+import java.util.Optional;
import java.util.Set;
+import java.util.jar.JarFile;
import java.util.regex.Pattern;
+import java.util.zip.ZipEntry;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.MessageException;
+import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.updatecenter.common.PluginManifest;
import org.sonar.updatecenter.common.Version;
@@ -40,6 +44,7 @@ import org.sonar.updatecenter.common.Version;
import static java.util.Objects.requireNonNull;
public class PluginInfo implements Comparable<PluginInfo> {
+ private static final Logger LOGGER = Loggers.get(PluginInfo.class);
private static final Joiner SLASH_JOINER = Joiner.on(" / ").skipNulls();
@@ -140,6 +145,9 @@ public class PluginInfo implements Comparable<PluginInfo> {
@CheckForNull
private boolean sonarLintSupported;
+ @CheckForNull
+ private String documentationPath;
+
private final Set<RequiredPlugin> requiredPlugins = new HashSet<>();
public PluginInfo(String key) {
@@ -234,6 +242,10 @@ public class PluginInfo implements Comparable<PluginInfo> {
return sonarLintSupported;
}
+ public String getDocumentationPath() {
+ return documentationPath;
+ }
+
@CheckForNull
public String getBasePlugin() {
return basePlugin;
@@ -263,6 +275,11 @@ public class PluginInfo implements Comparable<PluginInfo> {
return this;
}
+ public PluginInfo setDocumentationPath(@Nullable String documentationPath) {
+ this.documentationPath = documentationPath;
+ return this;
+ }
+
/**
* Required
*/
@@ -313,7 +330,7 @@ public class PluginInfo implements Comparable<PluginInfo> {
public PluginInfo setBasePlugin(@Nullable String s) {
if ("l10nen".equals(s)) {
- Loggers.get(PluginInfo.class).info("Plugin [{}] defines 'l10nen' as base plugin. " +
+ LOGGER.info("Plugin [{}] defines 'l10nen' as base plugin. " +
"This metadata can be removed from manifest of l10n plugins since version 5.2.", key);
basePlugin = null;
} else {
@@ -411,6 +428,7 @@ public class PluginInfo implements Comparable<PluginInfo> {
info.setName(manifest.getName());
info.setMainClass(manifest.getMainClass());
info.setVersion(Version.create(manifest.getVersion()));
+ info.setDocumentationPath(getDocumentationPath(jarFile));
// optional fields
info.setDescription(manifest.getDescription());
@@ -437,4 +455,16 @@ public class PluginInfo implements Comparable<PluginInfo> {
}
return info;
}
+
+ private static String getDocumentationPath(File file) {
+ try (JarFile jarFile = new JarFile(file)) {
+ return Optional.ofNullable(jarFile.getEntry("static/documentation.md"))
+ .map(ZipEntry::getName)
+ .orElse(null);
+ } catch (IOException e) {
+ LOGGER.warn("Could not retrieve documentation path from " + file, e);
+ }
+ return null;
+ }
+
}
diff --git a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java
index a3027ce0c4a..805dd988ad3 100644
--- a/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java
+++ b/sonar-core/src/test/java/org/sonar/core/platform/PluginInfoTest.java
@@ -210,7 +210,7 @@ public class PluginInfoTest {
manifest.setOrganization("SonarSource");
manifest.setOrganizationUrl("http://sonarsource.com");
manifest.setIssueTrackerUrl("http://jira.com");
- manifest.setRequirePlugins(new String[] {"java:2.0", "pmd:1.3"});
+ manifest.setRequirePlugins(new String[]{"java:2.0", "pmd:1.3"});
manifest.setSonarLintSupported(true);
File jarFile = temp.newFile();
@@ -237,7 +237,7 @@ public class PluginInfoTest {
manifest.setVersion("1.0");
manifest.setName("Java");
manifest.setMainClass("org.foo.FooPlugin");
- manifest.setRequirePlugins(new String[] {"license:" + version});
+ manifest.setRequirePlugins(new String[]{"license:" + version});
File jarFile = temp.newFile();
PluginInfo pluginInfo = PluginInfo.create(jarFile, manifest);
@@ -252,7 +252,7 @@ public class PluginInfoTest {
manifest.setVersion("1.0");
manifest.setName("Java");
manifest.setMainClass("org.foo.FooPlugin");
- manifest.setRequirePlugins(new String[] {"java:2.0", "license:" + version, "pmd:1.3"});
+ manifest.setRequirePlugins(new String[]{"java:2.0", "license:" + version, "pmd:1.3"});
File jarFile = temp.newFile();
PluginInfo pluginInfo = PluginInfo.create(jarFile, manifest);
@@ -261,7 +261,7 @@ public class PluginInfoTest {
@DataProvider
public static Object[][] licenseVersions() {
- return new Object[][] {
+ return new Object[][]{
{"0.3"},
{"7.2.0.1253"}
};
@@ -273,11 +273,21 @@ public class PluginInfoTest {
PluginInfo checkstyleInfo = PluginInfo.create(checkstyleJar);
assertThat(checkstyleInfo.getName()).isEqualTo("Checkstyle");
+ assertThat(checkstyleInfo.getDocumentationPath()).isNull();
assertThat(checkstyleInfo.getMinimalSqVersion()).isEqualTo(Version.create("2.8"));
}
@Test
- public void test_toString() throws Exception {
+ public void create_from_file_with_documentation() {
+ File jarWithDocs = FileUtils.toFile(getClass().getResource("/org/sonar/core/platform/jar_with_documentation.jar"));
+ PluginInfo checkstyleInfo = PluginInfo.create(jarWithDocs);
+
+ assertThat(checkstyleInfo.getDocumentationPath()).isNotBlank();
+ assertThat(checkstyleInfo.getDocumentationPath()).isEqualTo("static/documentation.md");
+ }
+
+ @Test
+ public void test_toString() {
PluginInfo pluginInfo = new PluginInfo("java").setVersion(Version.create("1.1"));
assertThat(pluginInfo.toString()).isEqualTo("[java / 1.1]");
diff --git a/sonar-core/src/test/resources/org/sonar/core/platform/jar_with_documentation.jar b/sonar-core/src/test/resources/org/sonar/core/platform/jar_with_documentation.jar
new file mode 100644
index 00000000000..bed32916585
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/platform/jar_with_documentation.jar
Binary files differ