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

93 lines
3.6 KiB
Java
Raw Normal View History

2021-04-03 03:54:24 +02:00
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;
2021-04-03 06:34:52 +02:00
import org.jetbrains.annotations.Nullable;
2021-04-03 03:54:24 +02:00
2021-05-11 13:16:37 +02:00
import java.io.IOException;
2021-04-03 03:54:24 +02:00
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) {
2021-05-11 13:16:37 +02:00
return getFromBranch(parts[0] + "/" + parts[1], null);
2021-04-03 03:54:24 +02:00
}
/* "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.");
2021-05-11 13:16:37 +02:00
return getFromBranch(parts[0] + "/" + parts[1], parts[3]);
2021-04-03 03:54:24 +02:00
}
}
/* "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;
}
2021-05-11 13:16:37 +02:00
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";
}
2021-04-03 06:34:52 +02:00
}
2021-05-11 13:16:37 +02:00
Resclone.LOGGER.info("Getting " + repo + " from " + branch + " branch.");
return "https://codeload.github.com/" + repo + "/legacy.zip/refs/heads/" + branch;
2021-04-03 03:54:24 +02:00
}
private String getFromTag(String user, String repo, String tag) {
2021-04-03 06:34:52 +02:00
Resclone.LOGGER.info("Getting from tag " + tag + ".");
return "https://codeload.github.com/" + user + "/" + repo + "/legacy.zip/refs/tags/" + tag;
2021-04-03 03:54:24 +02:00
}
}