Removed the drafty Github api - we now use the GitHubAPI4Java :)

This commit is contained in:
Florian CUNY 2019-04-18 14:41:29 +02:00
parent a18ed9b999
commit c47a41f3fc
8 changed files with 12 additions and 505 deletions

11
pom.xml
View File

@ -52,6 +52,7 @@
<bstats.version>1.4</bstats.version> <bstats.version>1.4</bstats.version>
<vault.version>1.7</vault.version> <vault.version>1.7</vault.version>
<placeholder.version>2.9.2</placeholder.version> <placeholder.version>2.9.2</placeholder.version>
<githubapi.version>1.0</githubapi.version>
<!-- Revision variable removes warning about dynamic version --> <!-- Revision variable removes warning about dynamic version -->
<revision>${build.version}</revision> <revision>${build.version}</revision>
<!-- This allows to change between versions. --> <!-- This allows to change between versions. -->
@ -144,6 +145,12 @@
<version>${spigot.version}</version> <version>${spigot.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<!-- Some APIs from us :D -->
<dependency>
<groupId>world.bentobox</groupId>
<artifactId>GitHubAPI4Java</artifactId>
<version>${githubapi.version}</version>
</dependency>
<!-- Metrics --> <!-- Metrics -->
<dependency> <dependency>
<groupId>org.bstats</groupId> <groupId>org.bstats</groupId>
@ -293,6 +300,10 @@
<pattern>org.bstats</pattern> <pattern>org.bstats</pattern>
<shadedPattern>world.bentobox.bentobox.util.metrics</shadedPattern> <shadedPattern>world.bentobox.bentobox.util.metrics</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>world.bentobox.githubapi4java</pattern>
<shadedPattern>world.bentobox.bentobox.api.github</shadedPattern>
</relocation>
</relocations> </relocations>
<artifactSet> <artifactSet>
<excludes> <excludes>

View File

@ -1,39 +0,0 @@
package world.bentobox.bentobox.api.github;
import org.eclipse.jdt.annotation.NonNull;
/**
* Represents a Contributor on a GitHub repository.
* @author Poslovitch
* @since 1.3.0
*/
public class Contributor {
private final @NonNull String name;
private final @NonNull String profile;
private int commits;
public Contributor(@NonNull String name, int commits) {
this.name = name;
this.profile = "https://github.com/" + name;
this.commits = commits;
}
@NonNull
public String getName() {
return name;
}
@NonNull
public String getProfile() {
return profile;
}
public int getCommits() {
return commits;
}
public void setCommits(int commits) {
this.commits = commits;
}
}

View File

@ -1,126 +0,0 @@
package world.bentobox.bentobox.api.github;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.util.Util;
/**
* Handles connection to the GitHub API, retrieves data and handles the {@link Repository} data that emerges from it.
* @author Poslovitch
* @since 1.3.0
*/
public class GitHubConnector {
private @NonNull String repositoryName;
private Repository repository;
public GitHubConnector(@NonNull String repositoryName) {
this.repositoryName = repositoryName;
}
public void connect() {
JsonElement repositoryData;
JsonElement releasesData;
JsonElement contributorData;
// Get the data
try {
repositoryData = getData(null);
// TODO getting other data is pointless if we couldn't get the data from the repository
contributorData = getData("contributors");
releasesData = getData("releases");
} catch (IOException e) {
BentoBox.getInstance().logStacktrace(e);
// TODO do not override data instead
return;
}
// Parse the data
/* It must be done in a specific order:
1. repository
2. contributors
3. releases
*/
parseRepositoryData(repositoryData);
parseContributorsData(contributorData);
parseReleasesData(releasesData);
}
@NonNull
private JsonElement getData(@Nullable String suffix) throws IOException {
HttpURLConnection connection = new GitHubURL(getRepositoryName(), suffix).openConnection();
String data = new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining("\n"));
return new JsonParser().parse(data);
}
private void parseRepositoryData(@NonNull JsonElement jsonElement) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
Repository.Builder builder = new Repository.Builder(repositoryName.split("/")[0], repositoryName.split("/")[1])
.stars(jsonObject.get("stargazers_count").getAsInt())
.forks(jsonObject.get("forks_count").getAsInt())
.openIssues(jsonObject.get("open_issues_count").getAsInt())
.latestCommit(Util.parseGitHubDate(jsonObject.get("pushed_at").getAsString()));
this.repository = builder.build();
}
private void parseContributorsData(@NonNull JsonElement jsonElement) {
for (JsonElement contributorElement : jsonElement.getAsJsonArray()) {
JsonObject contributor = contributorElement.getAsJsonObject();
this.repository.getContributors().add(new Contributor(contributor.get("login").getAsString(), contributor.get("contributions").getAsInt()));
}
}
private void parseReleasesData(@NonNull JsonElement jsonElement) {
for (JsonElement releaseElement : jsonElement.getAsJsonArray()) {
JsonObject release = releaseElement.getAsJsonObject();
String tag = release.get("tag_name").getAsString();
String url = repository.getUrl() + "/releases/tag/" + tag;
Release.Builder builder = new Release.Builder(release.get("name").getAsString(), tag, url)
.preRelease(release.get("prerelease").getAsBoolean())
.publishedAt(Util.parseGitHubDate( release.get("published_at").getAsString()));
// Run through the releases assets and try to find the correct one
for (JsonElement assetElement : release.get("assets").getAsJsonArray()) {
JsonObject asset = assetElement.getAsJsonObject();
String assetName = asset.get("name").getAsString();
if (assetName.endsWith(".jar") && !assetName.contains("javadoc") && !assetName.contains("sources")) {
// We found our asset!
builder.downloadUrl(asset.get("browser_download_url").getAsString())
.downloadSize(asset.get("size").getAsLong())
.downloadCount(asset.get("download_count").getAsInt());
break;
}
}
this.repository.getReleases().add(builder.build());
}
}
@NonNull
public String getRepositoryName() {
return repositoryName;
}
public Repository getRepository() {
return repository;
}
}

