aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-alm-client
diff options
context:
space:
mode:
authorAurelien Poscia <aurelien.poscia@sonarsource.com>2022-08-29 10:11:12 +0200
committersonartech <sonartech@sonarsource.com>2022-08-30 20:03:14 +0000
commit87d98049ac772e04e949e6bdeff548c6453b0248 (patch)
tree1bded051cd63bdfd8c542b36d10e8f31cb9023f0 /server/sonar-alm-client
parent914890e41cb7131886143a74acd8c4e5f27ec97a (diff)
downloadsonarqube-87d98049ac772e04e949e6bdeff548c6453b0248.tar.gz
sonarqube-87d98049ac772e04e949e6bdeff548c6453b0248.zip
SONAR-17210 verify Github scanning alert feature is available before posting scanning alerts
Diffstat (limited to 'server/sonar-alm-client')
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClient.java6
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClientImpl.java14
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubApplicationHttpClientImplTest.java29
3 files changed, 48 insertions, 1 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClient.java
index cbdb04b8204..673314b9b9e 100644
--- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClient.java
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClient.java
@@ -34,6 +34,12 @@ public interface GithubApplicationHttpClient {
GetResponse get(String appUrl, AccessToken token, String endPoint) throws IOException;
/**
+ * Content of the response is populated if response's HTTP code is {@link java.net.HttpURLConnection#HTTP_OK OK}.
+ * No log if there is an issue during the call.
+ */
+ GetResponse getSilent(String appUrl, AccessToken token, String endPoint) throws IOException;
+
+ /**
* Content of the response is populated if response's HTTP code is {@link java.net.HttpURLConnection#HTTP_OK OK} or
* {@link java.net.HttpURLConnection#HTTP_CREATED CREATED}.
*/
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClientImpl.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClientImpl.java
index a716055aee1..0572f7ceb32 100644
--- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClientImpl.java
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/github/GithubApplicationHttpClientImpl.java
@@ -68,18 +68,30 @@ public class GithubApplicationHttpClientImpl implements GithubApplicationHttpCli
@Override
public GetResponse get(String appUrl, AccessToken token, String endPoint) throws IOException {
+ return get(appUrl, token, endPoint, true);
+ }
+
+ @Override
+ public GetResponse getSilent(String appUrl, AccessToken token, String endPoint) throws IOException {
+ return get(appUrl, token, endPoint, false);
+ }
+
+ private GetResponse get(String appUrl, AccessToken token, String endPoint, boolean withLog) throws IOException {
validateEndPoint(endPoint);
try (okhttp3.Response response = client.newCall(newGetRequest(appUrl, token, endPoint)).execute()) {
int responseCode = response.code();
if (responseCode != HTTP_OK) {
- LOG.warn("GET response did not have expected HTTP code (was {}): {}", responseCode, attemptReadContent(response));
+ if (withLog) {
+ LOG.warn("GET response did not have expected HTTP code (was {}): {}", responseCode, attemptReadContent(response));
+ }
return new GetResponseImpl(responseCode, null, null);
}
return new GetResponseImpl(responseCode, readContent(response.body()).orElse(null), readNextEndPoint(response));
}
}
+
private static void validateEndPoint(String endPoint) {
checkArgument(endPoint.startsWith("/") || endPoint.startsWith("http"), "endpoint must start with '/' or 'http'");
}
diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubApplicationHttpClientImplTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubApplicationHttpClientImplTest.java
index 64ceedb1212..5287253b837 100644
--- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubApplicationHttpClientImplTest.java
+++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/github/GithubApplicationHttpClientImplTest.java
@@ -29,6 +29,7 @@ import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import okhttp3.mockwebserver.SocketPolicy;
import org.junit.Before;
+import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -37,6 +38,8 @@ import org.sonar.alm.client.github.GithubApplicationHttpClient.GetResponse;
import org.sonar.alm.client.github.GithubApplicationHttpClient.Response;
import org.sonar.alm.client.github.security.AccessToken;
import org.sonar.alm.client.github.security.UserAccessToken;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
import static java.lang.String.format;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
@@ -52,6 +55,9 @@ public class GithubApplicationHttpClientImplTest {
@Rule
public MockWebServer server = new MockWebServer();
+ @ClassRule
+ public static LogTester logTester = new LogTester().setLevel(LoggerLevel.WARN);
+
private GithubApplicationHttpClientImpl underTest;
private final AccessToken accessToken = new UserAccessToken(randomAlphabetic(10));
@@ -63,6 +69,7 @@ public class GithubApplicationHttpClientImplTest {
public void setUp() {
this.appUrl = format("http://%s:%s", server.getHostName(), server.getPort());
this.underTest = new GithubApplicationHttpClientImpl(new ConstantTimeoutConfiguration(500));
+ logTester.clear();
}
@Test
@@ -87,6 +94,28 @@ public class GithubApplicationHttpClientImplTest {
}
@Test
+ public void getSilent_no_log_if_code_is_not_200() throws IOException {
+ server.enqueue(new MockResponse().setResponseCode(403));
+
+ GetResponse response = underTest.getSilent(appUrl, accessToken, randomEndPoint);
+
+ assertThat(logTester.logs()).isEmpty();
+ assertThat(response.getContent()).isEmpty();
+
+ }
+
+ @Test
+ public void get_log_if_code_is_not_200() throws IOException {
+ server.enqueue(new MockResponse().setResponseCode(403));
+
+ GetResponse response = underTest.get(appUrl, accessToken, randomEndPoint);
+
+ assertThat(logTester.logs(LoggerLevel.WARN)).isNotEmpty();
+ assertThat(response.getContent()).isEmpty();
+
+ }
+
+ @Test
public void get_adds_authentication_header_with_Bearer_type_and_Accept_header() throws IOException, InterruptedException {
server.enqueue(new MockResponse());