From 13e36c5d8220ec7dbd49e8a23969995cb8285e17 Mon Sep 17 00:00:00 2001 From: ivinokur Date: Wed, 29 Nov 2023 11:14:14 +0200 Subject: [PATCH] fixup! Pass status to Exception on Unauthorised to GitHub Error --- .../api/factory/server/github/AbstractGithubURLParser.java | 5 ++++- .../che/api/factory/server/github/GithubApiClient.java | 2 +- .../che/api/factory/server/github/GithubURLParserTest.java | 7 ++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java b/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java index 382036792f..6ff9f87899 100644 --- a/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java +++ b/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/AbstractGithubURLParser.java @@ -126,7 +126,10 @@ private boolean isApiRequestRelevant(String repositoryUrl) { // belongs to GitHub. githubApiClient.getUser(""); } catch (ScmCommunicationException e) { - return e.getStatusCode() == HTTP_UNAUTHORIZED; + return e.getStatusCode() == HTTP_UNAUTHORIZED + // Check the error message as well, because other providers might also return 401 for + // such requests. + && e.getMessage().contains("Must authenticate to access this API."); } catch (ScmItemNotFoundException | ScmBadRequestException | IllegalArgumentException e) { return false; } diff --git a/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/GithubApiClient.java b/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/GithubApiClient.java index f31b962c00..17e43024fe 100644 --- a/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/GithubApiClient.java +++ b/wsmaster/che-core-api-factory-github-common/src/main/java/org/eclipse/che/api/factory/server/github/GithubApiClient.java @@ -287,7 +287,7 @@ private T executeRequest( throw new ScmItemNotFoundException(body); default: throw new ScmCommunicationException( - "Unexpected status code " + statusCode + " " + response.toString(), statusCode); + "Unexpected status code " + statusCode + " " + body, statusCode); } } } catch (IOException | InterruptedException | UncheckedIOException e) { diff --git a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubURLParserTest.java b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubURLParserTest.java index 69c3c8c94e..86ae08d249 100644 --- a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubURLParserTest.java +++ b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubURLParserTest.java @@ -337,7 +337,12 @@ public void shouldParseServerUrWithPullRequestId() throws Exception { public void shouldValidateGitHubServerUrl() throws Exception { // given String url = wireMockServer.url("/user/repo"); - stubFor(get(urlEqualTo("/api/v3/user")).willReturn(aResponse().withStatus(HTTP_UNAUTHORIZED))); + stubFor( + get(urlEqualTo("/api/v3/user")) + .willReturn( + aResponse() + .withStatus(HTTP_UNAUTHORIZED) + .withBody("{\"message\": \"Must authenticate to access this API.\",\n}"))); // when boolean valid = githubUrlParser.isValid(url);