1
mirror of https://github.com/CarmJos/GithubReleases4J.git synced 2024-09-19 13:45:45 +00:00

[v1.3.0] [A] Add the method to fetch how many versions behind

This commit is contained in:
Carm Jos 2022-01-22 15:16:34 +08:00
parent c53ca18e96
commit 8445ac812d
3 changed files with 80 additions and 1 deletions

View File

@ -14,7 +14,7 @@
<groupId>cc.carm.lib</groupId> <groupId>cc.carm.lib</groupId>
<artifactId>githubreleases4j</artifactId> <artifactId>githubreleases4j</artifactId>
<version>1.2.2</version> <version>1.3.0</version>
<name>GithubReleases4J</name> <name>GithubReleases4J</name>
<description>Github Releases for Java</description> <description>Github Releases for Java</description>

View File

@ -27,6 +27,7 @@ public class GithubReleases4J {
// Should not be the instance. // 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 String GITHUB_API_URL = "https://api.github.com";
public static SimpleDateFormat GH_TIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 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); 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
* <br> 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<GithubRelease> 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) { private static String buildURL(@NotNull String url, Object... params) {
return String.format(url, params); return String.format(url, params);
} }

View File

@ -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"));
}
}