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.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
VersionChecker/README.md

14 KiB

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;
    }
}