|
1 | 1 | package me.hsgamer.mcserverupdater.updater; |
2 | 2 |
|
| 3 | +import me.hsgamer.hscore.web.UserAgent; |
| 4 | +import me.hsgamer.hscore.web.WebUtils; |
3 | 5 | import me.hsgamer.mcserverupdater.api.JenkinsUpdater; |
4 | 6 | import me.hsgamer.mcserverupdater.util.VersionQuery; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | +import org.json.JSONTokener; |
5 | 10 |
|
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.net.URLConnection; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.regex.Matcher; |
6 | 18 | import java.util.regex.Pattern; |
7 | 19 |
|
8 | 20 | public class PufferfishUpdater extends JenkinsUpdater { |
| 21 | + private static final Pattern NORMAL_JOB_REGEX = Pattern.compile("Pufferfish-(\\d\\.\\d+)"); |
| 22 | + private static final Pattern PURPUR_JOB_REGEX = Pattern.compile("Pufferfish-Purpur-(\\d\\.\\d+)"); |
| 23 | + private static final Pattern PLUS_JOB_REGEX = Pattern.compile("PufferfishPlus-(\\d\\.\\d+)"); |
| 24 | + private static final Pattern PLUS_PURPUR_JOB_REGEX = Pattern.compile("PufferfishPlus-(\\d\\.\\d+)-Purpur"); |
| 25 | + private static final Pattern VERSION_REGEX = Pattern.compile("(\\d\\.\\d+(\\.\\d+)?)((-purpur|-plus)+)?"); |
| 26 | + |
| 27 | + private List<Version> versions; |
| 28 | + |
9 | 29 | public PufferfishUpdater(VersionQuery versionQuery) { |
10 | 30 | super(versionQuery, "https://ci.pufferfish.host/"); |
11 | 31 | } |
12 | 32 |
|
| 33 | + private void loadVersions() { |
| 34 | + String jobsUrl = jenkinsUrl + "api/json?tree=jobs[name]"; |
| 35 | + debug("Getting jobs from " + jobsUrl); |
| 36 | + try { |
| 37 | + URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(jobsUrl)); |
| 38 | + InputStream inputStream = connection.getInputStream(); |
| 39 | + JSONObject jsonObject = new JSONObject(new JSONTokener(inputStream)); |
| 40 | + JSONArray jobsArray = jsonObject.getJSONArray("jobs"); |
| 41 | + for (int i = 0; i < jobsArray.length(); i++) { |
| 42 | + JSONObject job = jobsArray.getJSONObject(i); |
| 43 | + String jobName = job.getString("name"); |
| 44 | + |
| 45 | + Matcher normalMatcher = NORMAL_JOB_REGEX.matcher(jobName); |
| 46 | + if (normalMatcher.matches()) { |
| 47 | + String majorVersion = normalMatcher.group(1); |
| 48 | + versions.add(new Version(jobName, majorVersion, false, false)); |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + Matcher purpurMatcher = PURPUR_JOB_REGEX.matcher(jobName); |
| 53 | + if (purpurMatcher.matches()) { |
| 54 | + String majorVersion = purpurMatcher.group(1); |
| 55 | + versions.add(new Version(jobName, majorVersion, false, true)); |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + Matcher plusMatcher = PLUS_JOB_REGEX.matcher(jobName); |
| 60 | + if (plusMatcher.matches()) { |
| 61 | + String majorVersion = plusMatcher.group(1); |
| 62 | + versions.add(new Version(jobName, majorVersion, true, false)); |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + Matcher plusPurpurMatcher = PLUS_PURPUR_JOB_REGEX.matcher(jobName); |
| 67 | + if (plusPurpurMatcher.matches()) { |
| 68 | + String majorVersion = plusPurpurMatcher.group(1); |
| 69 | + versions.add(new Version(jobName, majorVersion, true, true)); |
| 70 | + } |
| 71 | + } |
| 72 | + } catch (IOException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private Optional<Version> getVersion(String version) { |
| 78 | + version = version.toLowerCase(); |
| 79 | + Matcher matcher = VERSION_REGEX.matcher(version); |
| 80 | + if (!matcher.matches()) { |
| 81 | + return Optional.empty(); |
| 82 | + } |
| 83 | + |
| 84 | + String matchVersion = matcher.group(1); |
| 85 | + String flag = matcher.group(3); |
| 86 | + boolean isPlus = flag != null && flag.contains("plus"); |
| 87 | + boolean isPurpur = flag != null && flag.contains("purpur"); |
| 88 | + |
| 89 | + return versions.stream() |
| 90 | + .filter(v -> matchVersion.startsWith(v.majorVersion)) |
| 91 | + .filter(v -> v.isPlus == isPlus) |
| 92 | + .filter(v -> v.isPurpur == isPurpur) |
| 93 | + .findFirst(); |
| 94 | + } |
| 95 | + |
13 | 96 | @Override |
14 | 97 | public String[] getJob() { |
15 | | - Version v = Version.getVersion(version); |
16 | | - return new String[]{v == null ? "INVALID" : v.job}; |
| 98 | + if (versions == null) { |
| 99 | + versions = new ArrayList<>(); |
| 100 | + loadVersions(); |
| 101 | + } |
| 102 | + return getVersion(version) |
| 103 | + .map(v -> new String[]{v.job}) |
| 104 | + .orElseGet(() -> new String[]{"INVALID"}); |
17 | 105 | } |
18 | 106 |
|
19 | 107 | @Override |
20 | 108 | public Pattern getArtifactRegex() { |
21 | | - Version v = Version.getVersion(version); |
22 | | - return v == null ? Pattern.compile("INVALID") : v.artifactRegex; |
| 109 | + return Pattern.compile(".*\\.jar"); |
23 | 110 | } |
24 | 111 |
|
25 | 112 | @Override |
26 | 113 | public String getDefaultVersion() { |
27 | | - return "1.17.1"; |
| 114 | + return "1.20.4"; |
28 | 115 | } |
29 | 116 |
|
30 | | - private enum Version { |
31 | | - PURPUR_1_17(Pattern.compile("1\\.17(\\.\\d+)?-purpur"), "Pufferfish-Purpur-1.17"), |
32 | | - PURPUR_1_18(Pattern.compile("1\\.18(\\.\\d+)?-purpur"), "Pufferfish-Purpur-1.18"), |
33 | | - NORMAL_1_17(Pattern.compile("1\\.17(\\.\\d+)?"), "Pufferfish-1.17"), |
34 | | - NORMAL_1_18(Pattern.compile("1\\.18(\\.\\d+)?"), "Pufferfish-1.18"), |
35 | | - NORMAL_1_19(Pattern.compile("1\\.19(\\.\\d+)?"), "Pufferfish-1.19"), |
36 | | - PLUS_1_18(Pattern.compile("1\\.18(\\.\\d+)?-plus"), "PufferfishPlus-1.18"), |
37 | | - PLUS_1_19(Pattern.compile("1\\.19(\\.\\d+)?-plus"), "PufferfishPlus-1.19"), |
38 | | - PLUS_PURPUR_1_18(Pattern.compile("1\\.18(\\.\\d+)?-plus-purpur"), "PufferfishPlus-1.18-Purpur"), |
39 | | - PLUS_PURPUR_1_19(Pattern.compile("1\\.19(\\.\\d+)?-plus-purpur"), "PufferfishPlus-1.19-Purpur"), |
40 | | - ; |
41 | | - public final Pattern versionRegex; |
| 117 | + private static final class Version { |
42 | 118 | public final String job; |
43 | | - public final Pattern artifactRegex; |
| 119 | + public final String majorVersion; |
| 120 | + public final boolean isPlus; |
| 121 | + public final boolean isPurpur; |
44 | 122 |
|
45 | | - Version(Pattern versionRegex, String job, Pattern artifactRegex) { |
46 | | - this.versionRegex = versionRegex; |
| 123 | + private Version(String job, String majorVersion, boolean isPlus, boolean isPurpur) { |
47 | 124 | this.job = job; |
48 | | - this.artifactRegex = artifactRegex; |
49 | | - } |
50 | | - |
51 | | - Version(Pattern versionRegex, String job) { |
52 | | - this(versionRegex, job, Pattern.compile(".*\\.jar")); |
| 125 | + this.majorVersion = majorVersion; |
| 126 | + this.isPlus = isPlus; |
| 127 | + this.isPurpur = isPurpur; |
53 | 128 | } |
54 | 129 |
|
55 | | - public static Version getVersion(String version) { |
56 | | - for (Version v : values()) { |
57 | | - if (v.versionRegex.matcher(version).matches()) { |
58 | | - return v; |
59 | | - } |
60 | | - } |
61 | | - return null; |
| 130 | + @Override |
| 131 | + public String toString() { |
| 132 | + return "Version{" + |
| 133 | + "job='" + job + '\'' + |
| 134 | + ", majorVersion='" + majorVersion + '\'' + |
| 135 | + ", isPlus=" + isPlus + |
| 136 | + ", isPurpur=" + isPurpur + |
| 137 | + '}'; |
62 | 138 | } |
63 | 139 | } |
64 | 140 | } |
0 commit comments