View File

@ -1,37 +0,0 @@
package world.bentobox.bentobox.api.github;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
/**
* Somehow wraps {@link URL} and {@link java.net.URLConnection} to avoid boilerplate code when accessing to the GitHub API.
* @author Poslovitch
* @since 1.3.0
*/
public class GitHubURL {
private final @NonNull URL url;
public GitHubURL(@NonNull String repository, @Nullable String suffix) throws MalformedURLException {
suffix = (suffix != null && !suffix.isEmpty()) ? "/" + suffix : "";
this.url = new URL("https://api.github.com/repos/" + repository + suffix);
}
@NonNull
public URL toURL() {
return url;
}
public HttpURLConnection openConnection() throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(2500);
connection.addRequestProperty("User-Agent", "BentoBox GitHubLink (@BentoBoxWorld)");
connection.setDoOutput(true);
return connection;
}
}

View File

@ -1,130 +0,0 @@
package world.bentobox.bentobox.api.github;
import java.util.Date;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
/**
* Represents a release on a Github repository.
* See https://api.github.com/repos/BentoBoxWorld/BentoBox/releases.
* @author Poslovitch
* @since 1.3.0
*/
public class Release {
private final @NonNull String name;
private final @NonNull String tag;
private final @NonNull String url;
private final boolean preRelease;
private final @Nullable Date publishedAt;
/* Release asset related fields */
private final @Nullable String downloadUrl;
private final long downloadSize;
private int downloadCount;
private Release(@NonNull Builder builder) {
this.name = builder.name;
this.tag = builder.tag;
this.url = builder.url;
this.preRelease = builder.preRelease;
this.publishedAt = builder.publishedAt;
this.downloadUrl = builder.downloadUrl;
this.downloadSize = builder.downloadSize;
this.downloadCount = builder.downloadCount;
}
@NonNull
public String getName() {
return name;
}
@NonNull
public String getTag() {
return tag;
}
@NonNull
public String getUrl() {
return url;
}
public boolean isPreRelease() {
return preRelease;
}
@Nullable
public Date getPublishedAt() {
return publishedAt;
}
@Nullable
public String getDownloadUrl() {
return downloadUrl;
}
public long getDownloadSize() {
return downloadSize;
}
public int getDownloadCount() {
return downloadCount;
}
public void setDownloadCount(int downloadCount) {
this.downloadCount = downloadCount;
}
public static class Builder {
private final @NonNull String name;
private final @NonNull String tag;
private final @NonNull String url;
private boolean preRelease;
private @Nullable Date publishedAt;
private @Nullable String downloadUrl;
private long downloadSize;
private int downloadCount;
public Builder(@NonNull String name, @NonNull String tag, @NonNull String url) {
this.name = name;
this.tag = tag;
this.url = url;
this.preRelease = false;
this.downloadSize = 0L;
this.downloadCount = 0;
}
public Builder preRelease(boolean preRelease) {
this.preRelease = preRelease;
return this;
}
public Builder publishedAt(@Nullable Date publishedAt) {
this.publishedAt = publishedAt;
return this;
}
public Builder downloadUrl(@Nullable String downloadUrl) {
this.downloadUrl = downloadUrl;
return this;
}
public Builder downloadSize(long downloadSize) {
this.downloadSize = downloadSize;
return this;
}
public Builder downloadCount(int downloadCount) {
this.downloadCount = downloadCount;
return this;
}
public Release build() {
return new Release(this);
}
}
}

View File

