Skip to content

Commit

Permalink
Merge pull request #208 from cnescatlab/dev
Browse files Browse the repository at this point in the history
Version 3.3.1 implementation
  • Loading branch information
Sancretor authored Apr 7, 2021
2 parents 8e20885 + 06e2ddf commit 262c4ca
Show file tree
Hide file tree
Showing 24 changed files with 726 additions and 2,156 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Generate editable reports for SonarQube projects.
-a,--author <arg> Name of the report writer.
-b,--branch <arg> Branch of the targeted project. Requires Developer Edition or sonarqube-community-branch-plugin. Default: usage of main branch.
-c,--disable-conf Disable export of quality configuration used during analysis.
-d,--date <arg> Date for the report. Default: current date.
-d,--date <arg> Date for the report. Format: yyyy-MM-dd. Default: current date.
-e,--disable-spreadsheet Disable spreadsheet generation.
-f,--disable-csv Disable csv generation.
-h,--help Display this message.
Expand Down
2,111 changes: 334 additions & 1,777 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "15.6.2",
"react-addons-shallow-compare": "^15.6.3",
"react-addons-test-utils": "15.6.2",
"react-dev-utils": "^10.2.0",
"react-dev-utils": "^11.0.4",
"react-dom": "15.6.2",
"react-router": "3.0.2",
"react-transform-hmr": "1.0.4",
Expand Down
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.cnes.sonar</groupId>
<artifactId>cnesreport</artifactId>
<version>3.3.0</version>
<version>3.3.1</version>
<packaging>sonar-plugin</packaging>

<name>SonarQube CNES Report</name>
Expand Down Expand Up @@ -67,6 +67,10 @@
<id>louisjdmartin</id>
<name>Louis Martin</name>
</developer>
<developer>
<id>ErwanGauduchon</id>
<name>Erwan Gauduchon</name>
</developer>
</developers>

<dependencies>
Expand Down Expand Up @@ -171,6 +175,15 @@

<build>
<testSourceDirectory>src/test/ut/java</testSourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources-filtered</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>

<plugin>
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/fr/cnes/sonar/plugin/tools/FileTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

public class FileTools {

/** Logger of this class */
private static final Logger LOGGER = Logger.getLogger(FileTools.class.getName());

private FileTools(){
throw new IllegalStateException("Utility class");
}
Expand All @@ -46,15 +49,13 @@ public static void deleteFolder(File folder){
try {
Files.deleteIfExists(toDelete.toPath());
} catch (IOException e) {
Logger LOGGER = Logger.getLogger(FileTools.class.getName());
LOGGER.warning(e.getMessage());
}
}
}
try {
Files.deleteIfExists(folder.toPath());
} catch (IOException e) {
Logger LOGGER = Logger.getLogger(FileTools.class.getName());
LOGGER.warning(e.getMessage());
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/fr/cnes/sonar/plugin/tools/ZipFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
import java.util.zip.ZipOutputStream;

public class ZipFolder {

/** Logger of this class */
private static final Logger LOGGER = Logger.getLogger(ZipFolder.class.getName());

private ZipFolder(){
throw new IllegalStateException("Utility class");
}
Expand All @@ -52,7 +56,6 @@ public static void pack(String sourceDirPath, String zipFilePath) throws IOExcep
Files.deleteIfExists(path);
zs.closeEntry();
} catch (IOException e) {
Logger LOGGER = Logger.getLogger(ZipFolder.class.getName());
LOGGER.warning(e.getMessage());
}
});
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/fr/cnes/sonar/plugin/ws/ExportTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.ParseException;

