Skip to content

Commit

Permalink
Pass status to Exception on Unauthorised to GitHub Error
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Nov 27, 2023
1 parent 974c1b1 commit 0fbd872
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ private boolean isUserTokenPresent(String repositoryUrl) {
private boolean isApiRequestRelevant(String repositoryUrl) {
Optional<String> serverUrlOptional = getServerUrl(repositoryUrl);
if (serverUrlOptional.isPresent()) {
GithubApiClient GithubApiClient = new GithubApiClient(serverUrlOptional.get());
GithubApiClient githubApiClient = new GithubApiClient(serverUrlOptional.get());
try {
// If the user request catches the unauthorised error, it means that the provided url
// belongs to GitHub.
GithubApiClient.getUser("");
githubApiClient.getUser("");
} catch (ScmCommunicationException e) {
return e.getStatusCode() == HTTP_UNAUTHORIZED;
} catch (ScmItemNotFoundException | ScmBadRequestException | IllegalArgumentException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,24 +269,25 @@ private <T> T executeRequest(
try {
HttpResponse<InputStream> response =
httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
LOG.trace("executeRequest={} response {}", request, response.statusCode());
if (response.statusCode() == HTTP_OK) {
int statusCode = response.statusCode();
LOG.trace("executeRequest={} response {}", request, statusCode);
if (statusCode == HTTP_OK) {
return responseConverter.apply(response);
} else if (response.statusCode() == HTTP_NO_CONTENT) {
} else if (statusCode == HTTP_NO_CONTENT) {
return null;
} else {
String body =
response.body() == null
? "Unrecognised error"
: CharStreams.toString(new InputStreamReader(response.body(), Charsets.UTF_8));
switch (response.statusCode()) {
switch (statusCode) {
case HTTP_BAD_REQUEST:
throw new ScmBadRequestException(body);
case HTTP_NOT_FOUND:
throw new ScmItemNotFoundException(body);
default:
throw new ScmCommunicationException(
"Unexpected status code " + response.statusCode() + " " + response.toString());
"Unexpected status code " + statusCode + " " + response.toString(), statusCode);
}
}
} catch (IOException | InterruptedException | UncheckedIOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
*/
package org.eclipse.che.api.factory.server.github;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
Expand All @@ -22,6 +28,9 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import java.util.Optional;
import org.eclipse.che.api.core.ApiException;
import org.eclipse.che.api.factory.server.scm.PersonalAccessToken;
Expand Down Expand Up @@ -49,9 +58,17 @@ public class GithubURLParserTest {
/** Instance of component that will be tested. */
private GithubURLParser githubUrlParser;

WireMockServer wireMockServer;
WireMock wireMock;

/** Setup objects/ */
@BeforeMethod
protected void start() throws ApiException {
wireMockServer =
new WireMockServer(wireMockConfig().notifier(new Slf4jNotifier(false)).dynamicPort());
wireMockServer.start();
WireMock.configureFor("localhost", wireMockServer.port());
wireMock = new WireMock("localhost", wireMockServer.port());
this.personalAccessTokenManager = mock(PersonalAccessTokenManager.class);
this.githubApiClient = mock(GithubApiClient.class);

Expand Down Expand Up @@ -315,4 +332,17 @@ public void shouldParseServerUrWithPullRequestId() throws Exception {
verify(personalAccessTokenManager, times(2))
.get(any(Subject.class), eq("https://github-server.com"));
}

@Test
public void shouldValidateGitHubServerUrl() throws Exception {
// given
String url = wireMockServer.url("/user/repo");
stubFor(get(urlEqualTo("/api/v3/user")).willReturn(aResponse().withStatus(HTTP_UNAUTHORIZED)));

// when
boolean valid = githubUrlParser.isValid(url);

// then
assertTrue(valid);
}
}

0 comments on commit 0fbd872

Please sign in to comment.