diff --git a/src/main/java/io/gitlab/jfronny/resclone/fetchers/GitHubFetcher.java b/src/main/java/io/gitlab/jfronny/resclone/fetchers/GitHubFetcher.java index db409ff..48308cf 100644 --- a/src/main/java/io/gitlab/jfronny/resclone/fetchers/GitHubFetcher.java +++ b/src/main/java/io/gitlab/jfronny/resclone/fetchers/GitHubFetcher.java @@ -6,6 +6,8 @@ import io.gitlab.jfronny.resclone.Resclone; import io.gitlab.jfronny.resclone.util.UrlUtils; import org.jetbrains.annotations.Nullable; +import java.io.IOException; + public class GitHubFetcher extends PackFetcher { @Override @@ -23,7 +25,7 @@ public class GitHubFetcher extends PackFetcher { /* "user/repo" - Gets from latest commit of main/master branch. */ else if (parts.length == 2) { - return getFromBranch(parts[0], parts[1], null); + return getFromBranch(parts[0] + "/" + parts[1], null); } /* "user/repo/branch/branchName" - Gets from latest commit of specified branch. */ @@ -31,7 +33,7 @@ public class GitHubFetcher extends PackFetcher { if (parts.length < 4) throw new Exception("Missing branch name in source definition."); else { Resclone.LOGGER.info("Getting from " + parts[3] + " branch."); - return getFromBranch(parts[0], parts[1], parts[3]); + return getFromBranch(parts[0] + "/" + parts[1], parts[3]); } } @@ -70,21 +72,17 @@ public class GitHubFetcher extends PackFetcher { return null; } - private String getFromBranch(String user, String repo, @Nullable String branch) { - boolean main = branch == null; - String url; - - if (main) { - url = "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/heads/main"; - if (!UrlUtils.urlValid(url)) url = "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/heads/master"; - Resclone.LOGGER.info("Getting from main branch."); + private String getFromBranch(String repo, @Nullable String branch) { + if (branch == null) { + try { + branch = UrlUtils.readJsonFromURL("https://api.github.com/repos/" + repo, JsonObject.class).get("default_branch").getAsString(); + } catch (IOException e) { + Resclone.LOGGER.error("Failed to fetch branch for " + repo + ". Choosing \"main\"", e); + branch = "main"; + } } - else { - url = "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/heads/" + branch; - Resclone.LOGGER.info("Getting from " + branch + " branch."); - } - - return url; + Resclone.LOGGER.info("Getting " + repo + " from " + branch + " branch."); + return "https://codeload.github.com/" + repo + "/legacy.zip/refs/heads/" + branch; } private String getFromTag(String user, String repo, String tag) {