Skip to content

Commit

Permalink
fixup! 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 29, 2023
1 parent 0fbd872 commit 13e36c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ private <T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 13e36c5

Please sign in to comment.