From fc877483189b99657dab8d376a699deccb5ca481 Mon Sep 17 00:00:00 2001 From: Tyler Camp Date: Wed, 11 Jan 2023 14:58:33 -0500 Subject: [PATCH] Add support for checking policy status in build conditions --- .../com/codedx/api/client/CodeDxClient.java | 10 ++ .../java/com/codedx/util/CodeDxVersion.java | 2 + .../plugins/codedx/AnalysisResultChecker.java | 22 ++- .../codedx/AnalysisResultConfiguration.java | 10 +- .../plugins/codedx/CodeDxPublisher.java | 1 + .../codedx/CodeDxPublisher/config.jelly | 151 ++++++++++-------- src/main/webapp/help-breakForPolicy.html | 9 ++ 7 files changed, 133 insertions(+), 72 deletions(-) create mode 100644 src/main/webapp/help-breakForPolicy.html diff --git a/src/main/java/com/codedx/api/client/CodeDxClient.java b/src/main/java/com/codedx/api/client/CodeDxClient.java index 0b2898c..8268ae9 100644 --- a/src/main/java/com/codedx/api/client/CodeDxClient.java +++ b/src/main/java/com/codedx/api/client/CodeDxClient.java @@ -302,6 +302,16 @@ public List getProjectBranches(ProjectContext project) throws IOExceptio ); } + public boolean projectPolicyShouldBreakTheBuild(ProjectContext project) throws IOException, CodeDxClientException { + return doHttpRequest( + new HttpGet(), + "projects/" + project.toString() + "/policies/any/build-broken", + true, + new TypeToken(){}.getType(), + null + ); + } + /** * Perform an HttpRequest to the given api path, with an optional request body, and parse the response * @param request Generally a new `HttpGet`, `HttpPost`, or `HttpPut` diff --git a/src/main/java/com/codedx/util/CodeDxVersion.java b/src/main/java/com/codedx/util/CodeDxVersion.java index 111c8ad..c7764a1 100644 --- a/src/main/java/com/codedx/util/CodeDxVersion.java +++ b/src/main/java/com/codedx/util/CodeDxVersion.java @@ -48,6 +48,8 @@ public final class CodeDxVersion implements Comparable { // until 2022.4.3 public final static CodeDxVersion MIN_FOR_BRANCHING = fromString("2022.4.3"); + public final static CodeDxVersion MIN_FOR_POLICIES = fromString("2023.1.0"); + public static CodeDxVersion fromString(String version){ // format is expected to be "x(.y)*-abc", and we want the x.y.z part Pattern versionRegex = Pattern.compile("^(\\d+(?:\\.\\d+)*).*"); diff --git a/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultChecker.java b/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultChecker.java index 901ff2d..5a3c0d9 100644 --- a/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultChecker.java +++ b/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultChecker.java @@ -45,12 +45,13 @@ public class AnalysisResultChecker { private Date newThreshold; private boolean failureOnlyNew; private boolean unstableOnlyNew; + private boolean breakForPolicy; private PrintStream logger; private ProjectContext project; public AnalysisResultChecker(CodeDxClient client, CodeDxVersion cdxVersion, String failureSeverity, String unstableSeverity, Date newThreshold, boolean failureOnlyNew, - boolean unstableOnlyNew, ProjectContext project, PrintStream logger) { + boolean unstableOnlyNew, boolean breakForPolicy, ProjectContext project, PrintStream logger) { this.client = client; this.cdxVersion = cdxVersion; @@ -59,11 +60,28 @@ public AnalysisResultChecker(CodeDxClient client, CodeDxVersion cdxVersion, Stri this.newThreshold = newThreshold; this.failureOnlyNew = failureOnlyNew; this.unstableOnlyNew = unstableOnlyNew; + this.breakForPolicy = breakForPolicy; this.project = project; this.logger = logger; + + if (breakForPolicy && cdxVersion.compareTo(CodeDxVersion.MIN_FOR_POLICIES) < 0) { + logger.println( + "The discovered Code Dx version " + cdxVersion.toString() + " is older than the minimum required " + + "version for Policies (" + CodeDxVersion.MIN_FOR_POLICIES + "), policy-related options will be ignored." + ); + this.breakForPolicy = false; + } } - public Result checkResult() throws ClientProtocolException, CodeDxClientException, IOException{ + public Result checkResult() throws ClientProtocolException, CodeDxClientException, IOException { + + if (breakForPolicy) { + logger.println("Checking for build-breaking policy violations..."); + if (client.projectPolicyShouldBreakTheBuild(project)) { + logger.println("Failure: At least one Policy is violated and requires build failure"); + return Result.FAILURE; + } + } logger.println("Checking for findings that indicate build failure..."); if(!"None".equalsIgnoreCase(failureSeverity) && client.getFindingsCount(project, createFilter(failureSeverity, failureOnlyNew)) > 0){ diff --git a/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultConfiguration.java b/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultConfiguration.java index 3266126..b70988f 100644 --- a/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultConfiguration.java +++ b/src/main/java/org/jenkinsci/plugins/codedx/AnalysisResultConfiguration.java @@ -30,12 +30,13 @@ public class AnalysisResultConfiguration { private boolean unstableOnlyNew; private int numBuildsInGraph; private boolean breakIfFailed; + private boolean breakForPolicy; @DataBoundConstructor public AnalysisResultConfiguration(String failureSeverity, String unstableSeverity, boolean failureOnlyNew, boolean unstableOnlyNew, int numBuildsInGraph, - boolean breakIfFailed) { + boolean breakIfFailed, boolean breakForPolicy) { this.failureSeverity = failureSeverity; this.unstableSeverity = unstableSeverity; @@ -43,6 +44,7 @@ public AnalysisResultConfiguration(String failureSeverity, this.unstableOnlyNew = unstableOnlyNew; this.numBuildsInGraph = numBuildsInGraph; this.breakIfFailed = breakIfFailed; + this.breakForPolicy = breakForPolicy; } public String getFailureSeverity() { return failureSeverity; @@ -80,4 +82,10 @@ public boolean getBreakIfFailed() { public void setBreakIfFailed(boolean breakIfFailed) { this.breakIfFailed = breakIfFailed; } + public boolean getBreakForPolicy() { + return breakForPolicy; + } + public void setBreakForPolicy(boolean breakForPolicy) { + this.breakForPolicy = breakForPolicy; + } } diff --git a/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java b/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java index 5a93c51..8078724 100644 --- a/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java +++ b/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java @@ -441,6 +441,7 @@ public void perform( startingDate, // the time this process started is the "new" threshold for filtering analysisResultConfiguration.isFailureOnlyNew(), analysisResultConfiguration.isUnstableOnlyNew(), + analysisResultConfiguration.getBreakForPolicy(), project, buildOutput); Result buildResult = checker.checkResult(); diff --git a/src/main/resources/org/jenkinsci/plugins/codedx/CodeDxPublisher/config.jelly b/src/main/resources/org/jenkinsci/plugins/codedx/CodeDxPublisher/config.jelly index f330a3c..690ca4c 100644 --- a/src/main/resources/org/jenkinsci/plugins/codedx/CodeDxPublisher/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/codedx/CodeDxPublisher/config.jelly @@ -1,98 +1,111 @@ - - + See global.jelly for a general discussion about jelly script. + --> - - + + - + - - - + + + - + - - - + + + - - - + + + - - + + - + - + - - - - - - - - - - - - - - - - - - - - - - + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + - - - - - - - - - - - - - - - - - - - - - - - - - +
diff --git a/src/main/webapp/help-breakForPolicy.html b/src/main/webapp/help-breakForPolicy.html new file mode 100644 index 0000000..beeceb7 --- /dev/null +++ b/src/main/webapp/help-breakForPolicy.html @@ -0,0 +1,9 @@ +
+

+ If enabled, will consider the build a failure if at least one of the Code Dx project's violated policies has + its action set to "Break the build". +

+

+ (Note: policies are supported in Code Dx 2023.1.0 and up. This option will be ignored if using an older Code Dx version.) +

+
\ No newline at end of file