Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Add PMD for static analysis #102

Merged
merged 12 commits into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions config/pmd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0"?>

<ruleset name="Basic"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
The Basic ruleset contains a collection of good practices which should be followed.
</description>

<rule ref="category/java/errorprone.xml">
<exclude name="AvoidBranchingStatementAsLastInLoop" />
<exclude name="AvoidMultipleUnaryOperators"/>
<exclude name="AvoidUsingOctalValues"/>
<exclude name="BrokenNullCheck"/>
<exclude name="CheckSkipResult"/>
<exclude name="ClassCastExceptionWithToArray"/>
<exclude name="DontUseFloatTypeForLoopIndices"/>
<exclude name="JumbledIncrementer"/>
<exclude name="MisplacedNullCheck"/>
<exclude name="OverrideBothEqualsAndHashcode"/>
<exclude name="ReturnFromFinallyBlock"/>
<exclude name="UnconditionalIfStatement"/>
<exclude name="DataflowAnomalyAnalysis"/>
</rule>

<rule ref="category/java/multithreading.xml">
<exclude name="AvoidThreadGroup" />
<exclude name="DontCallThreadRun"/>
<exclude name="DoubleCheckedLocking"/>
<exclude name="BrokenNullCheck"/>
</rule>

<rule ref="category/java/bestpractices.xml">
<exclude name="AvoidUsingHardCodedIP" />
<exclude name="CheckResultSet"/>
<exclude name="GuardLogStatement"/>
</rule>

<rule ref="category/java/codestyle.xml">
<exclude name="ExtendsObject" />
<exclude name="ForLoopShouldBeWhileLoop"/>
<exclude name="AtLeastOneConstructor"/>
<exclude name="UnnecessaryAnnotationValueElement"/>
<exclude name="OnlyOneReturn"/>
</rule>

<rule ref="category/java/codestyle.xml/ClassNamingConventions">
<properties>
<property name="classPattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="abstractClassPattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="interfacePattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="enumPattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="annotationPattern" value="[A-Z][a-zA-Z0-9]*" />
<property name="utilityClassPattern" value="[A-Z][a-zA-Z0-9]+" />
</properties>
</rule>

<rule ref="category/java/performance.xml">
<exclude name="BigIntegerInstantiation" />
<exclude name="BooleanInstantiation"/>
<exclude name="AvoidInstantiatingObjectsInLoops" />
</rule>

<rule ref="category/java/design.xml">
<exclude name="UseUtilityClass"/>
<exclude name="CollapsibleIfStatements" />
<exclude name="SimplifiedTernary"/>
<exclude name="LawOfDemeter"/>
</rule>

