Skip to content

Commit

Permalink
fix: Avoid GitHub api request without token (#326)
Browse files Browse the repository at this point in the history
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.
Backport from main, see #325 (comment)
  • Loading branch information
vinokurig authored Jul 13, 2022
1 parent 243c1e6 commit 7ef4966
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ protected String formatAuthorization(String token) {
protected boolean isPublicRepository(GithubUrl remoteFactoryUrl) {
try {
urlFetcher.fetch(
GithubApiClient.GITHUB_API_SERVER
+ "/repos/"
remoteFactoryUrl.getHostName()
+ '/'
+ remoteFactoryUrl.getUsername()
+ "/"
+ '/'
+ remoteFactoryUrl.getRepository());
return true;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -70,11 +71,10 @@ public void shouldPreserveAbsolutePaths() throws Exception {
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(
Expand Down

0 comments on commit 7ef4966

Please sign in to comment.