From d738c7b0bbcbe0ce48adff1ceac21485df0cf7fd Mon Sep 17 00:00:00 2001 From: Tyler Camp Date: Mon, 9 Jan 2023 14:57:35 -0500 Subject: [PATCH] Add help sections for new fields, rearrange branch validation logic --- .../plugins/codedx/CodeDxPublisher.java | 45 ++++++++++++------- .../codedx/CodeDxPublisher/config.jelly | 6 +-- src/main/webapp/help-baseBranch.html | 16 +++++++ src/main/webapp/help-targetBranch.html | 10 +++++ 4 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 src/main/webapp/help-baseBranch.html create mode 100644 src/main/webapp/help-targetBranch.html diff --git a/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java b/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java index 2339fb2..426cfd5 100644 --- a/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java +++ b/src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java @@ -202,8 +202,10 @@ public String getTargetBranchName() { @DataBoundSetter public void setTargetBranchName(String targetBranchName) { + if (targetBranchName != null) targetBranchName = targetBranchName.trim(); + if (targetBranchName != null && targetBranchName.length() > 0) - this.targetBranchName = targetBranchName.trim(); + this.targetBranchName = targetBranchName; else this.targetBranchName = null; } @@ -214,8 +216,10 @@ public String getBaseBranchName() { @DataBoundSetter public void setBaseBranchName(String baseBranchName) { + if (baseBranchName != null) baseBranchName = baseBranchName.trim(); + if (baseBranchName != null && baseBranchName.length() > 0) - this.baseBranchName = baseBranchName.trim(); + this.baseBranchName = baseBranchName; else this.baseBranchName = null; } @@ -296,10 +300,6 @@ public void perform( ); } - if (baseBranchName == null) { - throw new AbortException("A parent branch must be specified when using a target branch"); - } - try { effectiveTargetBranch = TokenMacro.expandAll(build, workspace, listener, targetBranchName); } catch (MacroEvaluationException e) { @@ -309,7 +309,9 @@ public void perform( } try { - effectiveBaseBranch = TokenMacro.expandAll(build, workspace, listener, baseBranchName); + if (baseBranchName != null) { + effectiveBaseBranch = TokenMacro.expandAll(build, workspace, listener, baseBranchName); + } } catch (MacroEvaluationException e) { buildOutput.println("Macro expansion for base branch failed, falling back to default behavior"); buildOutput.println(e); @@ -324,21 +326,34 @@ public void perform( throw new IOException("An error occurred when fetching available branches for project " + projectId, e); } + boolean targetBranchExists = false; boolean baseBranchExists = false; for (Branch branch : availableBranches) { - if (branch.getName().equals(baseBranchName)) { + if (branch.getName().equals(effectiveBaseBranch)) { baseBranchExists = true; - break; + } else if (branch.getName().equals(effectiveTargetBranch)) { + targetBranchExists = true; } } + if (targetBranchExists) { + buildOutput.println("Using existing Code Dx branch: " + effectiveTargetBranch); + // not necessary, base branch is currently ignored in the backend if the target + // branch already exists. just setting to null to safeguard in case of future changes + effectiveBaseBranch = null; + } if (!targetBranchExists) { + if (effectiveBaseBranch == null) { + throw new AbortException("A parent branch must be specified when using a target branch"); + } - if (!baseBranchExists) { - throw new AbortException("The specified parent branch does not exist: " + baseBranchName); - } - } + if (!baseBranchExists) { + throw new AbortException("The specified parent branch does not exist: " + effectiveBaseBranch); + } - if (effectiveTargetBranch != null) { - buildOutput.println("Code Dx branch will be set to " + effectiveTargetBranch + " with base branch " + effectiveBaseBranch); + buildOutput.println( + "Analysis will create a new branch named '" + + effectiveTargetBranch + "' based on the branch '" + effectiveBaseBranch + "'" + ); + } } try { 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 d00b9f9..3005a1b 100644 --- a/src/main/resources/org/jenkinsci/plugins/codedx/CodeDxPublisher/config.jelly +++ b/src/main/resources/org/jenkinsci/plugins/codedx/CodeDxPublisher/config.jelly @@ -36,13 +36,11 @@ - - + - - + diff --git a/src/main/webapp/help-baseBranch.html b/src/main/webapp/help-baseBranch.html new file mode 100644 index 0000000..719dd93 --- /dev/null +++ b/src/main/webapp/help-baseBranch.html @@ -0,0 +1,16 @@ +
+ + The Code Dx branch to use as the parent if the specified "target branch" does not exist yet. + The new branch will inherit the findings of the base branch. The branch must exist in the + Code Dx project beforehand. + + + This field is ignored if a "target branch" is not specified, or if the target branch already exists. + + + This field is required if the "target branch" does not exist yet. + + + ("Branches" refer to branches within the Code Dx project, not SCM branches.) + +
\ No newline at end of file diff --git a/src/main/webapp/help-targetBranch.html b/src/main/webapp/help-targetBranch.html new file mode 100644 index 0000000..848fb2f --- /dev/null +++ b/src/main/webapp/help-targetBranch.html @@ -0,0 +1,10 @@ +
+ + The Code Dx branch to store analysis results in. If the branch does not exist, it + will be created with the specified "base branch". The "base branch" parameter is required + if the target branch does not exist yet. + + + ("Branches" refer to branches within the Code Dx project, not SCM branches.) + +
\ No newline at end of file