Skip to content

Commit cb673b7

Browse files
Merge pull request #192 from refactorfirst/#191-support-various-origins
Generate hyperlinks correctly for HTTPS and SSH origin URLs for multiple major providers
2 parents dbccf5f + 41d85da commit cb673b7

3 files changed

Lines changed: 131 additions & 3 deletions

File tree

change-proneness-ranker/src/main/java/org/hjug/git/GitLogReader.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ public String getCurrentCommitHash() throws IOException {
7070
return headCommit.getName();
7171
}
7272

73+
/**
74+
* Returns the repository's origin URL
75+
*
76+
* @return the origin URL as a String, or null if not configured
77+
*/
7378
/**
7479
* Returns the repository's origin URL
7580
*
@@ -79,6 +84,29 @@ public String getOriginUrl() {
7984
return gitRepository.getConfig().getString("remote", "origin", "url");
8085
}
8186

87+
public String getRepoUrl() throws IOException {
88+
String repoUrl;
89+
String originUrl = getOriginUrl();
90+
if (originUrl != null && originUrl.startsWith("git@")) {
91+
originUrl = originUrl.replace(":", "/").replace("git@", "https://");
92+
}
93+
94+
if (originUrl == null) {
95+
return "";
96+
}
97+
98+
repoUrl = originUrl.replace(".git", "");
99+
100+
if (repoUrl.contains("gitlab")) {
101+
repoUrl = repoUrl + "/-/blob/" + getCurrentCommitHash() + "/";
102+
} else if (repoUrl.contains("bitbucket")) {
103+
repoUrl = repoUrl + "/src/" + getCurrentCommitHash() + "/";
104+
} else {
105+
repoUrl = repoUrl + "/blob/" + getCurrentCommitHash() + "/";
106+
}
107+
return repoUrl;
108+
}
109+
82110
// log --follow implementation may be worth adopting in the future
83111
// https://github.com/spearce/jgit/blob/master/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/RevWalkTextBuiltin.java
84112

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

report/src/main/java/org/hjug/refactorfirst/report/SimpleHtmlReport.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,9 @@ public StringBuilder generateReport(
395395
}
396396

397397
static String getRepoUrl(String projectBaseDir) throws Exception {
398-
String repoUrl;
399398
try (GitLogReader glr = new GitLogReader(new File(projectBaseDir))) {
400-
repoUrl = glr.getOriginUrl().replace(".git", "") + "/blob/" + glr.getCurrentCommitHash() + "/";
399+
return glr.getRepoUrl();
401400
}
402-
return repoUrl;
403401
}
404402

405403
StringBuilder createMenu(

0 commit comments

Comments
 (0)