Skip to content

Commit

Permalink
Add help sections for new fields, rearrange branch validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tylercamp committed Jan 9, 2023
1 parent 2135ff1 commit d738c7b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
45 changes: 30 additions & 15 deletions src/main/java/org/jenkinsci/plugins/codedx/CodeDxPublisher.java
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
Expand Up @@ -36,13 +36,11 @@
<f:textbox default="Build #$${BUILD_NUMBER}" />
</f:entry>

<f:entry title="Target Branch" field="targetBranchName">
<!-- TODO - Add help page -->
<f:entry title="Target Branch" field="targetBranchName" help="/plugin/codedx/help-targetBranch.html">
<f:textbox />
</f:entry>

<f:entry title="Base Branch" field="baseBranchName">
<!-- TODO - Add help page -->
<f:entry title="Base Branch" field="baseBranchName" help="/plugin/codedx/help-baseBranch.html">
<f:textbox />
</f:entry>

Expand Down
16 changes: 16 additions & 0 deletions src/main/webapp/help-baseBranch.html
@@ -0,0 +1,16 @@
<div>
<span>
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.
</span>
<span>
This field is ignored if a "target branch" is not specified, or if the target branch already exists.
</span>
<span>
This field is required if the "target branch" does not exist yet.
</span>
<span>
("Branches" refer to branches within the Code Dx project, not SCM branches.)
</span>
</div>
10 changes: 10 additions & 0 deletions src/main/webapp/help-targetBranch.html
@@ -0,0 +1,10 @@
<div>
<span>
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.
</span>
<span>
("Branches" refer to branches within the Code Dx project, not SCM branches.)
</span>
</div>

0 comments on commit d738c7b

Please sign in to comment.