Skip to content

Commit

Permalink
Merge pull request #14 from IndianBartonka/master
Browse files Browse the repository at this point in the history
Added more methods
  • Loading branch information
neziw authored Oct 19, 2024
2 parents 591f21f + 54fb6e0 commit c13a080
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 14 deletions.
32 changes: 22 additions & 10 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
# ReleaseChecker

Easy and lightweight GitHub release parser written in Java using Gson and HTTP Requests.
Easy and lightweight GitHub release parser written in Java using Gson and HTTPS Requests.

### Example Usage:
```java
// https://github.com/nats-io/nats.java
final ReleaseCheck releaseCheck = ReleaseCheckBuilder.builder()
.setRepositoryOwner("nats-io")
.setRepositoryName("nats.java")
.build();

.setRepositoryOwner("nats-io")
.setRepositoryName("nats.java")
.build();

System.out.println("Latest version: " + releaseCheck.getLatestRelease().tagName());
System.out.println("Is newer version available: " + releaseCheck.isNewerVersionAvailable("2.18.1"));
System.out.println("Is a newer version available: " + releaseCheck.isNewerVersionAvailable("2.20.1"));

//You can also get all available releases using 'ReleaseCheck#getReleaseDataList()'

final int behindCount = releaseCheck.getBehindCount("2.20.1");

if (behindCount == -1) {
System.out.println("There is no such tag.");
} else if (behindCount == 0) {
System.out.println("You are using the latest version.");
} else {
System.out.println("Your version is " + behindCount + " versions behind the latest.");
}
```

**Output** (since 29/06/2024):
**Output** (since 19/10/2024):
```
Latest version: 2.19.0
Is newer version available: true
Latest version: 2.20.2
Is a newer version available: true
Your version is 1 versions behind the latest.
```

### Adding dependency
Expand Down Expand Up @@ -56,4 +68,4 @@ implementation "ovh.neziw:ReleaseChecker:1.0.1"
---
Special thanks to [JetBrains](https://www.jetbrains.com/products/) company for providing development tools used to develop this project.

[<img src="https://user-images.githubusercontent.com/65517973/210912946-447a6b9a-2685-4796-9482-a44bffc727ce.png" alt="JetBrains" width="150">](https://www.jetbrains.com)
[<img src="https://user-images.githubusercontent.com/65517973/210912946-447a6b9a-2685-4796-9482-a44bffc727ce.png" alt="JetBrains" width="150">](https://www.jetbrains.com)
65 changes: 61 additions & 4 deletions src/main/java/ovh/neziw/checker/ReleaseCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
*/
package ovh.neziw.checker;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.net.ssl.HttpsURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import ovh.neziw.checker.release.ReleaseData;
import ovh.neziw.checker.repository.RepositoryData;
import ovh.neziw.checker.util.GsonUtil;
Expand All @@ -41,13 +43,15 @@ public class ReleaseCheck {
private final String repositoryOwner;
private final String repositoryName;
private final String githubToken;
private final List<ReleaseData> releaseDataList;
private RepositoryData repositoryData;
private ReleaseData releaseData;

protected ReleaseCheck(final ReleaseCheckBuilder releaseCheckBuilder) {
this.repositoryOwner = releaseCheckBuilder.getRepositoryOwner();
this.repositoryName = releaseCheckBuilder.getRepositoryName();
this.githubToken = releaseCheckBuilder.getGithubToken();
this.releaseDataList = new LinkedList<>();
this.repositoryData = null;
this.releaseData = null;
}
Expand Down Expand Up @@ -80,7 +84,7 @@ public RepositoryData getRepositoryData() throws IOException {
}
return this.repositoryData;
}

public ReleaseData getLatestRelease() throws IOException {
if (this.releaseData != null) {
return this.releaseData;
Expand Down Expand Up @@ -110,6 +114,48 @@ public ReleaseData getLatestRelease() throws IOException {
return this.releaseData;
}

public List<ReleaseData> getReleaseDataList() throws IOException {
if (!this.releaseDataList.isEmpty()) {
return this.releaseDataList;
}

final URL url = URI.create(BASE_URL + this.repositoryOwner + "/" + this.repositoryName + "/releases").toURL();
final HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.setRequestProperty("Accept", "application/json");

if (this.githubToken != null) {
httpsURLConnection.setRequestProperty("Authorization", "Bearer " + this.githubToken);
}

try (final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpsURLConnection.getInputStream()))) {
final StringBuilder stringBuilder = new StringBuilder();
String line;

while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}

this.releaseDataList.clear();
this.releaseDataList.addAll(this.parseReleaseDataList(stringBuilder.toString()));
} finally {
httpsURLConnection.disconnect();
}

return this.releaseDataList;
}

public int getBehindCount(final String tagName) throws IOException {
int counter = 0;

for (final ReleaseData data : this.getReleaseDataList()) {
if (data.tagName().equals(tagName)) return counter;
counter++;
}

return -1;
}

public boolean isNewerVersionAvailable(final String version) throws IOException {
final String[] parts = version.split("\\.");
final String[] latestParts = this.parseCleanVersionString(this.getLatestRelease().tagName()).split("\\.");
Expand All @@ -134,6 +180,17 @@ private ReleaseData parseReleaseData(final String responseBody) {
return GsonUtil.getGson().fromJson(responseBody, ReleaseData.class);
}


private List<ReleaseData> parseReleaseDataList(final String responseBody) {
final TypeToken<List<ReleaseData>> typeToken = new TypeToken<>() {
};

final List<ReleaseData> releases = GsonUtil.getGson().fromJson(responseBody, typeToken);
releases.sort(Comparator.comparing(ReleaseData::publishedAt).reversed());

return releases;
}

private String parseCleanVersionString(final String string) {
return string.replaceAll("[^\\d.]", "");
}
Expand Down

0 comments on commit c13a080

Please sign in to comment.