Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add xml file support #62

Merged
merged 5 commits into from
Sep 11, 2022
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ Then your dashboard looks like this:

* JSON
* YAML/YML
* XML

For examples of report files, please have look into [etc](/etc) folder.

### Visualization

Expand Down
81 changes: 81 additions & 0 deletions etc/result.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<root>
<id>xml1</id>
<name>My fancy XML report without colors</name>
<items>
<item>
<id>Aktie</id>
<name>Aktie</name>
<items>
<item>
<id>Aktie_1</id>
<name>Aktie 1</name>
<result>
<incorrect>3441</incorrect>
<manually>348</manually>
<accurate>5199</accurate>
</result>
</item>
<item>
<id>Aktie_2</id>
<name>Aktie 2</name>
<result>
<incorrect>7488</incorrect>
<manually>455</manually>
<accurate>1456</accurate>
</result>
</item>
<item>
<id>Aktie_3</id>
<name>Aktie 3</name>
<result>
<incorrect>2973</incorrect>
<manually>572</manually>
<accurate>5954</accurate>
</result>
</item>
</items>
</item>
<item>
<id>Not_Found</id>
<name>Not_Found</name>
<result>
<incorrect>17701</incorrect>
<manually>1200</manually>
<accurate>14400</accurate>
</result>
</item>
<item>
<id>Renten</id>
<name>Renten</name>
<items>
<item>
<id>Rente1</id>
<name>Rente1</name>
<result>
<incorrect>7762</incorrect>
<manually>749</manually>
<accurate>4187</accurate>
</result>
</item>
<item>
<id>Rente2</id>
<name>Rente2</name>
<result>
<incorrect>4271</incorrect>
<manually>279</manually>
<accurate>5648</accurate>
</result>
</item>
</items>
</item>
<item>
<id>Derivat</id>
<name>Derivat</name>
<result>
<incorrect>1271</incorrect>
<manually>229</manually>
<accurate>4648</accurate>
</result>
</item>
</items>
</root>
4 changes: 4 additions & 0 deletions src/main/java/io/jenkins/plugins/reporter/model/Item.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.plugins.reporter.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import edu.hm.hafner.echarts.PieChartModel;
import edu.hm.hafner.echarts.PieData;
Expand Down Expand Up @@ -34,10 +35,12 @@ public class Item implements Serializable {
private String name;

@JsonProperty(value = "result", required = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
LinkedHashMap<String, Integer> result;

@Nullable
@JsonProperty(value = "items", required = false)
@JsonInclude(JsonInclude.Include.NON_NULL)
List<Item> items;

public String getId() {
Expand All @@ -56,6 +59,7 @@ public void setName(String name) {
this.name = name;
}

@JsonIgnore
public LinkedHashMap<String, Integer> getResult() {
if (result != null) {
return result;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/jenkins/plugins/reporter/model/Result.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jenkins.plugins.reporter.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;
Expand Down Expand Up @@ -31,9 +33,11 @@ public void setName(String name) {
private String name = String.valueOf(hashCode());

@JsonProperty(value = "items", required = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<Item> items;

@JsonProperty(value = "colors", required = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
private Map<String, String> colors;

public String getId() {
Expand Down Expand Up @@ -64,6 +68,7 @@ public void setColors(Map<String, String> colors) {
this.colors = colors;
}

@JsonIgnore
public String getColor(String id) {
String color = getColors().getOrDefault(id, DEFAULT_COLOR);

Expand All @@ -78,6 +83,7 @@ public String getColor(String id) {
return color;
}

@JsonIgnore
public boolean hasColors() {
return this.colors != null && this.colors.size() > 0;
}
Expand All @@ -88,6 +94,7 @@ public boolean hasColors() {
* the items to aggregate the childs for.
* @return the aggregated result.
*/
@JsonIgnore
public LinkedHashMap<String, Integer> aggregate(List<Item> items) {
return items
.stream()
Expand All @@ -96,6 +103,7 @@ public LinkedHashMap<String, Integer> aggregate(List<Item> items) {
.collect(Collectors.groupingBy(Map.Entry::getKey, LinkedHashMap::new, Collectors.summingInt(Map.Entry::getValue)));
}

@JsonIgnore
public List<String> getColorIds() {
if (aggregate().size() == 1) {
return findItems(getItems()).stream().map(Item::getId).collect(Collectors.toList());
Expand All @@ -104,6 +112,7 @@ public List<String> getColorIds() {
return new ArrayList<>(aggregate().keySet());
}

@JsonIgnore
public List<Item> findItems(List<Item> items)
{
List<Item> flatten = new ArrayList<>();
Expand All @@ -124,6 +133,7 @@ public List<Item> findItems(List<Item> items)
*
* @return the aggregated result.
*/
@JsonIgnore
public LinkedHashMap<String, Integer> aggregate() {
return aggregate(getItems());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.jenkins.plugins.reporter.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import edu.hm.hafner.echarts.JacksonFacade;
import io.jenkins.cli.shaded.org.apache.commons.io.FileUtils;
Expand All @@ -23,16 +24,19 @@ public class ResultParser {
public Optional<Result> parseResult(File file) throws IOException {

String extension = FilenameUtils.getExtension(file.getName()).toLowerCase(Locale.ROOT);

ObjectMapper jsonWriter = new ObjectMapper();
String json;

switch (extension) {
case "yaml":
case "yml": {
ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory());
Object obj = yamlReader.readValue(file, Object.class);
ObjectMapper jsonWriter = new ObjectMapper();
json = jsonWriter.writeValueAsString(obj);
Result result = new ObjectMapper(new YAMLFactory()).readerFor(Result.class).readValue(file);
json = jsonWriter.writeValueAsString(result);
break;
}
case "xml": {
Result result = new ObjectMapper(new XmlFactory()).readerFor(Result.class).readValue(file);
json = jsonWriter.writeValueAsString(result);
break;
}
case "json":
Expand All @@ -55,10 +59,6 @@ public Optional<Result> parseResult(File file) throws IOException {
JacksonFacade jackson = new JacksonFacade();
Result result = jackson.fromJson(json, Result.class);

if (!result.hasColors()) {

}

return Optional.of(result);

} catch (ValidationException e) {
Expand Down