@ -1,152 +0,0 @@
package world.bentobox.bentobox.api.github;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
/**
* Represents a GitHub repository.
* @author Poslovitch
* @since 1.3.0
*/
public class Repository {
private final @NonNull String owner;
private final @NonNull String name;
private final @NonNull List<Contributor> contributors;
private final @NonNull List<Release> releases;
private int stars;
private int forks;
private int openIssues;
private @Nullable Date latestCommit;
private Repository(@NonNull Builder builder) {
this.owner = builder.owner;
this.name = builder.name;
this.contributors = builder.contributors;
this.releases = builder.releases;
this.stars = builder.stars;
this.forks = builder.forks;
this.openIssues = builder.openIssues;
this.latestCommit = builder.latestCommit;
}
@NonNull
public String getOwner() {
return owner;
}
@NonNull
public String getName() {
return name;
}
@NonNull
public List<Contributor> getContributors() {
return contributors;
}
@NonNull
public List<Release> getReleases() {
return releases;
}
public int getStars() {
return stars;
}
public void setStars(int stars) {
this.stars = stars;
}
public int getForks() {
return forks;
}
public void setForks(int forks) {
this.forks = forks;
}
public int getOpenIssues() {
return openIssues;
}
public void setOpenIssues(int openIssues) {
this.openIssues = openIssues;
}
@NonNull
public String getUrl() {
return "https://github.com/" + getOwner() + "/" + getName();
}
@Nullable
public Date getLatestCommit() {
return latestCommit;
}
public void setLatestCommit(@Nullable Date latestCommit) {
this.latestCommit = latestCommit;
}
public static class Builder {
private final @NonNull String owner;
private final @NonNull String name;
private final @NonNull List<Contributor> contributors;
private final @NonNull List<Release> releases;
private int stars;
private int forks;
private int openIssues;
private @Nullable Date latestCommit;
public Builder(@NonNull String owner, @NonNull String name) {
this.owner = owner;
this.name = name;
this.contributors = new LinkedList<>();
this.releases = new LinkedList<>();
}
public Builder contributors(@NonNull Contributor... contributors) {
this.contributors.addAll(Arrays.asList(contributors));
return this;
}
public Builder releases(@NonNull Release... releases) {
this.releases.addAll(Arrays.asList(releases));
return this;
}
public Builder stars(int stars) {
this.stars = stars;
return this;
}
public Builder forks(int forks) {
this.forks = forks;
return this;
}
public Builder openIssues(int openIssues) {
this.openIssues = openIssues;
return this;
}
public Builder latestCommit(@Nullable Date latestCommit) {
this.latestCommit = latestCommit;
return this;
}
public Repository build() {
return new Repository(this);
}
}
}

View File

@ -1,5 +0,0 @@
/**
* Contains objects and classes to get, retrieve and store data from GitHub.
* @since 1.3.0
*/
package world.bentobox.bentobox.api.github;

View File

@ -1,15 +1,7 @@
package world.bentobox.bentobox.managers; package world.bentobox.bentobox.managers;
import java.util.LinkedHashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.Addon;
import world.bentobox.bentobox.api.github.GitHubConnector;
/** /**
* Handles web-related stuff. * Handles web-related stuff.
@ -20,13 +12,10 @@ import world.bentobox.bentobox.api.github.GitHubConnector;
public class WebManager { public class WebManager {
private @NonNull BentoBox plugin; private @NonNull BentoBox plugin;
private @Nullable GitHubConnector bentoBoxGitHubConnector;
private @NonNull Map<@NonNull Addon, @NonNull GitHubConnector> gitHubConnectors = new LinkedHashMap<>();
public WebManager(@NonNull BentoBox plugin) { public WebManager(@NonNull BentoBox plugin) {
this.plugin = plugin; this.plugin = plugin;
if (plugin.getSettings().isGithubDownloadData()) { if (plugin.getSettings().isGithubDownloadData()) {
this.bentoBoxGitHubConnector = new GitHubConnector("BentoBoxWorld/BentoBox");
setupAddonsGitHubConnectors(); setupAddonsGitHubConnectors();
} }
} }
@ -36,17 +25,13 @@ public class WebManager {
*/ */
private void setupAddonsGitHubConnectors() { private void setupAddonsGitHubConnectors() {
plugin.getAddonsManager().getEnabledAddons().stream() plugin.getAddonsManager().getEnabledAddons().stream()
.filter(addon -> !addon.getDescription().getRepository().isEmpty()) .filter(addon -> !addon.getDescription().getRepository().isEmpty());
.forEach(addon -> gitHubConnectors.put(addon, new GitHubConnector(addon.getDescription().getRepository())));
} }
/** /**
* Connects all the {@link GitHubConnector} to GitHub to retrieve data.
*/ */
public void requestGitHubData() { public void requestGitHubData() {
if (plugin.getSettings().isGithubDownloadData()) { if (plugin.getSettings().isGithubDownloadData()) {
Bukkit.getScheduler().runTask(plugin, () -> bentoBoxGitHubConnector.connect());
gitHubConnectors.values().forEach(gitHubConnector -> Bukkit.getScheduler().runTask(plugin, gitHubConnector::connect));
} }
} }
} }