Skip to content

Commit 30591dd

Browse files
Grzegorz Golawskilucamilanesio
authored andcommitted
Support Gerrit < 2.15
Check if Gerrit server version is < 2.15 and remove unsupported options before using changes API. Change-id: I45765e594503d0836a73913643f5923b5ed1bb7e
1 parent d73aa8b commit 30591dd

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

src/main/java/jenkins/plugins/gerrit/ProjectChanges.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414

1515
package jenkins.plugins.gerrit;
1616

17+
import com.google.common.annotations.VisibleForTesting;
1718
import com.google.gerrit.extensions.api.GerritApi;
19+
import com.google.gerrit.extensions.client.ListChangesOption;
1820
import com.google.gerrit.extensions.common.ChangeInfo;
1921
import com.google.gerrit.extensions.restapi.RestApiException;
22+
import java.util.EnumSet;
2023
import java.util.Optional;
24+
import java.util.logging.Level;
2125
import java.util.logging.Logger;
2226

2327
class ProjectChanges {
@@ -31,11 +35,53 @@ class ProjectChanges {
3135

3236
public Optional<ChangeInfo> get(int changeNumber) {
3337
try {
34-
return Optional.ofNullable(gerritApi.changes().id(changeNumber).get());
38+
EnumSet<ListChangesOption> options = EnumSet.allOf(ListChangesOption.class);
39+
options.remove(ListChangesOption.CHECK);
40+
if (isVersionBelow215(gerritApi.config().server().getVersion())) {
41+
options.remove(ListChangesOption.TRACKING_IDS);
42+
options.remove(ListChangesOption.SKIP_MERGEABLE);
43+
}
44+
return Optional.ofNullable(gerritApi.changes().id(changeNumber).get(options));
3545
} catch (RestApiException e) {
3646
LOGGER.severe(String.format("Unable to retrieve change %d", changeNumber));
3747
LOGGER.throwing(ProjectChanges.class.getName(), "get", e);
3848
return Optional.empty();
3949
}
4050
}
51+
52+
@VisibleForTesting
53+
boolean isVersionBelow215(String version) {
54+
if (version == null) {
55+
return false;
56+
}
57+
58+
if (version.equals("<2.8")) {
59+
return true;
60+
}
61+
62+
String[] versionSplit = version.split("\\.");
63+
if (versionSplit.length == 0) {
64+
return false;
65+
}
66+
try {
67+
int majorVersion = Integer.parseInt(versionSplit[0]);
68+
if (majorVersion < 2) {
69+
return true;
70+
}
71+
if (majorVersion > 2) {
72+
return false;
73+
}
74+
if (versionSplit.length < 2) {
75+
return true;
76+
}
77+
int minorVersion = Integer.parseInt(versionSplit[1]);
78+
if (minorVersion < 15) {
79+
return true;
80+
}
81+
return false;
82+
} catch (NumberFormatException e) {
83+
LOGGER.log(Level.SEVERE, "Unable to parse Gerrit version " + version, e);
84+
return false;
85+
}
86+
}
4187
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (C) 2019 GerritForge Ltd
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package jenkins.plugins.gerrit;
16+
17+
import static org.junit.Assert.*;
18+
19+
import org.junit.Test;
20+
21+
public class ProjectChangesTest {
22+
23+
@Test
24+
public void testisVersionBelow215() throws Exception {
25+
ProjectChanges pc = new ProjectChanges(null);
26+
27+
assertTrue(pc.isVersionBelow215("1"));
28+
assertTrue(pc.isVersionBelow215("1.0"));
29+
assertTrue(pc.isVersionBelow215("1.1.1"));
30+
assertTrue(pc.isVersionBelow215("<2.8"));
31+
assertTrue(pc.isVersionBelow215("2"));
32+
assertTrue(pc.isVersionBelow215("2.0"));
33+
assertTrue(pc.isVersionBelow215("2.0.19"));
34+
assertTrue(pc.isVersionBelow215("2.14"));
35+
assertTrue(pc.isVersionBelow215("2.14.99"));
36+
37+
assertFalse(pc.isVersionBelow215("2.15"));
38+
assertFalse(pc.isVersionBelow215("2.15.0"));
39+
assertFalse(pc.isVersionBelow215("2.15.99"));
40+
assertFalse(pc.isVersionBelow215("2.16"));
41+
assertFalse(pc.isVersionBelow215("3"));
42+
assertFalse(pc.isVersionBelow215("3.0"));
43+
assertFalse(pc.isVersionBelow215("3.0.0"));
44+
assertFalse(pc.isVersionBelow215("3.1"));
45+
46+
assertFalse(pc.isVersionBelow215(null));
47+
assertFalse(pc.isVersionBelow215(""));
48+
assertFalse(pc.isVersionBelow215(" "));
49+
assertFalse(pc.isVersionBelow215("."));
50+
assertFalse(pc.isVersionBelow215(".."));
51+
assertFalse(pc.isVersionBelow215("Error"));
52+
}
53+
}

0 commit comments

Comments
 (0)