public class ExportTask implements RequestHandler {

Expand All @@ -60,7 +61,7 @@ public class ExportTask implements RequestHandler {
*/
@Override
public void handle(Request request, Response response) throws BadExportationDataTypeException, IOException,
UnknownQualityGateException, OpenXML4JException, XmlException, SonarQubeException{
UnknownQualityGateException, OpenXML4JException, XmlException, SonarQubeException, ParseException {

// Get project key
String projectKey = request.getParam(PluginStringManager.getProperty("api.report.args.key")).getValue();
Expand All @@ -81,20 +82,21 @@ public void handle(Request request, Response response) throws BadExportationData

// Build SonarQube local URL
String port = config.get("sonar.web.port").orElse(PluginStringManager.getProperty("plugin.defaultPort"));
String host = String.format(PluginStringManager.getProperty("plugin.defaultHost"), port);
String context = config.get("sonar.web.context").orElse(PluginStringManager.getProperty("plugin.defaultContext"));
String sonarUrl = String.format(PluginStringManager.getProperty("plugin.defaultHost"), port, context);

ReportCommandLine.execute(new String[]{
"report",
"-o", outputDirectory.getAbsolutePath(),
"-s", host,
"-s", sonarUrl,
"-p", projectKey,
"-b", pBranch.isPresent()?pBranch.getValue(): StringManager.NO_BRANCH,
"-a", request.getParam(PluginStringManager.getProperty("api.report.args.author")).getValue(),
"-t", request.getParam(PluginStringManager.getProperty("api.report.args.token")).getValue()
});

stream.setMediaType("application/zip");
String filename = ReportFactory.formatFilename("zip.report.output", "", projectKey);
String filename = ReportFactory.formatFilename("zip.report.output", "", "", projectKey);
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + '"');


Expand Down
15 changes: 3 additions & 12 deletions src/main/java/fr/cnes/sonar/report/ReportCommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;
Expand Down Expand Up @@ -78,15 +79,15 @@ public static void main(final String[] args) {

} catch (BadExportationDataTypeException | BadSonarQubeRequestException | IOException |
UnknownQualityGateException | OpenXML4JException | XmlException | SonarQubeException |
IllegalStateException | IllegalArgumentException e) {
IllegalStateException | IllegalArgumentException | ParseException e) {
// it logs all the stack trace
LOGGER.log(Level.SEVERE, e.getMessage(), e);
System.exit(-1);
}
}

public static void execute(final String[] args) throws BadExportationDataTypeException , BadSonarQubeRequestException , IOException,
UnknownQualityGateException, OpenXML4JException, XmlException, SonarQubeException{
UnknownQualityGateException, OpenXML4JException, XmlException, SonarQubeException, ParseException {
// Log message.
String message;

Expand All @@ -100,16 +101,6 @@ public static void execute(final String[] args) throws BadExportationDataTypeExc
// assumes the language is set with language_country
StringManager.changeLocale(conf.getLanguage());

// Display version information and exit.
if(conf.isVersion()) {
final String name = ReportCommandLine.class.getPackage().getImplementationTitle();
final String version = ReportCommandLine.class.getPackage().getImplementationVersion();
final String vendor = ReportCommandLine.class.getPackage().getImplementationVendor();
message = String.format("%s %s by %s", name, version, vendor);
LOGGER.info(message);
System.exit(0);
}

// Print information about SonarQube.
message = String.format("SonarQube URL: %s", conf.getServer());
LOGGER.info(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ public class MarkdownExporter implements IExporter {
/**
* Placeholder for issues table
*/
private static String ISSUES_DETAILS_PLACEHOLDER = "$ISSUES_DETAILS";
private static final String ISSUES_DETAILS_PLACEHOLDER = "$ISSUES_DETAILS";
/**
* Placeholder for volume table
*/
private static final String VOLUMES_TABLE_PLACEHOLDER = "$VOLUME";
/**
* Markdown special chars
*/
private static String CELL_SEPARATOR = "|";
private static String ESCAPED_CELL_SEPARATOR = "&#124";
private static String HEADER_SEPARATOR = "---";
private static String LINE_BREAK = "\n";
private static String MD_LINE_BREAK = " <br /> ";
private static final String CELL_SEPARATOR = "|";
private static final String ESCAPED_CELL_SEPARATOR = "&#124";
private static final String HEADER_SEPARATOR = "---";
private static final String LINE_BREAK = "\n";
private static final String MD_LINE_BREAK = " <br /> ";

@Override
public File export(Object data, String path, String filename) throws IOException, BadExportationDataTypeException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ class RuleComparator implements Comparator<String>{
public int compare(String o1, String o2) {
int compare = 0;

if(o1.isEmpty() || o2.isEmpty())compare = 1;
if(o1.isEmpty() || o2.isEmpty()){compare = 1;}

//If rule is removed in quality gate, the issue is send to the end of list
if(report.getRule(o1) == null){compare = 1;}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public static List<XWPFChartSpace> getChartSpaces(final XWPFDocument document)
final ChartSpaceDocument c = ChartSpaceDocument.Factory.parse(inputStream);
result.add(new XWPFChartSpace(c, p.getPackagePart()));
} catch(final ClassCastException e){
LOGGER.log(Level.WARNING, "Error while getting charts, can't convert XMLObject into ChartSpaceDocument", e);
LOGGER.log(Level.WARNING, "Error while getting charts, cannot convert XMLObject into ChartSpaceDocument", e);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/fr/cnes/sonar/report/exporters/xlsx/XlsXTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public static void addListOfMap(XSSFSheet sheet, List<Map<String,String>> list,
content = new String[headers.size()];
int index;
// adding each field of the map in a different column of the row
for (Map.Entry issue : map.entrySet()) {
for (Map.Entry<String, String> issue : map.entrySet()) {
// index of the column to fill comparing key and headers
index = headers.indexOf(issue.getKey().toString());
index = headers.indexOf(issue.getKey());
// get the cell having the same key as the header
content[index] = issue.getValue().toString();
content[index] = String.valueOf(issue.getValue());
}

// create a row from resources as string's list
Expand Down Expand Up @@ -159,9 +159,9 @@ public static List<String> extractHeader(List<Map<String,String>> list) {
}

// for each unique header
for(Map.Entry header : gatherer.entrySet()) {
for(Map.Entry<String, String> header : gatherer.entrySet()) {
// we had the key to the list
result.add(header.getKey().toString());
result.add(header.getKey());
}

return result;
Expand Down
36 changes: 29 additions & 7 deletions src/main/java/fr/cnes/sonar/report/factory/ReportFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
Expand Down Expand Up @@ -80,7 +81,7 @@ private ReportFactory() {}
* @throws OpenXML4JException Caused by Apache library.
*/
public static void report(final ReportConfiguration configuration, final Report model)
throws IOException, XmlException, BadExportationDataTypeException, OpenXML4JException {
throws IOException, XmlException, BadExportationDataTypeException, OpenXML4JException, ParseException {

// Files exporters : export the resources in the correct file type
final DocXExporter docXExporter = new DocXExporter();
Expand All @@ -99,28 +100,28 @@ public static void report(final ReportConfiguration configuration, final Report
// Export issues and metrics in report if requested.
if(configuration.isEnableReport()) {
// prepare docx report's filename
final String docXFilename = formatFilename(REPORT_FILENAME, configuration.getOutput(), model.getProjectName());
final String docXFilename = formatFilename(REPORT_FILENAME, configuration.getOutput(), configuration.getDate(), model.getProjectName());
// export the full docx report
docXExporter.export(model, docXFilename, configuration.getTemplateReport());
}

// Export issues in spreadsheet if requested.
if(configuration.isEnableSpreadsheet()) {
// construct the xlsx filename by replacing date and name
final String xlsXFilename = formatFilename(ISSUES_FILENAME, configuration.getOutput(), model.getProjectName());
final String xlsXFilename = formatFilename(ISSUES_FILENAME, configuration.getOutput(), configuration.getDate(), model.getProjectName());
// export the xlsx issues' list
issuesExporter.export(model, xlsXFilename, configuration.getTemplateSpreadsheet());
}

// Export in markdown if requested
if (configuration.isEnableMarkdown()) {
final String MDFilename = formatFilename(MD_FILENAME, configuration.getOutput(), model.getProjectName());
final String MDFilename = formatFilename(MD_FILENAME, configuration.getOutput(), configuration.getDate(), model.getProjectName());
markdownExporter.export(model, MDFilename, configuration.getTemplateMarkdown());
}

// Export issues in report if requested
if(configuration.isEnableCSV()) {
final String CSVFilename = formatFilename(CSV_FILENAME, configuration.getOutput(), model.getProjectName());
final String CSVFilename = formatFilename(CSV_FILENAME, configuration.getOutput(), configuration.getDate(), model.getProjectName());
csvExporter.export(model, CSVFilename, model.getProjectName());
}
}
Expand Down Expand Up @@ -193,14 +194,35 @@ private static void exportAllQualityProfiles(final Report report, final IExporte
* Format a given filename pattern.
* Add the date and the project's name
* @param propertyName Name of pattern's property
* @param baseDir Path to the folder where to save the file
* @param projectDate Date of the current project
* @param projectName Name of the current project
* @return a formatted filename
*/
public static String formatFilename(final String propertyName, final String baseDir, final String projectName) {
public static String formatFilename(final String propertyName, final String baseDir, final String projectDate, final String projectName)
throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(StringManager.DATE_PATTERN);
String dateStr;
// put the current date if no date is given
if (projectDate.isEmpty()) {
dateStr = simpleDateFormat.format(new Date());
} else {
// accept only the defined format
if (!projectDate.matches(StringManager.DATE_REGEX)) {
throw new IllegalArgumentException("Please provide a date that respects " + StringManager.DATE_PATTERN + " format.");
}
// reject inconsistent dates
simpleDateFormat.setLenient(false);
try {
dateStr = simpleDateFormat.format(simpleDateFormat.parse(projectDate));
} catch (ParseException e) {
throw new ParseException("Invalid date value: day or month exceeds its bounds.", e.getErrorOffset());
}
}
// construct the filename by replacing date and name
return StringManager.getProperty(propertyName)
.replaceFirst(BASEDIR, Matcher.quoteReplacement(baseDir))
.replace(DATE, new SimpleDateFormat(StringManager.DATE_PATTERN).format(new Date()))
.replace(DATE, dateStr)
.replace(NAME, escapeProjectName(projectName));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,6 @@ public JsonObject request(final String request)
try {
json = getGson().fromJson(raw, JsonElement.class);
} catch (Exception e) {
// log exception's message
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new BadSonarQubeRequestException("Server answered: " + raw +
StringManager.SPACE + e.getMessage());
}
Expand Down Expand Up @@ -340,7 +338,7 @@ protected String stringRequest(final String request) throws SonarQubeException,
// replace spaces
String preparedRequest = request.replace(" ", "%20");
// replace + characters
preparedRequest = preparedRequest.replaceAll("\\+", "%2B");
preparedRequest = preparedRequest.replace("+", "%2B");

// launch the request on SonarQube server and retrieve resources into a string
return RequestManager.getInstance().get(preparedRequest, this.token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ private List<Issue> getIssuesByStatus(String confirmed)

// in case of overflow we log the problem
if(overflow) {
LOGGER.warning(StringManager.string(StringManager.ISSUES_OVERFLOW_MSG));
String message = StringManager.string(StringManager.ISSUES_OVERFLOW_MSG);
LOGGER.warning(message);
}

// return the issues
Expand Down Expand Up @@ -302,7 +303,8 @@ public List<Map<String,String>> getRawIssues() throws BadSonarQubeRequestExcepti

// in case of overflow we log the problem
if(overflow) {
LOGGER.warning(StringManager.string(StringManager.ISSUES_OVERFLOW_MSG));
String message = StringManager.string(StringManager.ISSUES_OVERFLOW_MSG);
LOGGER.warning(message);
}

// return the issues
Expand Down
Loading

0 comments on commit 262c4ca

Please sign in to comment.