|
| 1 | +package org.hjug.git; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import org.eclipse.jgit.api.Git; |
| 11 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.io.TempDir; |
| 16 | + |
| 17 | +class GitLogReaderGetRepoUrlTest { |
| 18 | + |
| 19 | + @TempDir |
| 20 | + Path tempDir; |
| 21 | + |
| 22 | + private Git git; |
| 23 | + private File projectBaseDir; |
| 24 | + |
| 25 | + @BeforeEach |
| 26 | + void setUp() throws GitAPIException, IOException { |
| 27 | + projectBaseDir = tempDir.toFile(); |
| 28 | + git = Git.init().setDirectory(projectBaseDir).call(); |
| 29 | + Files.write(tempDir.resolve("test.txt"), "test content".getBytes()); |
| 30 | + git.add().addFilepattern(".").call(); |
| 31 | + git.commit().setMessage("Initial commit").call(); |
| 32 | + } |
| 33 | + |
| 34 | + @AfterEach |
| 35 | + void tearDown() { |
| 36 | + git.close(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testGetRepoUrlWithGitHubSshOrigin() throws Exception { |
| 41 | + git.remoteAdd() |
| 42 | + .setName("origin") |
| 43 | + .setUri(new org.eclipse.jgit.transport.URIish("git@github.com:user/repo.git")) |
| 44 | + .call(); |
| 45 | + |
| 46 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 47 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 48 | + String commitHash = git.log().call().iterator().next().getName(); |
| 49 | + assertTrue(repoUrl.startsWith("https://github.com/user/repo/blob/" + commitHash + "/")); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void testGetRepoUrlWithGitLabSshOrigin() throws Exception { |
| 55 | + git.remoteAdd() |
| 56 | + .setName("origin") |
| 57 | + .setUri(new org.eclipse.jgit.transport.URIish("git@gitlab.com:user/repo.git")) |
| 58 | + .call(); |
| 59 | + |
| 60 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 61 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 62 | + String commitHash = git.log().call().iterator().next().getName(); |
| 63 | + assertTrue(repoUrl.startsWith("https://gitlab.com/user/repo/-/blob/" + commitHash + "/")); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testGetRepoUrlWithBitBucketSshOrigin() throws Exception { |
| 69 | + git.remoteAdd() |
| 70 | + .setName("origin") |
| 71 | + .setUri(new org.eclipse.jgit.transport.URIish("git@bitbucket.org:user/repo.git")) |
| 72 | + .call(); |
| 73 | + |
| 74 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 75 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 76 | + String commitHash = git.log().call().iterator().next().getName(); |
| 77 | + assertTrue(repoUrl.startsWith("https://bitbucket.org/user/repo/src/" + commitHash + "/")); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testGetRepoUrlWithHttpsOrigin() throws Exception { |
| 83 | + git.remoteAdd() |
| 84 | + .setName("origin") |
| 85 | + .setUri(new org.eclipse.jgit.transport.URIish("https://github.com/user/repo.git")) |
| 86 | + .call(); |
| 87 | + |
| 88 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 89 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 90 | + String commitHash = git.log().call().iterator().next().getName(); |
| 91 | + assertTrue(repoUrl.startsWith("https://github.com/user/repo/blob/" + commitHash + "/")); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void testGetRepoUrlWithNoOrigin() throws Exception { |
| 97 | + try (GitLogReader gitLogReader = new GitLogReader(projectBaseDir)) { |
| 98 | + String repoUrl = gitLogReader.getRepoUrl(); |
| 99 | + assertEquals("", repoUrl); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments