aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/NEUIO.java
blob: b9f086a468d40cfcaf8acd70095b023037f2b0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package io.github.moulberry.notenoughupdates;

import org.kohsuke.github.*;

import java.io.IOException;
import java.util.*;

public class NEUIO {

    private final String accessToken;

    /**
     * THIS CLASS PROVIDES METHODS FOR INTERFACING WITH THE GIT REPOSITORY NotEnoughUpdates-REPO. THIS REPOSITORY
     * CONTAINS ALL THE JSON ITEMS. THIS SHOULD NOT BE A PERMANENT SOLUTION AND I SHOULD LOOK AT USING SOME FORM OF
     * HOSTING SERVICE OTHER THAN A GIT REPOSITORY IF THE USERBASE OF THE MOD GROWS SIGNIFICANTLY. UNFORTUNATELY I
     * CANT AFFORD HOSTING RIGHT NOW SO THIS IS WHAT YOU GET AND GITHUB WILL PROBABLY THROW A FIT IF A LARGE NUMBER
     * OF USERS START DOWNLOADING FROM THE REPO ALL AT ONCE.
     */

    public NEUIO(String accessToken) {
        this.accessToken = accessToken;
    }

    /**
     * Creates a new branch, commits to it with a single file change and submits a pull request from the new branch
     * back to the master branch.
     */
    public boolean createNewRequest(String newBranchName, String prTitle, String prBody, String filename, String content) {
        try {
            GitHub github = new GitHubBuilder().withOAuthToken(accessToken).build();
            System.out.println("Getting repo");

            //https://github.com/Moulberry/NotEnoughUpdates-REPO
            GHRepository repo = github.getRepositoryById("247692460");

            System.out.println("Getting last commit");
            String lastCommitSha = repo.getRef("heads/master").getObject().getSha();
            System.out.println("Last master commit sha: " + lastCommitSha);

            String lastTreeSha = repo.getCommit(lastCommitSha).getTree().getSha();

            GHTreeBuilder tb = repo.createTree();
            tb.baseTree(lastTreeSha);
            tb.add(filename, content, false);
            GHTree tree = tb.create();
            System.out.println("Created new tree: " + tree.getSha());

            GHCommitBuilder cb = repo.createCommit();
            cb.message(prTitle);
            cb.tree(tree.getSha());
            cb.parent(lastCommitSha);
            GHCommit commit = cb.create();
            System.out.println("Created commit: " + commit.getSHA1());

            repo.createRef("refs/heads/"+newBranchName, commit.getSHA1());
            System.out.println("Set new branch head to commit.");

            repo.createPullRequest(prTitle, newBranchName, "master", prBody);
            return true;
        } catch(IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @param oldShas Map from filename (eg. BOW.json) to the sha in the local repository
     * @return Map from filename to the new shas
     */
    public Map<String, String> getChangedItems(Map<String, String> oldShas) {
        HashMap<String, String> changedFiles = new HashMap<>();
        try {
            GitHub github = new GitHubBuilder().withOAuthToken(accessToken).build();
            GHRepository repo = github.getRepositoryById("247692460");

            for(GHTreeEntry treeEntry : repo.getTreeRecursive("master", 1).getTree()) {
                if(treeEntry.getPath().contains(".")) {
                    String oldSha = oldShas.get(treeEntry.getPath());
                    if(!treeEntry.getSha().equals(oldSha)) {
                        changedFiles.put(treeEntry.getPath(), treeEntry.getSha());
                    }
                }
            }
        } catch(IOException e) {
            return null;
        }
        return changedFiles;
    }

    public Set<String> getRemovedItems(Set<String> currentlyInstalled) {
        Set<String> removedItems = new HashSet<>();
        Set<String> repoItems = new HashSet<>();
        try {
            GitHub github = new GitHubBuilder().withOAuthToken(accessToken).build();
            GHRepository repo = github.getRepositoryById("247692460");

            for(GHTreeEntry treeEntry : repo.getTreeRecursive("master", 1).getTree()) {
                String[] split = treeEntry.getPath().split("/");
                repoItems.add(split[split.length-1].split("\\.")[0]);
            }
        } catch(IOException e) {
            e.printStackTrace();
            return removedItems;
        }
        removedItems.addAll(currentlyInstalled);
        removedItems.removeAll(repoItems);
        return removedItems;
    }
}