Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change token to credentials for sonarqube #152

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.influxdb.client.write.Point;
import hudson.EnvVars;
import jenkinsci.plugins.influxdb.renderer.ProjectNameRenderer;
Expand All @@ -21,6 +22,7 @@
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;

import hudson.model.Run;
import hudson.model.TaskListener;
Expand Down Expand Up @@ -91,7 +93,7 @@ public class SonarQubePointGenerator extends AbstractPointGenerator {
private final String customPrefix;
private final TaskListener listener;

private String token = null;
private StringCredentials sonarqubeCredentials;

private EnvVars env;

Expand Down Expand Up @@ -200,10 +202,11 @@ private void setSonarDetails(String sonarBuildURL) {

sonarMetricsUrl = sonarServer + String.format(SONAR_METRICS_BASE_URL, projectKey, projectKey);

token = env.get("SONAR_AUTH_TOKEN");
if (token != null) {
String credentialsId = env.get("SONAR_AUTH_TOKEN");
if (credentialsId != null) {
String logMessage = "[InfluxDB Plugin] INFO: Using SonarQube auth token found in environment variable SONAR_AUTH_TOKEN";
listener.getLogger().println(logMessage);
sonarqubeCredentials = CredentialsProvider.findCredentialById(credentialsId, StringCredentials.class, build);
} else {
String logMessage = "[InfluxDB Plugin] WARNING: No SonarQube auth token found in environment variable SONAR_AUTH_TOKEN. Depending on access rights, this might result in a HTTP/401.";
listener.getLogger().println(logMessage);
Expand Down Expand Up @@ -254,9 +257,11 @@ protected String getResult(String url) throws IOException {
.url(url)
.header("Accept", "application/json");

if (token != null) {
String credential = Credentials.basic(token, "", StandardCharsets.UTF_8);
if (sonarqubeCredentials != null) {
String credential = Credentials.basic(sonarqubeCredentials.getSecret().getPlainText(), "", StandardCharsets.UTF_8);
requestBuilder.header("Authorization", credential);
} else {
listener.error("Unable to find valid Credentials for Sonarqube");
}

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