1
0
Fork 0
A simple version checker, using semantic-release formats like 1.2.3, 2.3.4 or 6.6.6, as well as 3.4.5-alpha.2 or 4.5.6-beta.3, for a given GitHub user and repository, or GitLab instance apiV4 project URL.
Go to file
Griefed 8506d9fc83
Merge branch 'main' of https://git.griefed.de/Griefed/VersionChecker
# Conflicts:
#	.gitlab-ci.yml
2023-07-29 12:37:34 +02:00
.github Update JamesIves/github-pages-deploy-action action to v4.4.2 2023-06-04 10:18:39 +00:00
.run breaking: Overhaul everything. Allow to check for pre- and regular releases. Throw exceptions at correct places. Improve tests with realistic example. 2022-02-02 21:48:12 +01:00
gradle/wrapper Update dependency gradle to v8.2 2023-07-04 10:21:39 +00:00
src fix: Remove check for specific release assets. 2023-04-05 08:53:11 +02:00
.gitignore breaking: Overhaul everything. Allow to check for pre- and regular releases. Throw exceptions at correct places. Improve tests with realistic example. 2022-02-02 21:48:12 +01:00
.gitlab-ci.yml Merge branch 'main' of https://git.griefed.de/Griefed/VersionChecker 2023-07-29 12:37:34 +02:00
.releaserc.yml docs: Improve documentation 2022-02-04 13:12:23 +01:00
build.gradle Update dependency com.fasterxml.jackson.core:jackson-databind to v2.15.2 2023-06-04 10:18:45 +00:00
CHANGELOG.md RELEASE: 1.1.9 2023-05-06 18:08:12 +00:00
gradlew Update dependency gradle to v8.2 2023-07-04 10:21:39 +00:00
gradlew.bat chore(deps): update dependency gradle to v7.6 2022-12-04 11:28:57 +00:00
LICENSE Add LICENSE 2022-01-15 16:10:45 +01:00
README.md docs(Examples): Provide an example for the usage of the new Update-object in the README and expand sourceTar and sourceTarBz2 methods descriptions on when they are available. 2022-04-17 12:44:46 +02:00
renovate.json ci: Enable RenovateBot dependency updates 2022-02-24 11:47:32 +01:00

Simple semantic version checker for GitHub and GitLab

Homepage Blog Fleet GitHub DockerHub Discord


Sources, GitHub, GitLab and Mirroring and all that good stuff

Repositories on GitHub are now for issues only. I've set up my own installation of GitLab and moved all my repositories over to Git.Griefed.de. Make sure to check there first for the latest code before opening an issue on GitHub.

For questions, you can always join my Discord server and talk to me there.

This repository is available at:

GitHub release (latest by date including pre-releases) GitHub

GitHub Repo stars GitHub forks GitHub contributors GitHub all releases

[[TOC]]

A simple version checker, using semantic-release formats like 1.2.3, 2.3.4 or 6.6.6, as well as 3.4.5-alpha.2 or 4.5.6-beta.3, for a given GitHub user and repository, or GitLab instance and project ID.

Made for use as an update-checker in ServerpackCreator, but you can do with it whatever you like.

Implementation

Maven

<dependency>
  <groupId>de.griefed</groupId>
  <artifactId>versionchecker</artifactId>
  <version>$VERSION</version>
</dependency>

Gradle

implementation 'de.griefed:versionchecker:$VERSION'

Versions

For available versions, see the sonatype repo

Examples

See UpdateCheckerTests

The simplest way to acquire update information is to call .check(...) of your GitHub- or GitHubChecker instances with your current version. This will return an instance of Update which contains details about the available update. It is wrapped in an Optional, so you can conveniently check via .isPresent() if an update is available.

Once an instance of Update has been retrieved, you can access information about it via any of the available methods.

Example:

GitHubChecker GITHUB = new GitHubChecker("Griefed/ServerPackCreator");

