Jump to content

GitHub Version Checker


Recommended Posts

Posted (edited)

As title states, this code checks your script version and compares it to your GitHub version. This requires you to have a latest release on GitHub and the versioning of the release will have to be similar to v1.0 or v0.12. Use only numerical characters and a . to add spaces. The script consists of two classes one being VersionChecker which does the main work, and the other being InfoCache which stores the GitHub Username, Project Name, and the current version of the local script. Once you call the boolean method needsUpdated() and fill in the parameters once, you can than call needsUpdated() again without the parameters. The script is very simple, feel free to edit and change it to however you would like. Hope this helps someone out.

Spoiler

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class VersionChecker {

    private static boolean hasRunInitialCheck = false;

    private static float currentVersion = 0f;
    private static float gitHubVersion = 0f;

    private static String url = "TempUrl";

    private static InfoCache infoCache;

    public static boolean needsUpdated(String gitHubUserName, String gitHubProjectName, double thisScriptVersion) {
        if (!hasRunInitialCheck) {
            infoCache = new InfoCache(gitHubUserName, gitHubProjectName, thisScriptVersion);
            getVersions(infoCache);
        }
        return gitHubVersion > currentVersion;
    }

    public static boolean needsUpdated() {
        if (!hasRunInitialCheck) {
            throw new NullPointerException("Specify Parameters First. The Parameters will than be cached!");
        }
        return gitHubVersion > currentVersion;
    }

    public static float getCurrentVersion() {
        return currentVersion;
    }

    public static float getGitHubVersion() {
        return gitHubVersion;
    }

    private static void getVersions(InfoCache infoCache) {
        setGitHubLink(infoCache.getGitHubUserName(), infoCache.getGitHubProjectName());
        setGitHubVersion(returnGitHubVersion());
        setCurrentVersion("" + infoCache.getThisScriptVersion());
        hasRunInitialCheck = true;
    }

    private static void setGitHubLink(String gitHubUserName, String gitHubProjectName) {
        url = String.format("https://api.github.com/repos/%s/%s/releases/latest", gitHubUserName, gitHubProjectName);
    }

    private static void setCurrentVersion(String version) {
        try{
            currentVersion = Float.parseFloat(version);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void setGitHubVersion(String version) {
        try {
            gitHubVersion = Float.parseFloat(version);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    private static String returnGitHubVersion() {
        try {
            URL currentUrl = new URL(url);
            HttpsURLConnection connection = (HttpsURLConnection) currentUrl.openConnection();
            InputStream inputStream = connection.getInputStream();
            InputStreamReader streamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(streamReader);
            StringBuilder stringBuilder = new StringBuilder();

            String line;

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

            if (stringBuilder.toString().contains("\"tag_name\":")) {
                int startPoint = stringBuilder.toString().indexOf("\"tag_name\":");
                int endPoint = stringBuilder.toString().indexOf(",", startPoint);
                return stringBuilder.toString().substring(startPoint + 13, endPoint - 1);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "0";
    }
}

 

 

Spoiler

public class InfoCache {
    private String gitHubUserName;
    private String gitHubProjectName;
    private double thisScriptVersion;

    public InfoCache(String gitHubUserName, String gitHubProjectName, double thisScriptVersion){
        this.gitHubUserName = gitHubUserName;
        this.gitHubProjectName = gitHubProjectName;
        this.thisScriptVersion = thisScriptVersion;
    }

    public String getGitHubUserName(){
        return gitHubUserName;
    }

    public String getGitHubProjectName() {
        return gitHubProjectName;
    }

    public double getThisScriptVersion() {
        return thisScriptVersion;
    }

    @Override
    public String toString() {
        return String.format("UserName: [%s] :: ProjectName [%s] :: ThisScriptVersion: [%.2f]", this.gitHubUserName, this.gitHubProjectName, this.thisScriptVersion);
    }
}

 

 

Edited by BravoTaco
  • Like 3
Posted (edited)
19 hours ago, Medusaa said:

Awesome seeing "new" people contribute to the community :) 

Been wanting to post more stuff but, as i'm not that good with programming yet, I don't want to give out my bad code practices 😂

14 hours ago, Proton said:

Agreed, thanks BravoTaco <3

No problem. Hope it helps you and or someone else out.

Edited by BravoTaco

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...