</ruleset>
martinda marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 20 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<rulesets>
<ruleset>/config/pmd.xml</ruleset>
</rulesets>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>check</goal>
<goal>cpd-check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
Expand Down Expand Up @@ -271,5 +290,4 @@
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@SpringBootApplication
public class CustomJenkinsDistributionServiceApplication {

public static void main(String[] args) {
public static void main(final String[] args) {
SpringApplication.run(CustomJenkinsDistributionServiceApplication.class, args);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/error")
public class IndexController implements ErrorController {

private static final String PATH = "/error";

@RequestMapping(value = PATH)
public String error() {
return "This is the backend of the custom distribution service application. If you see this message the service is up and running";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.org.jenkins.custom.jenkins.distribution.service.services.PackagerDownloadService;
import com.org.jenkins.custom.jenkins.distribution.service.util.Util;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.io.ByteArrayInputStream;
Expand All @@ -22,7 +23,6 @@
@RestController
@CrossOrigin
@RequestMapping("/package")

public class PackagerController {

private static Util util = new Util();
Expand All @@ -42,15 +42,18 @@ public class PackagerController {
* @return a ResponseEntity instance with a body containing the package configuration as a YAML string.
*/
@PostMapping(path = "/getPackageConfiguration")
public ResponseEntity<?> getPackageConfig(@RequestBody String postPayload) {
public ResponseEntity<?> getPackageConfig(@RequestBody final String postPayload) {
LOGGER.info("Request Received for packaging configuration with params" + postPayload);
String yamlResponse = "";
HttpStatus httpStatus;
try {
String yamlResponse = generatePackageConfig(new JSONObject(postPayload));
return new ResponseEntity<>(yamlResponse, HttpStatus.OK);
} catch (Exception e) {
yamlResponse = generatePackageConfig(new JSONObject(postPayload));
httpStatus = HttpStatus.OK;
} catch (IOException e) {
LOGGER.severe(e.toString());
return new ResponseEntity(HttpStatus.NOT_FOUND);
httpStatus = HttpStatus.NOT_FOUND;
}
return new ResponseEntity<>(yamlResponse, httpStatus);
}

/**
Expand All @@ -61,11 +64,11 @@ public ResponseEntity<?> getPackageConfig(@RequestBody String postPayload) {
* @return a ResponseEntity instance with a body containing the generated jenkins.war package.
*/
@PostMapping (path = "/downloadWarPackage")
public ResponseEntity<?> downloadWAR(@RequestBody String postPayload) {
public ResponseEntity<?> downloadWAR(@RequestBody final String postPayload) {
LOGGER.info("Request Received for downloading war file with configuration" + postPayload);
try {
return new PackagerDownloadService().downloadWAR(getWarVersion(), postPayload);
} catch (Exception e) {
} catch (IOException | InterruptedException e) {
LOGGER.severe(e.toString());
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
Expand All @@ -78,24 +81,21 @@ public ResponseEntity<?> downloadWAR(@RequestBody String postPayload) {
* @return The content of the browser's text editor in an HTTP response (download from the server).
*/
@PostMapping (path = "/downloadPackageConfiguration")
public ResponseEntity<?> downloadPackageConfig(@RequestBody String postPayload) {
try {
public ResponseEntity<?> downloadPackageConfig(@RequestBody final String postPayload) {
LOGGER.info(postPayload);
InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(postPayload.getBytes(
StandardCharsets.UTF_8)));
String headerValue = "attachment; filename=packager-config.yml";
final InputStreamResource resource = new InputStreamResource(new ByteArrayInputStream(postPayload.getBytes(StandardCharsets.UTF_8)));
final String headerValue = "attachment; filename=packager-config.yml";
LOGGER.info("Returning packager-config.yml");
return Util.returnResource(Util.returnHeaders(headerValue), postPayload.getBytes(StandardCharsets.UTF_8).length, resource);
} catch (Exception e) {
LOGGER.severe(e.toString());
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
}

private String getWarVersion() throws Exception {
Yaml yaml = new Yaml();
Map<String , Map<String,String>> yamlMaps = (Map<String, Map<String,String>>) yaml.load(util.readStringFromFile("packager-config.yml"));
JSONObject json = new JSONObject(new ObjectMapper().writeValueAsString(yamlMaps.get("war")));
private String getWarVersion() throws IOException {
final Yaml yaml = new Yaml();
final JSONObject json;

final Map<String, Map<String, String>> yamlMaps = (Map<String, Map<String, String>>) yaml.load(util.readStringFromFile("packager-config.yml"));
json = new JSONObject(new ObjectMapper().writeValueAsString(yamlMaps.get("war")));

return json.getJSONObject("source").get("version").toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.org.jenkins.custom.jenkins.distribution.service;

import com.org.jenkins.custom.jenkins.distribution.service.services.UpdateCenterService;
import java.util.Map;
import java.util.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -13,14 +14,15 @@
@RestController
@CrossOrigin("*")
@RequestMapping("api/plugin")
@SuppressWarnings("PMD.AvoidCatchingGenericException")
public class PluginController {

private final static Logger LOGGER = Logger.getLogger(PluginController.class.getName());
private final UpdateCenterService updateCenterService;
private transient final static Logger LOGGER = Logger.getLogger(PluginController.class.getName());
private transient final UpdateCenterService updateService;

@Autowired
public PluginController(UpdateCenterService updateCenterService) {
this.updateCenterService = updateCenterService;
public PluginController(final UpdateCenterService updaterService) {
this.updateService = updaterService;
}

/*
Expand All @@ -30,11 +32,15 @@ public PluginController(UpdateCenterService updateCenterService) {
@GetMapping(path = "/getPluginList")
public ResponseEntity<?> getPlugins() {
LOGGER.info("Request Received");
Map updateMap = null;
HttpStatus status;
try {
return new ResponseEntity<>(updateCenterService.downloadUpdateCenterJSON().toMap(), HttpStatus.OK);
updateMap = updateService.downloadUpdateCenterJSON().toMap();
status = HttpStatus.OK;
} catch (Exception e) {
LOGGER.severe(e.toString());
return new ResponseEntity(HttpStatus.NOT_FOUND);
status = HttpStatus.NOT_FOUND;
}
return new ResponseEntity(updateMap, status);
}
}
Loading