-
Notifications
You must be signed in to change notification settings - Fork 10
/
TargetBranchChecker.java
97 lines (82 loc) · 3.21 KB
/
TargetBranchChecker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package org.jenkinsci.plugins.codedx;
import com.secdec.codedx.api.client.Branch;
import com.secdec.codedx.api.client.CodeDxClient;
import com.secdec.codedx.api.client.CodeDxClientException;
import com.secdec.codedx.util.CodeDxVersion;
import hudson.AbortException;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
public class TargetBranchChecker {
final private int projectId;
final private CodeDxClient client;
final private ValueResolver resolver;
final private PrintStream logger;
private String targetBranchName, baseBranchName;
public TargetBranchChecker(int projectId, CodeDxClient client, ValueResolver resolver, PrintStream logger) {
this.projectId = projectId;
this.client = client;
this.resolver = resolver;
this.logger = logger;
this.targetBranchName = this.baseBranchName = null;
}
public String getTargetBranchName() {
return this.targetBranchName;
}
public String getBaseBranchName() {
return this.baseBranchName;
}
public void validate(CodeDxVersion codedxVersion, String targetBranch, String baseBranch) throws IOException, InterruptedException {
if (targetBranch == null) {
// no target branch, nothing branch-related to do here
return;
}
if (codedxVersion.compareTo(CodeDxVersion.MIN_FOR_BRANCHING) < 0) {
logger.println(
"The connected Code Dx server with version " + codedxVersion + " does not support project branches. " +
"The minimum required version is " + CodeDxVersion.MIN_FOR_BRANCHING + ". The target branch and base " +
"branch options will be ignored."
);
return;
}
this.targetBranchName = resolver.resolve("target branch", targetBranch);
if (baseBranch != null) {
this.baseBranchName = resolver.resolve("base branch", baseBranch);
}
logger.println("Validating base branch selection...");
List<Branch> availableBranches;
try {
availableBranches = client.getProjectBranches(projectId);
} catch (CodeDxClientException e) {
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(this.baseBranchName)) {
baseBranchExists = true;
} else if (branch.getName().equals(this.targetBranchName)) {
targetBranchExists = true;
}
}
if (targetBranchExists) {
logger.println("Using existing Code Dx branch: " + this.targetBranchName);
// 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
this.baseBranchName = null;
} if (!targetBranchExists) {
if (this.baseBranchName == 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: " + this.baseBranchName);
}
logger.println(
"Analysis will create a new branch named '" +
this.targetBranchName + "' based on the branch '" + this.baseBranchName + "'"
);
}
}
}