Skip to content

Commit

Permalink
Minor changes for release (#127)
Browse files Browse the repository at this point in the history
* Remove unnecessary import

* Reduce whitespace and get rid of IDE warnings

* Change "introduced in" versions for actual

* Add missing version info
  • Loading branch information
asimell authored Feb 1, 2022
1 parent 3fb51c8 commit cb0f0a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
6 changes: 3 additions & 3 deletions doc/available_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ In order to publish data for this measurement, your job needs to set an environm
| test_status | string | PASSED, SKIPPED, FAILED, FIXED, REGRESSION | |
| test_status_ordinal | integer | 0-4 in order of test_status | |
| test_duration | float | test duration in seconds | |
| test_duration | float | test duration in seconds | |
| test_count | long | Test counter; Useful for aggregations. ||
| test_duration | float | test duration in seconds | 3.1 |
| test_count | long | Test counter; Useful for aggregations. | 3.1 |

Tags specific for this measurement:

Expand All @@ -92,7 +92,7 @@ Tags specific for this measurement:
| Metric | Type | Description | Introduced in |
| --- | --- | --- | --- |
| alert_status | string | State of the Quality Gate | 2.4 |
| quality_gate_details | string | Provides which quality gate condition is failing and which is not.| 3.0.3 |
| quality_gate_details | string | Provides which quality gate condition is failing and which is not.| 3.1 |
| blocker_issues | float | Total amount of blocker issues | |
| branch_coverage | float | Branch coverage | 2.2 |
| bugs | float | Total amount of bugs | 2.2 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public class SonarQubePointGenerator extends AbstractPointGenerator {

private String token = null;

private EnvVars env = null;
private EnvVars env;

public SonarQubePointGenerator(Run<?, ?> build, TaskListener listener,
ProjectNameRenderer projectNameRenderer,
Expand All @@ -105,18 +105,16 @@ public SonarQubePointGenerator(Run<?, ?> build, TaskListener listener,
* @return true, if environment variable LOG_SONAR_QUBE_RESULTS is set to true and SQ Reports exist
*/
public boolean hasReport() {

try {

try {
String[] result = getSonarProjectFromBuildReport();

projectKey = result[0];
sonarBuildURL = result[1];
sonarBuildTaskId = result[2];
projectKey = result[0];
sonarBuildURL = result[1];
sonarBuildTaskId = result[2];
sonarBuildTaskIdUrl = result[3];

return !StringUtils.isEmpty(sonarBuildURL);
} catch (IOException | IndexOutOfBoundsException e) {}
} catch (IOException | IndexOutOfBoundsException ignored) {}

return false;
}
Expand All @@ -126,29 +124,24 @@ public void setEnv(EnvVars env) {
}

private void waitForQualityGateTask() throws IOException {

String status = null;
String output = null;
String logMessage = null;

String status;
String output;
String logMessage;
int count = 0;

int MAX_RETRY_COUNT = DEFAULT_MAX_RETRY_COUNT;

String max_retry = this.env.get("SONAR_TASK_MAX_RETRY_COUNT");

if (max_retry != null && !max_retry.isEmpty()) {
try {
MAX_RETRY_COUNT = Integer.parseInt(max_retry);
} catch(NumberFormatException e) {
MAX_RETRY_COUNT = DEFAULT_MAX_RETRY_COUNT;
}
} catch(NumberFormatException ignored) { }
}

do {
try {
Thread.sleep(DEFAULT_RETRY_SLEEP);
} catch(InterruptedException e) {}
} catch(InterruptedException ignored) {}

output = getResult(sonarBuildTaskIdUrl);
JSONObject taskObjects = JSONObject.fromObject(output);
Expand Down Expand Up @@ -200,7 +193,6 @@ public Point[] generate() {

Point point = null;
try {

waitForQualityGateTask();

point = buildPoint("sonarqube_data", customPrefix, build)
Expand Down Expand Up @@ -244,7 +236,6 @@ protected String getResult(String url) throws IOException {
}

try (Response response = httpClient.newCall(requestBuilder.build()).execute()) {

if (response.code() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.code() + " from URL : " + url);
}
Expand All @@ -262,19 +253,18 @@ private String[] getSonarProjectFromBuildReport() throws IOException {
String url = null;
String taskId = null;
String taskUrl = null;
String[] rtr_str_array = {projName, url, taskId, taskUrl};

String workspaceDir = env.get("WORKSPACE", "");
List<Path> reportsPaths = this.findReportByFileName(workspaceDir);

if (reportsPaths.size() != 1) {
return rtr_str_array;
return new String[]{null, null, null, null};
}
String reportFilePath = reportsPaths.get(0).toFile().getPath();

try (BufferedReader br = new BufferedReader(
new InputStreamReader(
new FileInputStream(reportFilePath), "UTF-8"))) {
new FileInputStream(reportFilePath), StandardCharsets.UTF_8))) {
String line;

Pattern p_proj_name = Pattern.compile(PROJECT_KEY_PATTERN_IN_REPORT);
Expand Down Expand Up @@ -304,14 +294,11 @@ private String[] getSonarProjectFromBuildReport() throws IOException {
match = p_url_task.matcher(line);
if (match.matches()) {
taskUrl = match.group(1);
continue;
}
}
}
}

rtr_str_array = new String[]{projName, url, taskId, taskUrl};

return rtr_str_array;
return new String[]{projName, url, taskId, taskUrl};
}

public List<Path> findReportByFileName(String workspacePath)
Expand Down Expand Up @@ -339,7 +326,7 @@ public Float getSonarMetric(String url, String metric) throws IOException {
Float value = null;
try {
value = Float.parseFloat(getSonarMetricValue(url, metric));
} catch (NumberFormatException exp) {}
} catch (NumberFormatException ignored) {}

return value;
}
Expand All @@ -354,7 +341,7 @@ private String getSonarMetricValue(String url, String metric) throws IOException
JSONArray array = metricsObjects.getJSONObject("component").getJSONArray("measures");
JSONObject metricsObject = array.getJSONObject(0);
value = metricsObject.getString("value");
} catch (IndexOutOfBoundsException exp) {}
} catch (IndexOutOfBoundsException ignored) {}

return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.Extension;
Expand Down

0 comments on commit cb0f0a9

Please sign in to comment.