Skip to content

Commit 8c5252f

Browse files
committed
Fixing getFileContentFromUrl
1 parent 014e39b commit 8c5252f

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/main/java/javiergs/tulip/GitHubHandler.java

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,34 @@ public GitHubHandler(String token) {
6666
//}
6767

6868
public String getFileContentFromUrl(String fileUrl) throws IOException {
69+
// If the URL is not in the expected /blob/ or /tree/ form, try to normalize it
70+
if (!fileUrl.contains("/blob/") && !fileUrl.contains("/tree/")) {
71+
// Expected shape right now: https://github.com/{owner}/{repo}/{path...}
72+
// We will assume branch "main" and insert /blob/main/ after repo
73+
// 1) strip trailing slash
74+
String normalized = fileUrl;
75+
if (normalized.endsWith("/")) {
76+
normalized = normalized.substring(0, normalized.length() - 1);
77+
}
78+
79+
// split into pieces
80+
// e.g. https://github.com/javiergs/ADASIM/library/... ->
81+
// [0]=https:, [1]=, [2]=github.com, [3]=javiergs, [4]=ADASIM, [5..]=path
82+
String[] parts = normalized.split("/", 6);
83+
if (parts.length < 5) {
84+
throw new IllegalArgumentException("Cannot normalize GitHub URL: " + fileUrl);
85+
}
86+
String owner = parts[3];
87+
String repo = parts[4];
88+
String path = (parts.length == 6) ? parts[5] : "";
89+
90+
// now just call the owner/repo/path version assuming branch "main"
91+
return getFileContent(owner, repo, path, "main");
92+
}
93+
94+
// original path for already-correct URLs
6995
URLHelper u = URLHelper.parseGitHubUrl(fileUrl);
7096
if (!u.isBlob) {
71-
// If the user gave just ".../repo/path/to/File.java" without /blob/<ref>/...
72-
// we can assume main and re-call with a normalized URL
73-
if ((u.ref == null || u.ref.isBlank()) && u.path != null && !u.path.isBlank()) {
74-
// re-try using owner/repo/path with the default branch "main"
75-
return getFileContent(u.owner, u.repo, u.path, "main");
76-
}
7797
throw new IllegalArgumentException("URL does not point to a file (/blob/...): " + fileUrl);
7898
}
7999
String path = (u.path == null) ? "" : u.path;

0 commit comments

Comments
 (0)