Resclone/src/main/java/io/gitlab/jfronny/resclone/fetchers/GitHubFetcher.java

95 lines
3.6 KiB
Java

package io.gitlab.jfronny.resclone.fetchers;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.gitlab.jfronny.resclone.Resclone;
import io.gitlab.jfronny.resclone.util.UrlUtils;
import org.jetbrains.annotations.Nullable;
public class GitHubFetcher extends PackFetcher {
@Override
public String getSourceTypeName() {
return "github";
}
@Override
String getDownloadUrl(String baseUrl) throws Exception {
String[] parts = baseUrl.split("/");
if (parts.length < 2) {
throw new Exception("Minimum source must contain \"user/repo\".");
}
/* "user/repo" - Gets from latest commit of main/master branch. */
else if (parts.length == 2) {
return getFromBranch(parts[0], parts[1], null);
}
/* "user/repo/branch/branchName" - Gets from latest commit of specified branch. */
else if (parts[2].equalsIgnoreCase("branch")) {
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]);
}
}
/* "user/repo/release" - Gets from the latest release. */
else if (parts[2].equalsIgnoreCase("release")) {
try {
JsonObject latestRelease = UrlUtils.readJsonFromURL("https://api.github.com/repos/" + parts[0] + "/" + parts[1] + "/releases/latest", JsonObject.class);
String res = null;
for (JsonElement element : latestRelease.get("assets").getAsJsonArray()) {
JsonObject o = element.getAsJsonObject();
if ("application/x-zip-compressed".equals(o.get("content_type").getAsString()) || o.get("name").getAsString().endsWith(".zip")) {
res = o.get("browser_download_url").getAsString();
break;
}
}
Resclone.LOGGER.info("Getting from latest release.");
if (res == null) return latestRelease.get("zipball_url").getAsString();
else return res;
} catch (Throwable e) {
throw new Exception("Failed to get github release asset", e);
}
}
/* "user/repo/tag/tagNum" - Gets from a specified tag. */
else if (parts[2].equalsIgnoreCase("tag")) {
if (parts.length < 4) throw new Exception("Missing tag number in source definition.");
else {
return getFromTag(parts[0], parts[1], parts[3]);
}
}
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.");
}
else {
url = "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/heads/" + branch;
Resclone.LOGGER.info("Getting from " + branch + " branch.");
}
return url;
}
private String getFromTag(String user, String repo, String tag) {
Resclone.LOGGER.info("Getting from tag " + tag + ".");
return "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/tags/" + tag;
}
}