From 8445ac812d9b90e2e7e2315a4f90bc3e24c78860 Mon Sep 17 00:00:00 2001 From: carm Date: Sat, 22 Jan 2022 15:16:34 +0800 Subject: [PATCH] [v1.3.0] [A] Add the method to fetch how many versions behind --- pom.xml | 2 +- .../githubreleases4j/GithubReleases4J.java | 57 +++++++++++++++++++ src/test/java/GithubVersionTest.java | 22 +++++++ 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/test/java/GithubVersionTest.java diff --git a/pom.xml b/pom.xml index 72a5cda..1ca62b8 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ cc.carm.lib githubreleases4j - 1.2.2 + 1.3.0 GithubReleases4J Github Releases for Java diff --git a/src/main/java/cc/carm/lib/githubreleases4j/GithubReleases4J.java b/src/main/java/cc/carm/lib/githubreleases4j/GithubReleases4J.java index 34f28c6..53a4268 100644 --- a/src/main/java/cc/carm/lib/githubreleases4j/GithubReleases4J.java +++ b/src/main/java/cc/carm/lib/githubreleases4j/GithubReleases4J.java @@ -27,6 +27,7 @@ public class GithubReleases4J { // Should not be the instance. } + public static String GITHUB_URL = "https://github.com"; public static String GITHUB_API_URL = "https://api.github.com"; public static SimpleDateFormat GH_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); @@ -160,6 +161,62 @@ public class GithubReleases4J { return getLatestRelease(owner, repository, null); } + /** + * Get how many versions behind the current version's tag name. + * + * @param owner Repository's Owner + * @param repository Repository's Name + * @param token OAuth Access Token + *
Necessary when this repository is private. + * @param currentTagName Current Version's tag name. + * @return NULL if not fetch releases, -1 if no match tag name. + * @since 1.3.0 + */ + public static @Nullable Integer getVersionBehind(@NotNull String owner, @NotNull String repository, + @Nullable String token, @NotNull String currentTagName) { + List releases = GithubReleases4J.listReleases(owner, repository, token); + if (releases.isEmpty()) return null; // Could not fetch releases. + + int i = 0; + for (GithubRelease release : releases) { + if (release.getTagName().equalsIgnoreCase(currentTagName)) { + break; + } + i++; + } + + if (i == releases.size()) return -1; // No match tag name; + + return i; + } + + /** + * Get how many versions behind the current version's tag name. + * + * @param owner Repository's Owner + * @param repository Repository's Name + * @param currentTagName Current Version's tag name. + * @return NULL if not fetch releases, -1 if no match tag name. + * @since 1.3.0 + */ + public static @Nullable Integer getVersionBehind(@NotNull String owner, @NotNull String repository, + @NotNull String currentTagName) { + return getVersionBehind(owner, repository, null, currentTagName); + } + + public static @NotNull String getReleasesURL(@NotNull String owner, @NotNull String repository) { + return buildURL("%s/%s/%s/releases", GITHUB_URL, owner, repository); + } + + public static @NotNull String getLatestReleaseURL(@NotNull String owner, @NotNull String repository) { + return getReleasesURL(owner, repository) + "/latest"; + } + + public static @NotNull String getReleaseURLByTag(@NotNull String owner, @NotNull String repository, + @NotNull String releaseTagName) { + return getReleasesURL(owner, repository) + "/tag/" + releaseTagName; + } + private static String buildURL(@NotNull String url, Object... params) { return String.format(url, params); } diff --git a/src/test/java/GithubVersionTest.java b/src/test/java/GithubVersionTest.java new file mode 100644 index 0000000..5704311 --- /dev/null +++ b/src/test/java/GithubVersionTest.java @@ -0,0 +1,22 @@ +import cc.carm.lib.githubreleases4j.GithubReleases4J; +import org.junit.Test; + +public class GithubVersionTest { + + + @Test + public void test() { + + System.out.println(GithubReleases4J.getVersionBehind("CarmJos", "UltraDepository", "1.3.4")); + + System.out.println(GithubReleases4J.getVersionBehind("CarmJos", "UltraDepository", "v1.2.0")); + + System.out.println(GithubReleases4J.getVersionBehind("CarmJos", "UltraDepository", "TEST")); + + System.out.println(GithubReleases4J.getVersionBehind("CarmJos", "NULL", "TEST")); + + System.out.println("Download at " + GithubReleases4J.getReleaseURLByTag("CarmJos", "UltraDepository", "1.3.4")); + System.out.println("Download at " + GithubReleases4J.getLatestReleaseURL("CarmJos", "UltraDepository")); + + } +}