89 lines
3.3 KiB
Java
89 lines
3.3 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;
|
|
|
|
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) {
|
|
Resclone.LOGGER.info("Getting from main branch.");
|
|
return getFromMain(parts[0], parts[1]);
|
|
}
|
|
|
|
/* "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 {
|
|
Resclone.LOGGER.info("Getting from " + parts[3] + " tag.");
|
|
return getFromTag(parts[0], parts[1], parts[3]);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private String getFromMain(String user, String repo) {
|
|
String url = getFromBranch(user, repo, "main");
|
|
if (!UrlUtils.urlValid(url)) url = getFromBranch(user, repo, "master");
|
|
|
|
return url;
|
|
}
|
|
|
|
private String getFromBranch(String user, String repo, String branch) {
|
|
return "https://github.com/" + user + "/" + repo + "/archive/refs/heads/" + branch + ".zip";
|
|
}
|
|
|
|
private String getFromTag(String user, String repo, String tag) {
|
|
return "https://github.com/" + user + "/" + repo + "/archive/refs/tags/" + tag + ".zip";
|
|
}
|
|
|
|
} |