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

Fixes getBuildCause(String classname) so that it no longer relies on class loading #85

Merged
merged 2 commits into from
Dec 11, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case was covered by the existing tests, the last test case supplies two cause types and filters by Cause$UserIdCause to check that only one is returned.

result.add(JSONObject.fromObject(w.toString()).getJSONArray("causes").get(0));
}
return result;
}

/**
Expand All @@ -151,12 +162,11 @@ 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);
Expand Down