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 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) {