diff --git a/github-vfs/src/main/java/org/ndx/aadarchi/vfs/github/GitHubFileProvider.java b/github-vfs/src/main/java/org/ndx/aadarchi/vfs/github/GitHubFileProvider.java index 64989f19..4b143340 100644 --- a/github-vfs/src/main/java/org/ndx/aadarchi/vfs/github/GitHubFileProvider.java +++ b/github-vfs/src/main/java/org/ndx/aadarchi/vfs/github/GitHubFileProvider.java @@ -65,7 +65,18 @@ private Optional getAuthenticationData(FileSystemOptions return Optional.ofNullable(userAuthenticationData); } + /** + * Convert the relative project url into a valid commons-vfs url (with the GitHub prefix) + * @param project user/project identifier. If absolute path is given as parameter, it is replaced. + * @return + */ public static String urlFor(String project) { + if(project.contains(".git")) { + project = project.replace(".git", ""); + } + if(project.startsWith("https://github.com")) { + return project.replace("https://", "github://"); + } return String.format("github://github.com/%s", project); } } diff --git a/github-vfs/src/test/java/org/ndx/aadarchi/vfs/github/TestFor345_Fail_When_Using_Repository_Path.java b/github-vfs/src/test/java/org/ndx/aadarchi/vfs/github/TestFor345_Fail_When_Using_Repository_Path.java new file mode 100644 index 00000000..2cee5473 --- /dev/null +++ b/github-vfs/src/test/java/org/ndx/aadarchi/vfs/github/TestFor345_Fail_When_Using_Repository_Path.java @@ -0,0 +1,32 @@ +package org.ndx.aadarchi.vfs.github; + +import java.io.IOException; +import java.nio.charset.Charset; + +import org.apache.commons.vfs2.FileObject; +import org.apache.commons.vfs2.FileSystemException; +import org.apache.commons.vfs2.FileSystemOptions; +import org.apache.commons.vfs2.VFS; +import org.apache.commons.vfs2.auth.StaticUserAuthenticator; +import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +class TestFor345_Fail_When_Using_Repository_Path { + private static FileSystemOptions authenticationOptions; + + @BeforeAll public static void putGitHubCredentialsInAuth() { + String token = System.getProperty("aadarchi.github.token"); + StaticUserAuthenticator auth = new StaticUserAuthenticator("github.com", + null, token); + authenticationOptions = new FileSystemOptions(); + DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(authenticationOptions, auth); + } + + @Test + void can_have_file_object_when_repo_path_is_given() throws FileSystemException { + FileObject rootFile = VFS.getManager().resolveFile(GitHubFileProvider.urlFor("https://github.com/Riduidel/aadarchi.git"), authenticationOptions); + Assertions.assertThat((Object) rootFile).isNotNull(); + } +}