Skip to content

Commit

Permalink
Rename globalConfigDataForSonarInstance to sonarInstance
Browse files Browse the repository at this point in the history
  • Loading branch information
kirk-fitz committed Sep 10, 2024
1 parent 7c21d92 commit 725d2f3
Show file tree
Hide file tree
Showing 32 changed files with 223 additions and 258 deletions.
20 changes: 9 additions & 11 deletions src/main/java/org/quality/gates/jenkins/plugin/BuildDecision.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,29 @@ public class BuildDecision {

private final QualityGatesProvider qualityGatesProvider;

public BuildDecision(SonarInstance globalConfigDataForSonarInstance) {
qualityGatesProvider = new QualityGatesProvider(globalConfigDataForSonarInstance);
public BuildDecision(SonarInstance sonarInstance) {
qualityGatesProvider = new QualityGatesProvider(sonarInstance);

Check warning on line 12 in src/main/java/org/quality/gates/jenkins/plugin/BuildDecision.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 11-12 are not covered by tests
}

public BuildDecision(QualityGatesProvider qualityGatesProvider) {
this.qualityGatesProvider = qualityGatesProvider;
}

public boolean getStatus(
SonarInstance globalConfigDataForSonarInstance, JobConfigData jobConfigData, BuildListener listener)
public boolean getStatus(SonarInstance sonarInstance, JobConfigData jobConfigData, BuildListener listener)
throws QGException {
try {
return qualityGatesProvider
.getAPIResultsForQualityGates(jobConfigData, globalConfigDataForSonarInstance, listener)
.getAPIResultsForQualityGates(jobConfigData, sonarInstance, listener)
.hasStatusGreen();
} catch (JSONException | InterruptedException e) {
throw new QGException("Please check your credentials or your Project Key", e);
}
}

SonarInstance chooseSonarInstance(GlobalSonarQualityGatesConfiguration globalConfig, JobConfigData jobConfigData) {
if (globalConfig.fetchListOfGlobalConfigData().isEmpty()) {
if (globalConfig.fetchSonarInstances().isEmpty()) {
return noSonarInstance(jobConfigData);
} else if (globalConfig.fetchListOfGlobalConfigData().size() == 1) {
} else if (globalConfig.fetchSonarInstances().size() == 1) {
return singleSonarInstance(globalConfig, jobConfigData);
}

Expand All @@ -45,11 +44,10 @@ private SonarInstance noSonarInstance(JobConfigData jobConfigData) {

private SonarInstance singleSonarInstance(
GlobalSonarQualityGatesConfiguration globalConfig, JobConfigData jobConfigData) {
var globalConfigDataForSonarInstance =
globalConfig.fetchListOfGlobalConfigData().get(0);
jobConfigData.setSonarInstanceName(globalConfigDataForSonarInstance.getName());
var sonarInstance = globalConfig.fetchSonarInstances().get(0);
jobConfigData.setSonarInstanceName(sonarInstance.getName());

return globalConfigDataForSonarInstance;
return sonarInstance;
}

public SonarInstance multipleSonarInstances(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void setListOfGlobalConfigInstances(List<SonarInstance> listOfGlobalConfi

protected List<SonarInstance> instantiateGlobalConfigData(JSONObject json) {
listOfGlobalConfigInstances = new ArrayList<>();
var globalDataConfigs = (JSON) json.opt("listOfGlobalConfigData");
var globalDataConfigs = (JSON) json.opt("sonarInstances");

if (globalDataConfigs == null) {
globalDataConfigs = new JSONArray();
Expand Down Expand Up @@ -61,17 +61,17 @@ protected void addGlobalConfigDataForSonarInstance(JSONObject globalConfigData)
var url = globalConfigData.optString("url");

if (!"".equals(name)) {
SonarInstance globalConfigDataForSonarInstance;
SonarInstance sonarInstance;
var token = globalConfigData.optString("token");
if (StringUtils.isNotEmpty(token)) {
globalConfigDataForSonarInstance = new SonarInstance(
sonarInstance = new SonarInstance(
name,
url,
Secret.fromString(Util.fixEmptyAndTrim(globalConfigData.optString("token"))),
timeToWait,
maxWaitTime);
} else {
globalConfigDataForSonarInstance = new SonarInstance(
sonarInstance = new SonarInstance(
name,
url,
globalConfigData.optString("account"),
Expand All @@ -81,7 +81,7 @@ protected void addGlobalConfigDataForSonarInstance(JSONObject globalConfigData)
}

if (!containsGlobalConfigWithName(name)) {
listOfGlobalConfigInstances.add(globalConfigDataForSonarInstance);
listOfGlobalConfigInstances.add(sonarInstance);

Check warning on line 84 in src/main/java/org/quality/gates/jenkins/plugin/GlobalConfigurationService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 67-84 are not covered by tests
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Symbol("sonarQualityGates")
public class GlobalSonarQualityGatesConfiguration extends GlobalConfiguration {

private List<SonarInstance> listOfGlobalConfigData;
private List<SonarInstance> sonarInstances;

private final GlobalConfigurationService globalConfigurationService;

Expand All @@ -24,28 +24,29 @@ public GlobalSonarQualityGatesConfiguration() {
}

public GlobalSonarQualityGatesConfiguration(
List<SonarInstance> listOfGlobalConfigData, GlobalConfigurationService globalConfigurationService) {
this.listOfGlobalConfigData = listOfGlobalConfigData;
List<SonarInstance> sonarInstances, GlobalConfigurationService globalConfigurationService) {
this.sonarInstances = sonarInstances;
this.globalConfigurationService = globalConfigurationService;
}

public List<SonarInstance> getListOfGlobalConfigData() {
public List<SonarInstance> getSonarInstances() {
load();
return listOfGlobalConfigData;
return sonarInstances;
}

public List<SonarInstance> fetchListOfGlobalConfigData() {
return listOfGlobalConfigData;
public List<SonarInstance> fetchSonarInstances() {
return sonarInstances;

Check warning on line 38 in src/main/java/org/quality/gates/jenkins/plugin/GlobalSonarQualityGatesConfiguration.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 38 is not covered by tests
}

@DataBoundSetter
public void setGlobalConfigDataForSonarInstances(List<SonarInstance> globalConfigDataForSonarInstances) {
this.listOfGlobalConfigData = globalConfigDataForSonarInstances;
public void setSonarInstances(List<SonarInstance> globalConfigDataForSonarInstances) {
this.sonarInstances = globalConfigDataForSonarInstances;
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
listOfGlobalConfigData = globalConfigurationService.instantiateGlobalConfigData(json);
sonarInstances = globalConfigurationService.instantiateGlobalConfigData(json);
req.bindJSON(this, json);
save();

return true;
Expand All @@ -60,9 +61,9 @@ public FormValidation doCheckName(@QueryParameter String name) {
}

public SonarInstance getSonarInstanceByName(String name) {
for (SonarInstance globalConfigDataForSonarInstance : listOfGlobalConfigData) {
if (name.equals(globalConfigDataForSonarInstance.getName())) {
return globalConfigDataForSonarInstance;
for (SonarInstance sonarInstance : sonarInstances) {
if (name.equals(sonarInstance.getName())) {
return sonarInstance;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class JobConfigurationService {
public ListBoxModel getListOfSonarInstanceNames(GlobalSonarQualityGatesConfiguration globalConfig) {
var listBoxModel = new ListBoxModel();

for (SonarInstance globalConfigDataForSonarInstance : globalConfig.fetchListOfGlobalConfigData()) {
listBoxModel.add(globalConfigDataForSonarInstance.getName());
for (SonarInstance sonarInstance : globalConfig.fetchSonarInstances()) {
listBoxModel.add(sonarInstance.getName());
}

return listBoxModel;
Expand All @@ -37,7 +37,7 @@ public JobConfigData createJobConfigData(JSONObject formData, GlobalSonarQuality

var name = "";

if (!globalConfig.fetchListOfGlobalConfigData().isEmpty()) {
if (!globalConfig.fetchSonarInstances().isEmpty()) {

Check warning on line 40 in src/main/java/org/quality/gates/jenkins/plugin/JobConfigurationService.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 40 is only partially covered, one branch is missing
name = hasFormDataKey(formData, globalConfig);
}

Expand All @@ -53,7 +53,7 @@ protected String hasFormDataKey(JSONObject formData, GlobalSonarQualityGatesConf
return formData.getString("sonarInstancesName");
}

return globalConfig.fetchListOfGlobalConfigData().get(0).getName();
return globalConfig.fetchSonarInstances().get(0).getName();
}

public JobConfigData checkProjectKeyIfVariable(
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/org/quality/gates/jenkins/plugin/QGBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ public class QGBuilder extends Builder {

private final JobExecutionService jobExecutionService;

private SonarInstance globalConfigDataForSonarInstance;
private SonarInstance sonarInstance;

@DataBoundConstructor
public QGBuilder(JobConfigData jobConfigData, SonarInstance globalConfigDataForSonarInstance) {
public QGBuilder(JobConfigData jobConfigData, SonarInstance sonarInstance) {
this.jobConfigData = jobConfigData;
this.buildDecision = new BuildDecision(globalConfigDataForSonarInstance);
this.buildDecision = new BuildDecision(sonarInstance);
this.jobExecutionService = new JobExecutionService();
this.jobConfigurationService = new JobConfigurationService();
this.globalConfigDataForSonarInstance = null;
this.sonarInstance = null;

Check warning on line 29 in src/main/java/org/quality/gates/jenkins/plugin/QGBuilder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 24-29 are not covered by tests
}

protected QGBuilder(
JobConfigData jobConfigData,
BuildDecision buildDecision,
JobExecutionService jobExecutionService,
JobConfigurationService jobConfigurationService,
SonarInstance globalConfigDataForSonarInstance) {
SonarInstance sonarInstance) {
this.jobConfigData = jobConfigData;
this.buildDecision = buildDecision;
this.jobExecutionService = jobExecutionService;
this.jobConfigurationService = jobConfigurationService;
this.globalConfigDataForSonarInstance = globalConfigDataForSonarInstance;
this.sonarInstance = sonarInstance;
}

public JobConfigData getJobConfigData() {
Expand All @@ -48,10 +48,9 @@ public JobConfigData getJobConfigData() {

@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
globalConfigDataForSonarInstance =
buildDecision.chooseSonarInstance(jobExecutionService.getGlobalConfigData(), jobConfigData);
sonarInstance = buildDecision.chooseSonarInstance(jobExecutionService.getGlobalConfigData(), jobConfigData);

if (globalConfigDataForSonarInstance == null) {
if (sonarInstance == null) {

Check warning on line 53 in src/main/java/org/quality/gates/jenkins/plugin/QGBuilder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 53 is only partially covered, one branch is missing
listener.error(
JobExecutionService.GLOBAL_CONFIG_NO_LONGER_EXISTS_ERROR, jobConfigData.getSonarInstanceName());
return false;
Expand All @@ -73,7 +72,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
try {
var checkedJobConfigData =
jobConfigurationService.checkProjectKeyIfVariable(jobConfigData, build, listener);
buildHasPassed = buildDecision.getStatus(globalConfigDataForSonarInstance, checkedJobConfigData, listener);
buildHasPassed = buildDecision.getStatus(sonarInstance, checkedJobConfigData, listener);
if ("".equals(jobConfigData.getSonarInstanceName())) {
listener.getLogger().println(JobExecutionService.DEFAULT_CONFIGURATION_WARNING);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public QGBuilderDescriptor(
this.jobExecutionService = jobExecutionService;
}

public ListBoxModel doFillListOfGlobalConfigDataItems() {
public ListBoxModel doFillSonarInstancesItems() {

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing permission check Warning

Potential missing permission check in QGBuilderDescriptor#doFillSonarInstancesItems

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing POST/RequirePOST annotation Warning

Potential CSRF vulnerability: If QGBuilderDescriptor#doFillSonarInstancesItems connects to user-specified URLs, modifies state, or is expensive to run, it should be annotated with @POST or @RequirePOST
return jobConfigurationService.getListOfSonarInstanceNames(jobExecutionService.getGlobalConfigData());
}

Expand Down Expand Up @@ -75,10 +75,10 @@ public boolean configure(StaplerRequest staplerRequest, JSONObject json) throws
public QGBuilder newInstance(StaplerRequest req, JSONObject formData) throws QGException {
var firstInstanceJobConfigData =
jobConfigurationService.createJobConfigData(formData, jobExecutionService.getGlobalConfigData());
var globalConfigDataForSonarInstance = jobExecutionService
var sonarInstance = jobExecutionService
.getGlobalConfigData()
.getSonarInstanceByName(firstInstanceJobConfigData.getSonarInstanceName());

return new QGBuilder(firstInstanceJobConfigData, globalConfigDataForSonarInstance);
return new QGBuilder(firstInstanceJobConfigData, sonarInstance);

Check warning on line 82 in src/main/java/org/quality/gates/jenkins/plugin/QGBuilderDescriptor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 78-82 are not covered by tests
}
}
19 changes: 9 additions & 10 deletions src/main/java/org/quality/gates/jenkins/plugin/QGPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ public class QGPublisher extends Recorder {

private final JobExecutionService jobExecutionService;

private SonarInstance globalConfigDataForSonarInstance;
private SonarInstance sonarInstance;

@DataBoundConstructor
public QGPublisher(JobConfigData jobConfigData, SonarInstance globalConfigDataForSonarInstance) {
public QGPublisher(JobConfigData jobConfigData, SonarInstance sonarInstance) {
this.jobConfigData = jobConfigData;
this.buildDecision = new BuildDecision(globalConfigDataForSonarInstance);
this.buildDecision = new BuildDecision(sonarInstance);
this.jobExecutionService = new JobExecutionService();
this.jobConfigurationService = new JobConfigurationService();
this.globalConfigDataForSonarInstance = null;
this.sonarInstance = null;

Check warning on line 30 in src/main/java/org/quality/gates/jenkins/plugin/QGPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 25-30 are not covered by tests
}

public QGPublisher(
JobConfigData jobConfigData,
BuildDecision buildDecision,
JobExecutionService jobExecutionService,
JobConfigurationService jobConfigurationService,
SonarInstance globalConfigDataForSonarInstance) {
SonarInstance sonarInstance) {
this.jobConfigData = jobConfigData;
this.buildDecision = buildDecision;
this.jobConfigurationService = jobConfigurationService;
this.jobExecutionService = jobExecutionService;
this.globalConfigDataForSonarInstance = globalConfigDataForSonarInstance;
this.sonarInstance = sonarInstance;
}

public JobConfigData getJobConfigData() {
Expand All @@ -59,10 +59,9 @@ public BuildStepMonitor getRequiredMonitorService() {

@Override
public boolean prebuild(AbstractBuild<?, ?> build, BuildListener listener) {
globalConfigDataForSonarInstance =
buildDecision.chooseSonarInstance(jobExecutionService.getGlobalConfigData(), jobConfigData);
sonarInstance = buildDecision.chooseSonarInstance(jobExecutionService.getGlobalConfigData(), jobConfigData);

if (globalConfigDataForSonarInstance == null) {
if (sonarInstance == null) {

Check warning on line 64 in src/main/java/org/quality/gates/jenkins/plugin/QGPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 64 is only partially covered, one branch is missing
listener.error(
JobExecutionService.GLOBAL_CONFIG_NO_LONGER_EXISTS_ERROR, jobConfigData.getSonarInstanceName());
return false;
Expand All @@ -85,7 +84,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
try {
var checkedJobConfigData =
jobConfigurationService.checkProjectKeyIfVariable(jobConfigData, build, listener);
buildHasPassed = buildDecision.getStatus(globalConfigDataForSonarInstance, checkedJobConfigData, listener);
buildHasPassed = buildDecision.getStatus(sonarInstance, checkedJobConfigData, listener);

if ("".equals(jobConfigData.getSonarInstanceName())) {
listener.getLogger().println(JobExecutionService.DEFAULT_CONFIGURATION_WARNING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public JobExecutionService getJobExecutionService() {
return jobExecutionService;
}

public ListBoxModel doFillListOfGlobalConfigDataItems() {
public ListBoxModel doFillSonarInstancesItems() {

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing permission check Warning

Potential missing permission check in QGPublisherDescriptor#doFillSonarInstancesItems

Check warning

Code scanning / Jenkins Security Scan

Stapler: Missing POST/RequirePOST annotation Warning

Potential CSRF vulnerability: If QGPublisherDescriptor#doFillSonarInstancesItems connects to user-specified URLs, modifies state, or is expensive to run, it should be annotated with @POST or @RequirePOST
return jobConfigurationService.getListOfSonarInstanceNames(jobExecutionService.getGlobalConfigData());
}

Expand Down Expand Up @@ -78,10 +78,10 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
public QGPublisher newInstance(StaplerRequest req, JSONObject formData) throws QGException {
var firstInstanceJobConfigData =
jobConfigurationService.createJobConfigData(formData, jobExecutionService.getGlobalConfigData());
var globalConfigDataForSonarInstance = jobExecutionService
var sonarInstance = jobExecutionService
.getGlobalConfigData()
.getSonarInstanceByName(firstInstanceJobConfigData.getSonarInstanceName());

return new QGPublisher(firstInstanceJobConfigData, globalConfigDataForSonarInstance);
return new QGPublisher(firstInstanceJobConfigData, sonarInstance);

Check warning on line 85 in src/main/java/org/quality/gates/jenkins/plugin/QGPublisherDescriptor.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 81-85 are not covered by tests
}
}
Loading

0 comments on commit 725d2f3

Please sign in to comment.