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 dependency check results to Jenkins REST API #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,20 @@
import org.jenkinsci.plugins.DependencyCheck.model.Finding;
import org.jenkinsci.plugins.DependencyCheck.model.SeverityDistribution;
import org.jenkinsci.plugins.DependencyCheck.transformer.FindingsTransformer;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.json.JsonHttpResponse;
import org.kohsuke.stapler.verb.GET;

import hudson.model.Action;
import hudson.model.Api;
import hudson.model.Run;
import io.jenkins.plugins.util.AbstractXmlStream;
import io.jenkins.plugins.util.BuildAction;
import io.jenkins.plugins.util.JobAction;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

Expand All @@ -43,6 +50,7 @@
* @author Steve Springett ([email protected])
* @since 5.0.0
*/
@ExportedBean
public class ResultAction extends BuildAction<DependencyCheckBuildResult> {

private static final long serialVersionUID = -6533677178186658819L;
Expand Down Expand Up @@ -135,4 +143,47 @@
return JSONObject.fromObject(getSeverityDistribution(), jsonConfig);
}

public Api getApi() {
Copy link
Member

Choose a reason for hiding this comment

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

Miss API documentation (_api.jelly in the parent object folder)

return new Api(this);

Check warning on line 147 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 147 is not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

in this case we have to return only severity distribution so you can return new Api(getSeverityDistribution()) without add all other methods (getSeverityDistribution trigger a lock) and keep the buildaction clean

}

@Exported(visibility = 2)
public int getCriticalCount() {
return getSeverityDistribution().getCritical();

Check warning on line 152 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 152 is not covered by tests
}

@Exported(visibility = 2)
public int getHighCount() {
return getSeverityDistribution().getHigh();

Check warning on line 157 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 157 is not covered by tests
}

@Exported(visibility = 2)
public int getMediumCount() {
return getSeverityDistribution().getMedium();

Check warning on line 162 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 162 is not covered by tests
}

@Exported(visibility = 2)
public int getLowCount() {
return getSeverityDistribution().getLow();

Check warning on line 167 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 167 is not covered by tests
}

@Exported(visibility = 2)
public int getInfoCount() {
return getSeverityDistribution().getInfo();

Check warning on line 172 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 172 is not covered by tests
}

@Exported(visibility = 2)
public int getUnassignedCount() {
return getSeverityDistribution().getUnassigned();

Check warning on line 177 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 177 is not covered by tests
}

@GET
@WebMethod(name = "findings")
public JsonHttpResponse getFindingsExposedInRemoteApi() {
Copy link
Member

Choose a reason for hiding this comment

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

I would not expose result unless a valid use case

JSONObject tResult = new JSONObject();

Check warning on line 183 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 183 is not covered by tests
JsonConfig jsonConfig = new JsonConfig();

Check warning on line 184 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 184 is not covered by tests
jsonConfig.setExcludes(new String[] { "count", });

Check warning on line 185 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 185 is not covered by tests
tResult.put("findings", JSONArray.fromObject(getFindings(), jsonConfig));

Check warning on line 186 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 186 is not covered by tests
return new JsonHttpResponse(tResult, 200);

Check warning on line 187 in src/main/java/org/jenkinsci/plugins/DependencyCheck/ResultAction.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 187 is not covered by tests
}
}