Optional<Update> gitHubUpdate = GITHUB.check("3.0.1",false);
if (gitHubUpdate.isPresent()) {
    
    // Gets the version of this update as a String.
    gitHubUpdate.get().version();
    
    // Check whether the update has a description
    if (gitHubUpdate.get().description().isPresent()) {
        
        // Get the description
        gitHubUpdate.get().description().get();
    }
    
    // Get the url to the release page of this update
    gitHubUpdate.get().url();
    
    // Get the date at which this update has been released.
    gitHubUpdate.get().releaseDate();
    
    // Check if the update has any assets attached to it
    if (gitHubUpdate.get().assets().isPresent()) {
        
        // Get the available assets of this update
        gitHubUpdate.get().assets().get();
    }
    
    // Get the available source-archives of this update
    gitHubUpdate.get().sources();
}

public class UpdateChecker {

    private static final Logger LOG = LogManager.getLogger(UpdateChecker.class);

    private GitHubChecker GITHUB;
    private GitLabChecker GITGRIEFED;
    private GitLabChecker GITLAB;

    public UpdateChecker() throws IOException {

        this.GITHUB = new GitHubChecker("Griefed/ServerPackCreator");
        this.GITLAB = new GitLabChecker("https://gitlab.com/api/v4/projects/32677538/releases");
        this.GITGRIEFED = new GitLabChecker("https://git.griefed.de/api/v4/projects/63/releases");
    }

    public UpdateChecker refresh() {
        try {
            this.GITHUB.refresh();
        } catch (Exception ex) {
            LOG.error("Error refreshing GitHub.", ex);
            this.GITHUB = null;
        }
        try {
            this.GITLAB.refresh();
        } catch (Exception ex) {
            LOG.error("Error refreshing GitLab.", ex);
            this.GITLAB = null;
        }
        try {
            this.GITGRIEFED.refresh();
        } catch (Exception ex) {
            LOG.error("Error refreshing GitGriefed.", ex);
            this.GITGRIEFED = null;
        }
        return this;
    }

    public String checkForUpdate() {
        String updater = "No updates available.";
        
        // Check GitHub for the most recent release.
        if (GITHUB != null) {
            
            // Check GitHub for new versions which are not pre-releases. Run with true to check pre-releases as well.
            updater = GITHUB.checkForUpdate("your current version here", false);
        }
        
        
        if (GITGRIEFED != null) {

            // After checking GitLab, and we did not get a version, check GitGriefed.
            // Check GitGriefed for new versions which are not pre-releases. Run with true to check pre-releases as well.
            // Only check if we did not already get a version from prior checks.
            if (!updater.contains(";") && GITGRIEFED.checkForUpdate("your current version here", false).contains(";")) {
                updater = GITGRIEFED.checkForUpdate(updater.split(";")[0], false);
                
                // Check GitGriefed for a newer version, with the version received from GitHub, if we received a new version from GitHub.
                // Don't check for pre-releases.
            } else if (updater.contains(";") && GITGRIEFED.checkForUpdate(updater.split(";")[0], false).contains(";")) {
                updater = GITGRIEFED.checkForUpdate(updater.split(";")[0], false);
            }
        }

        
        if (GITLAB != null) {

            // After checking GitGriefed, and we did not get a version, check GitLab.
            // Check GitLab for new versions which are not pre-releases. Run with true to check pre-releases as well.
            // Only check if we did not already get a version from prior checks.
            if (!updater.contains(";") && GITLAB.checkForUpdate("your current version here", false).contains(";")) {
                updater = GITLAB.checkForUpdate(updater.split(";")[0], false);
                
                // Check GitLab for a newer version, with the version we received from GitGriefed, if we received a new version from GitGriefed.
                // Don't check for pre-releases.
            } else if (updater.contains(";") && GITLAB.checkForUpdate(updater.split(";")[0], false).contains(";")) {
                updater = GITLAB.checkForUpdate(updater.split(";")[0], false);
            }
        }
        
        // Output can be either "No updates available." if...well...no updates are available.
        // or "2.1.1;https://github.com/Griefed/ServerPackCreator/releases/download/2.1.1/serverpackcreator-2.1.1.jar"
        // if you ran this for ServerPackCreator, with version 2.1.1, without checking for pre-releases. (at the time of me writing this)

        LOG.info("Update checks returned: " + updater);

        return updater;
    }
}