From: Aurelien Poscia Date: Mon, 29 Aug 2022 08:11:12 +0000 (+0200) Subject: SONAR-17210 verify Github scanning alert feature is available before posting scanning... X-Git-Tag: 9.7.0.61563~296 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=87d98049ac772e04e949e6bdeff548c6453b0248;p=sonarqube.git SONAR-17210 verify Github scanning alert feature is available before posting scanning alerts --- 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 @@ -33,6 +33,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 @@ -86,6 +93,28 @@ public class GithubApplicationHttpClientImplTest { .hasMessage("invalidUrl/endpoint is not a valid url"); } + @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());