Skip to content

Commit 900fe24

Browse files
committed
fetch Pufferfish versions using Regex
close #5
1 parent ae7ea18 commit 900fe24

2 files changed

Lines changed: 109 additions & 33 deletions

File tree

mc-server-updater-lib/src/main/java/me/hsgamer/mcserverupdater/api/JenkinsUpdater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public abstract class JenkinsUpdater implements SimpleChecksum, InputStreamUpdat
1818
protected final UpdateBuilder updateBuilder;
1919
protected final String version;
2020
protected final String build;
21-
private final String jenkinsUrl;
21+
protected final String jenkinsUrl;
2222

2323
protected JenkinsUpdater(VersionQuery versionQuery, String jenkinsUrl) {
2424
this.jenkinsUrl = jenkinsUrl.endsWith("/") ? jenkinsUrl : jenkinsUrl + "/";
Lines changed: 108 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,140 @@
11
package me.hsgamer.mcserverupdater.updater;
22

3+
import me.hsgamer.hscore.web.UserAgent;
4+
import me.hsgamer.hscore.web.WebUtils;
35
import me.hsgamer.mcserverupdater.api.JenkinsUpdater;
46
import me.hsgamer.mcserverupdater.util.VersionQuery;
7+
import org.json.JSONArray;
8+
import org.json.JSONObject;
9+
import org.json.JSONTokener;
510

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;
618
import java.util.regex.Pattern;
719

820
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+
929
public PufferfishUpdater(VersionQuery versionQuery) {
1030
super(versionQuery, "https://ci.pufferfish.host/");
1131
}
1232

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+
1396
@Override
1497
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"});
17105
}
18106

19107
@Override
20108
public Pattern getArtifactRegex() {
21-
Version v = Version.getVersion(version);
22-
return v == null ? Pattern.compile("INVALID") : v.artifactRegex;
109+
return Pattern.compile(".*\\.jar");
23110
}
24111

25112
@Override
26113
public String getDefaultVersion() {
27-
return "1.17.1";
114+
return "1.20.4";
28115
}
29116

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 {
42118
public final String job;
43-
public final Pattern artifactRegex;
119+
public final String majorVersion;
120+
public final boolean isPlus;
121+
public final boolean isPurpur;
44122

45-
Version(Pattern versionRegex, String job, Pattern artifactRegex) {
46-
this.versionRegex = versionRegex;
123+
private Version(String job, String majorVersion, boolean isPlus, boolean isPurpur) {
47124
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;
53128
}
54129

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+
'}';
62138
}
63139
}
64140
}

0 commit comments

Comments
 (0)