Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow to get files from git repository if file name starts with dot #351

Merged
merged 1 commit into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ public void shouldResolveRelativePaths(String relative, String expected, String
public static Object[][] relativePathsProvider() {
return new Object[][] {
{"./file.txt", "https://foo.bar/rest/api/1.0/projects/proj/repos/repo/raw/file.txt", null},
{"../file.txt", "https://foo.bar/rest/api/1.0/projects/proj/repos/repo/raw/file.txt", null},
{"/file.txt", "https://foo.bar/rest/api/1.0/projects/proj/repos/repo/raw/file.txt", null},
{"file.txt", "https://foo.bar/rest/api/1.0/projects/proj/repos/repo/raw/file.txt", null},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ protected boolean isPublicRepository(T remoteFactoryUrl) {
return false;
}

private String formatUrl(String fileURL) throws DevfileException {
protected String formatUrl(String fileURL) throws DevfileException {
String requestURL;
try {
if (new URI(fileURL).isAbsolute()) {
requestURL = fileURL;
} else {
// since files retrieved via REST, we cannot use path symbols like . ./ so cut them off
requestURL = remoteFactoryUrl.rawFileLocation(fileURL.replaceAll("^[/.]+", ""));
// since files retrieved via REST, we cannot use path like '.' or one that starts with './'
// so cut them off
requestURL =
remoteFactoryUrl.rawFileLocation(fileURL.replaceAll("^(?:\\.?\\/)|(?:\\.$)", ""));
}
} catch (URISyntaxException e) {
throw new DevfileException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
*/
package org.eclipse.che.api.factory.server.scm;

import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;

import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl;
import org.eclipse.che.api.workspace.server.devfile.URLFetcher;
Expand All @@ -34,20 +36,20 @@ public class AuthorizingFactoryParameterResolverTest {
@Mock private GitCredentialManager gitCredentialManager;
@Mock private PersonalAccessToken personalAccessToken;

private AuthorizingFileContentProvider provider;
private AuthorizingFileContentProvider<?> provider;

@BeforeMethod
public void setUp() throws Exception {
provider =
new AuthorizingFileContentProvider(
new AuthorizingFileContentProvider<>(
remoteFactoryUrl, urlFetcher, personalAccessTokenManager, gitCredentialManager);
when(remoteFactoryUrl.getHostName()).thenReturn("hostName");
when(remoteFactoryUrl.rawFileLocation(anyString())).thenReturn("rawFileLocation");
when(remoteFactoryUrl.rawFileLocation(anyString())).thenAnswer(returnsFirstArg());
}

@Test
public void shouldFetchContentWithAuthentication() throws Exception {
// given
when(remoteFactoryUrl.getHostName()).thenReturn("hostName");
when(urlFetcher.fetch(anyString(), anyString())).thenReturn("content");
when(personalAccessTokenManager.fetchAndSave(any(Subject.class), anyString()))
.thenReturn(personalAccessToken);
Expand All @@ -62,6 +64,7 @@ public void shouldFetchContentWithAuthentication() throws Exception {
@Test
public void shouldFetchContentWithoutAuthentication() throws Exception {
// given
when(remoteFactoryUrl.getHostName()).thenReturn("hostName");
when(urlFetcher.fetch(anyString())).thenReturn("content");

// when
Expand All @@ -70,4 +73,14 @@ public void shouldFetchContentWithoutAuthentication() throws Exception {
// then
verify(personalAccessTokenManager, never()).fetchAndSave(any(Subject.class), anyString());
}

@Test
public void shouldOmitDotInTheResourceName() throws Exception {
assertEquals(provider.formatUrl("./pom.xml"), "pom.xml");
}

@Test
public void shouldKeepResourceNameUnchanged() throws Exception {
assertEquals(provider.formatUrl(".gitconfig"), ".gitconfig");
}
}