|
| 1 | +package com.mathworks.ci; |
| 2 | + |
| 3 | +import hudson.FilePath; |
| 4 | +import hudson.model.Action; |
| 5 | +import hudson.model.Run; |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Iterator; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import java.util.Map.Entry; |
| 15 | +import javax.annotation.CheckForNull; |
| 16 | +import org.json.simple.JSONArray; |
| 17 | +import org.json.simple.JSONObject; |
| 18 | +import org.json.simple.parser.JSONParser; |
| 19 | +import org.json.simple.parser.ParseException; |
| 20 | + |
| 21 | + |
| 22 | +public class BuildArtifactAction implements Action { |
| 23 | + private Run<?, ?> build; |
| 24 | + private FilePath workspace; |
| 25 | + private int totalCount; |
| 26 | + private int skipCount; |
| 27 | + private int failCount; |
| 28 | + private static final String ROOT_ELEMENT = "taskDetails"; |
| 29 | + private static final String BUILD_ARTIFACT_FILE = "buildArtifact.json"; |
| 30 | + |
| 31 | + public BuildArtifactAction(Run<?, ?> build, FilePath workspace) { |
| 32 | + this.build = build; |
| 33 | + this.workspace = workspace; |
| 34 | + |
| 35 | + // Setting the counts of task when Action is created. |
| 36 | + try{ |
| 37 | + setCounts(); |
| 38 | + } catch (ParseException e) { |
| 39 | + throw new RuntimeException(e); |
| 40 | + } catch (InterruptedException e){ |
| 41 | + throw new RuntimeException(e); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @CheckForNull |
| 46 | + @Override |
| 47 | + public String getIconFileName() { |
| 48 | + return "document.png"; |
| 49 | + } |
| 50 | + |
| 51 | + @CheckForNull |
| 52 | + @Override |
| 53 | + public String getDisplayName() { |
| 54 | + return "MATLAB Build Results"; |
| 55 | + } |
| 56 | + |
| 57 | + @CheckForNull |
| 58 | + @Override |
| 59 | + public String getUrlName() { |
| 60 | + return "buildresults"; |
| 61 | + } |
| 62 | + |
| 63 | + public List<BuildArtifactData> getBuildArtifact() throws ParseException, InterruptedException, IOException { |
| 64 | + List<BuildArtifactData> artifactData = new ArrayList<BuildArtifactData>(); |
| 65 | + FilePath fl = new FilePath(new File(build.getRootDir().getAbsolutePath() + "/" + BUILD_ARTIFACT_FILE)); |
| 66 | + try (InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(fl.toURI())), "UTF-8")) { |
| 67 | + Object obj = new JSONParser().parse(reader); |
| 68 | + JSONObject jo = (JSONObject) obj; |
| 69 | + if (jo.get(ROOT_ELEMENT) instanceof JSONArray) { |
| 70 | + JSONArray ja = (JSONArray) jo.get(ROOT_ELEMENT); |
| 71 | + Iterator itr2 = ja.iterator(); |
| 72 | + Iterator<Entry> itr1; |
| 73 | + while (itr2.hasNext()) { |
| 74 | + BuildArtifactData data = new BuildArtifactData(); |
| 75 | + itr1 = ((Map) itr2.next()).entrySet().iterator(); |
| 76 | + while (itr1.hasNext()) { |
| 77 | + Entry pair = itr1.next(); |
| 78 | + iterateAllTaskAttributes(pair, data); |
| 79 | + } |
| 80 | + artifactData.add(data); |
| 81 | + } |
| 82 | + } else { |
| 83 | + Map ja = ((Map) jo.get(ROOT_ELEMENT)); |
| 84 | + Iterator<Entry> itr1 = ja.entrySet().iterator(); |
| 85 | + BuildArtifactData data = new BuildArtifactData(); |
| 86 | + while (itr1.hasNext()) { |
| 87 | + Entry pair = itr1.next(); |
| 88 | + iterateAllTaskAttributes(pair, data); |
| 89 | + } |
| 90 | + artifactData.add(data); |
| 91 | + } |
| 92 | + } catch (IOException e) { |
| 93 | + throw new IOException(e.getLocalizedMessage()); |
| 94 | + } |
| 95 | + return artifactData; |
| 96 | + } |
| 97 | + |
| 98 | + public void setTotalcount(int totalCount) { |
| 99 | + this.totalCount = totalCount; |
| 100 | + } |
| 101 | + |
| 102 | + public void setSkipCount(int skipCount) { |
| 103 | + this.skipCount = skipCount; |
| 104 | + } |
| 105 | + |
| 106 | + public int getTotalCount() { |
| 107 | + return this.totalCount; |
| 108 | + } |
| 109 | + |
| 110 | + public int getFailCount() { |
| 111 | + return this.failCount; |
| 112 | + } |
| 113 | + |
| 114 | + public void setFailCount(int failCount) { |
| 115 | + this.failCount = failCount; |
| 116 | + } |
| 117 | + |
| 118 | + public int getSkipCount() { |
| 119 | + return this.skipCount; |
| 120 | + } |
| 121 | + |
| 122 | + public Run getOwner() { |
| 123 | + return this.build; |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param owner the owner to set |
| 128 | + */ |
| 129 | + public void setOwner(Run owner) { |
| 130 | + this.build = owner; |
| 131 | + } |
| 132 | + |
| 133 | + public FilePath getWorkspace() { |
| 134 | + return this.workspace; |
| 135 | + } |
| 136 | + |
| 137 | + private void setCounts() throws InterruptedException, ParseException { |
| 138 | + List<BuildArtifactData> artifactData = new ArrayList<BuildArtifactData>(); |
| 139 | + FilePath fl = new FilePath(new File(build.getRootDir().getAbsolutePath() + "/" + BUILD_ARTIFACT_FILE)); |
| 140 | + try (InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(fl.toURI())), "UTF-8")) { |
| 141 | + Object obj = new JSONParser().parse(reader); |
| 142 | + JSONObject jo = (JSONObject) obj; |
| 143 | + |
| 144 | + // getting taskDetails |
| 145 | + if (jo.get(ROOT_ELEMENT) instanceof JSONArray) { |
| 146 | + JSONArray ja = (JSONArray) jo.get(ROOT_ELEMENT); |
| 147 | + Iterator itr2 = ja.iterator(); |
| 148 | + Iterator<Entry> itr1; |
| 149 | + while (itr2.hasNext()) { |
| 150 | + BuildArtifactData data = new BuildArtifactData(); |
| 151 | + itr1 = ((Map) itr2.next()).entrySet().iterator(); |
| 152 | + while (itr1.hasNext()) { |
| 153 | + Entry pair = itr1.next(); |
| 154 | + iterateFailedSkipped(pair, data); |
| 155 | + } |
| 156 | + artifactData.add(data); |
| 157 | + setTotalcount(artifactData.size()); |
| 158 | + } |
| 159 | + } else { |
| 160 | + Map ja = ((Map) jo.get(ROOT_ELEMENT)); |
| 161 | + Iterator<Entry> itr1 = ja.entrySet().iterator(); |
| 162 | + BuildArtifactData data = new BuildArtifactData(); |
| 163 | + while (itr1.hasNext()) { |
| 164 | + Entry pair = itr1.next(); |
| 165 | + iterateFailedSkipped(pair, data); |
| 166 | + } |
| 167 | + artifactData.add(data); |
| 168 | + setTotalcount(artifactData.size()); |
| 169 | + } |
| 170 | + } catch (IOException e) { |
| 171 | + e.printStackTrace(); |
| 172 | + } |
| 173 | + |
| 174 | + // Update the FAILED and SKIPPED task count |
| 175 | + int failCount = 0; |
| 176 | + int skipCount = 0; |
| 177 | + for (BuildArtifactData data : artifactData) { |
| 178 | + if (data.getTaskFailed()) { |
| 179 | + failCount = failCount + 1; |
| 180 | + } else if (data.getTaskSkipped()) { |
| 181 | + skipCount = skipCount + 1; |
| 182 | + } |
| 183 | + } |
| 184 | + // Set count for each failed and skipped tasks |
| 185 | + setFailCount(failCount); |
| 186 | + setSkipCount(skipCount); |
| 187 | + } |
| 188 | + |
| 189 | + private void iterateAllTaskAttributes(Entry pair, BuildArtifactData data) { |
| 190 | + // Iterates across all task attributes and updates |
| 191 | + String key = pair.getKey().toString(); |
| 192 | + switch(key.toLowerCase()){ |
| 193 | + case "duration": |
| 194 | + data.setTaskDuration(pair.getValue().toString()); |
| 195 | + break; |
| 196 | + case "name" : |
| 197 | + data.setTaskName(pair.getValue().toString()); |
| 198 | + break; |
| 199 | + case "description": |
| 200 | + data.setTaskDescription(pair.getValue().toString()); |
| 201 | + break; |
| 202 | + case "failed": |
| 203 | + data.setTaskFailed((Boolean) pair.getValue()); |
| 204 | + break; |
| 205 | + case "skipped": |
| 206 | + data.setTaskSkipped((Boolean) pair.getValue()); |
| 207 | + break; |
| 208 | + default : |
| 209 | + break; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + private void iterateFailedSkipped(Entry pair, BuildArtifactData data) { |
| 214 | + if (pair.getKey().toString().equalsIgnoreCase("failed")) { |
| 215 | + data.setTaskFailed((Boolean) pair.getValue()); |
| 216 | + } else if (pair.getKey().toString().equalsIgnoreCase("skipped")) { |
| 217 | + data.setTaskSkipped((Boolean) pair.getValue()); |
| 218 | + } |
| 219 | + } |
| 220 | +} |
0 commit comments