Improve GitHub fetcher

This commit is contained in:
JFronny 2021-05-11 13:16:37 +02:00
parent c186821942
commit 887d1d2233
No known key found for this signature in database
GPG Key ID: BEC5ACBBD4EE17E5
1 changed files with 14 additions and 16 deletions

View File

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