From 8aaf918353f97ac72491487ce1a7c40baa28af95 Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Mon, 11 Jul 2022 15:26:06 +0300 Subject: [PATCH] fix: Avoid GitHub api request without token (#325) GitHub allows to use only 60 unauthorized reqests per host. In order to avid API rate limit exceeded GitHub eroor, substitute the unauthorized GitHub API request to an HTTP repository request. --- .../github/GithubAuthorizingFileContentProvider.java | 6 +++--- .../github/GithubAuthorizingFileContentProviderTest.java | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/wsmaster/che-core-api-factory-github/src/main/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProvider.java b/wsmaster/che-core-api-factory-github/src/main/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProvider.java index e2036a427e..594e2575b1 100644 --- a/wsmaster/che-core-api-factory-github/src/main/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProvider.java +++ b/wsmaster/che-core-api-factory-github/src/main/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProvider.java @@ -43,10 +43,10 @@ class GithubAuthorizingFileContentProvider extends AuthorizingFileContentProvide protected boolean isPublicRepository(GithubUrl remoteFactoryUrl) { try { urlFetcher.fetch( - GithubApiClient.GITHUB_API_SERVER - + "/repos/" + remoteFactoryUrl.getHostName() + + '/' + remoteFactoryUrl.getUsername() - + "/" + + '/' + remoteFactoryUrl.getRepository()); return true; } catch (IOException e) { diff --git a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java index d30864463b..db7b98ed73 100644 --- a/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java +++ b/wsmaster/che-core-api-factory-github/src/test/java/org/eclipse/che/api/factory/server/github/GithubAuthorizingFileContentProviderTest.java @@ -19,6 +19,7 @@ import java.io.FileNotFoundException; import org.eclipse.che.api.factory.server.scm.GitCredentialManager; import org.eclipse.che.api.factory.server.scm.PersonalAccessToken; import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager; +import org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException; import org.eclipse.che.api.workspace.server.devfile.FileContentProvider; import org.eclipse.che.api.workspace.server.devfile.URLFetcher; import org.mockito.Mock; @@ -70,11 +71,10 @@ public class GithubAuthorizingFileContentProviderTest { public void shouldThrowNotFoundForPublicRepos() throws Exception { URLFetcher urlFetcher = Mockito.mock(URLFetcher.class); String url = "https://raw.githubusercontent.com/foo/bar/devfile.yaml"; - when(urlFetcher.fetch(eq(url), anyString())).thenThrow(FileNotFoundException.class); - var personalAccessToken = new PersonalAccessToken(url, "che", "token"); when(personalAccessTokenManager.fetchAndSave(any(), anyString())) - .thenReturn(personalAccessToken); - when(urlFetcher.fetch(eq("https://api.github.com/repos/eclipse/che"))).thenReturn("OK"); + .thenThrow(UnknownScmProviderException.class); + when(urlFetcher.fetch(eq(url))).thenThrow(FileNotFoundException.class); + when(urlFetcher.fetch(eq("https://github.com/eclipse/che"))).thenReturn("OK"); GithubUrl githubUrl = new GithubUrl().withUsername("eclipse").withRepository("che"); FileContentProvider fileContentProvider = new GithubAuthorizingFileContentProvider(