0

Below is the concerned code. Basically what the code is supposed to do is output the URL, name and version of each GitHub release defined by GetUpdateInfo.getInfo().

GetUpdateInfo.getInfo (NOTE Strings login, oauth and password omitted for security reasons.):


import java.util.List;

import org.kohsuke.github.*;

import org.apache.commons.lang3.ArrayUtils;

public class GetUpdateInfo {
    public static getInfo() {
         String version = "";
        String url = "";
        
        String[] urls = {};
        String[] names = {};
        String[] versions = {};
        String[] releases = {};
        
        GitHub github = GitHubBuilder.fromEnvironment(login, password, oauth).build();
        //Get the repo name from the organization
        GHOrganization gho = github.getOrganization("NuovoLauncher-Mods");
        PagedIterable<GHRepository> repos = gho.listRepositories();
        List<GHRepository> repos_list = repos.asList();
        for(int i=0; i < repos_list.size(); i++) {
            GHRepository repo_test = repos_list.get(i);
            GHRelease latest = repo_test.getLatestRelease();
            
            ArrayUtils.add(releases, latest.toString());
            ArrayUtils.add(names, latest.getName());
            ui.setName(names);
            ui.setRelease(releases);
            
            List<GHAsset> assets = latest.getAssets();
            for( int x = 0; x < assets.size(); x++ ) {
                GHAsset asset = assets.get(x);
                url = asset.getBrowserDownloadUrl();
                version = url.split("/")[7];
                
                System.out.format("URL: %s, Name: %s, Latest Release: %s. Version %s\n", url, latest.getName(), latest, version);
                
                ArrayUtils.add(urls, url);
                ArrayUtils.add(versions, version);
                ui.setURL(urls);
                ui.setVersion(versions);
            }
        }
        return ui;
    }
    
    
    public static void main(String[] args) throws Exception {   
        GetUpdateInfo.getInfo();
    }
}

DownloadUpdate.runner:

    public static void runner() throws Exception {
        String system_type = System.getProperty("os.name");
        File fpath = new File("");
        UpdateInfo ui = GetUpdateInfo.getInfo();
        for(int i = 0; i < ui.getName().length; i++) {
            System.out.format("URL: %s, Name %s, Version, %s", ui.getURL()[i], ui.getName()[i], ui.getVersion()[i]);
            System.out.format("Downloading %s-%s", ui.getName()[i], ui.getVersion()[i]);
            System.out.print("\n");

            if(system_type.equals("Linux")) {
                fpath = new File(System.getProperty("user.home") + "/.minecraft/mods/" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
            } else if(system_type.equals("Windows")) {
                fpath = new File(System.getProperty("user.home") + "/AppData/Roaming/.minecraft/mods" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
            } else {
                fpath = new File(System.getProperty("user.home") + "/.minecraft/mods/" + ui.getName()[i] + "-" + ui.getVersion()[i] + ".jar");
            }

            String url = ui.getURL()[i];
            FileUtils.copyURLToFile(new URL(url), fpath);
        }
    }
    public static void main(String[] args) throws Exception {
        System.out.println("DEBUG START");
        DownloadUpdate.runner();
    }
}

Looking at the code, I cannot see a reason why the code is not outputting like expected; I am getting zero output on console, simply the line stating that the code is being executed. No exceptions either.

EDIT: variable ui is not being returned properly. For example, ui.getName[0] throws an ArrayIndexOutOfBoundsException, due to the length being zero. Seeing this, I now understand why the for loop isn't behaving as expected. Is this a scope issue? What am I missing here?

  • 1
    Try output an empty newline or `flush` the output – AterLux May 02 '22 at 20:18
  • 2
    Q: I assume the "loop" you're referring to is the one in DownloadUpdate.main()? Q: Have you stepped through the code in the debugger and verified that "ui.getName().length" is > 0? Q: If "yes", have you also verified that it enters the loop, and executes your "System.out.println()" statements? ALSO: to compare strings in Java, use `String.equals()`, *NOT* "==". – paulsm4 May 02 '22 at 20:20
  • 2
    Unrelated to your question, but `system_type == "Linux"` is not the correct way to compare strings in Java; see [here](https://stackoverflow.com/q/513832/12299000). – kaya3 May 02 '22 at 20:20
  • @AterLux I tried your suggestions and neither of them worked. – Connor Tozer May 02 '22 at 20:22
  • @paulsm4 Yes I have verified that ui.getName().length > 0 – Connor Tozer May 02 '22 at 20:22
  • 2
    Have .... you ... stepped ... through ... the ... code ... *IN YOUR DEBUGGER*??? If not, why not? If "yes", did it execute the lines you expect? Do your variables have the values you expect? – paulsm4 May 02 '22 at 20:23
  • 1
    @paulsm4 I understand your question now, thank you for the clarification. After debugging and running the coverage script, it missed 291 instructions. So no, it didn't behave as I expected. – Connor Tozer May 02 '22 at 20:25
  • @paulsm4 Given the edits above any idea as to why the variable ui isnt being returned properly? Is it a scope issue that I am missing? – Connor Tozer May 03 '22 at 00:30
  • "ui" is initialized in UpdateInfo. It sounds like you need to set a breakpoint in the UpdateInfo() constructor and step through the code! – paulsm4 May 03 '22 at 00:34
  • Okay, maybe this is my lack of professional instruction with java, I am solely self taught, but even stepping through the code I am not seeing anything. What am I missing? – Connor Tozer May 03 '22 at 00:39
  • Thing is: you dont need to use a debugger. Just do a println() for LIKE every piece of interesting information. You could for example print `ui.getName().length` BEFORE you start looping. The key aspect: when you dont understand what your code is doing, then change your code to TELL you what is doing, as said, the easy straightforward way is: by adding many print statements. – GhostCat May 04 '22 at 13:50
  • Could you please provide code that is syntactically correct? What does `GetUpdateInfo.getInfo()` return? What values contain `repos_list.size()` and maybe inside the `assets.size()` ? – cyberbrain May 04 '22 at 14:00

1 Answers1

1

An obvious problem of your code is the use of ArrayUtils.add: you have to reassign its result to the input array, as you cannot modify arrays like lists in Java.

Use it like this:

releases = ArrayUtils.add(releases, latest.toString());
names = ArrayUtils.add(names, latest.getName());

and later in the for-loop:

urls = ArrayUtils.add(urls, url);
versions = ArrayUtils.add(versions, version);

Also you don't need to set the elements in each loop cycle to the result:

ui.setURL(urls);
ui.setVersion(versions);

Those would be sufficient once the for-loop has completed.

An alternative would be to first use List<String> instead of the arrays. If you have control over the UpdateInfo class, change it there to be lists too, otherwise create an array from the lists before you set it in UpdateInfo.

As a general advice I would recommend that you get rid of your static methods. Create instances and use your credentials (login, password, oauth) as member fields OR pass in even the whole GitHub instance. This way you would have an easier time writing proper tests.

cyberbrain
  • 3,433
  • 1
  • 12
  • 22
  • thank you for pointing this out, I totally forgot how to use ArrayUtils, and I bet money thats why the values are null! – Connor Tozer May 04 '22 at 14:15