Skip to content

Commit

Permalink
Merge pull request #85 from mikecirioli/fix_cnfe_2
Browse files Browse the repository at this point in the history
Fixes getBuildCause(String classname) so that it no longer relies on class loading
  • Loading branch information
dwnusbaum committed Dec 11, 2018
2 parents 68f35ca + 1ca0217 commit 2a639b6
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,18 @@ public int getNumber() throws AbortException {

@Whitelisted
public JSONArray getBuildCauses() throws IOException, ClassNotFoundException {
return getBuildCauses("hudson.model.Cause");
JSONArray result = new JSONArray();

for(Cause cause : build().getCauses()) {
StringWriter w = new StringWriter();
CauseAction causeAction = new CauseAction(cause);
DataWriter writer = JSON.createDataWriter(causeAction, w);
Model<CauseAction> model = new ModelBuilder().get(CauseAction.class);
model.writeTo(causeAction, writer);
// return a slightlly cleaner object by removing the outer object
result.add(JSONObject.fromObject(w.toString()).getJSONArray("causes").get(0));
}
return result;
}

/**
Expand All @@ -151,18 +162,17 @@ public JSONArray getBuildCauses() throws IOException, ClassNotFoundException {
* @throws IOException
*/
@Whitelisted
public JSONArray getBuildCauses(String className) throws IOException, ClassNotFoundException {
Class clazz = Class.forName(className);
public JSONArray getBuildCauses(String className) throws IOException {
JSONArray result = new JSONArray();

for(Cause cause : build().getCauses()) {
if (clazz.isInstance(cause)) {
if (className.equals(cause.getClass().getName())) {
StringWriter w = new StringWriter();
CauseAction causeAction = new CauseAction(cause);
DataWriter writer = JSON.createDataWriter(causeAction, w);
Model<CauseAction> model = new ModelBuilder().get(CauseAction.class);
model.writeTo(causeAction, writer);
// return a slightlly cleaner object by removing the outer object
// return a slightly cleaner object by removing the outer object
result.add(JSONObject.fromObject(w.toString()).getJSONArray("causes").get(0));
}
}
Expand Down

0 comments on commit 2a639b6

Please sign in to comment.