diff --git a/src/main/java/jenkins/plugins/gerrit/ProjectChanges.java b/src/main/java/jenkins/plugins/gerrit/ProjectChanges.java index 3d3bf7a9..221dee59 100644 --- a/src/main/java/jenkins/plugins/gerrit/ProjectChanges.java +++ b/src/main/java/jenkins/plugins/gerrit/ProjectChanges.java @@ -14,9 +14,12 @@ package jenkins.plugins.gerrit; +import com.google.common.annotations.VisibleForTesting; import com.google.gerrit.extensions.api.GerritApi; +import com.google.gerrit.extensions.client.ListChangesOption; import com.google.gerrit.extensions.common.ChangeInfo; import com.google.gerrit.extensions.restapi.RestApiException; +import java.util.EnumSet; import java.util.Optional; import java.util.logging.Logger; @@ -31,11 +34,48 @@ class ProjectChanges { public Optional get(int changeNumber) { try { - return Optional.ofNullable(gerritApi.changes().id(changeNumber).get()); + EnumSet options = EnumSet.allOf(ListChangesOption.class); + options.remove(ListChangesOption.CHECK); + if (isVersionBelow215(gerritApi.config().server().getVersion())) { + options.remove(ListChangesOption.TRACKING_IDS); + options.remove(ListChangesOption.SKIP_MERGEABLE); + } + return Optional.ofNullable(gerritApi.changes().id(changeNumber).get(options)); } catch (RestApiException e) { LOGGER.severe(String.format("Unable to retrieve change %d", changeNumber)); LOGGER.throwing(ProjectChanges.class.getName(), "get", e); return Optional.empty(); } } + + @VisibleForTesting + boolean isVersionBelow215(String version) { + if (version == null) { + return false; + } + + if (version.equals("<2.8")) { + return true; + } + + String[] versionSplit = version.split("\\."); + if (versionSplit.length == 0) { + return false; + } + try { + if (versionSplit.length >= 1 && Integer.parseInt(versionSplit[0]) < 2) { + return true; + } + if (versionSplit.length >= 1 && Integer.parseInt(versionSplit[0]) > 2) { + return false; + } + if (versionSplit.length == 1 || Integer.parseInt(versionSplit[1]) < 15) { + return true; + } + } catch (NumberFormatException e) { + return false; + } + + return false; + } } diff --git a/src/test/java/jenkins/plugins/gerrit/ProjectChangesTest.java b/src/test/java/jenkins/plugins/gerrit/ProjectChangesTest.java new file mode 100644 index 00000000..25698e30 --- /dev/null +++ b/src/test/java/jenkins/plugins/gerrit/ProjectChangesTest.java @@ -0,0 +1,53 @@ +// Copyright (C) 2019 GerritForge Ltd +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package jenkins.plugins.gerrit; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class ProjectChangesTest { + + @Test + public void testisVersionBelow215() throws Exception { + ProjectChanges pc = new ProjectChanges(null); + + assertTrue(pc.isVersionBelow215("1")); + assertTrue(pc.isVersionBelow215("1.0")); + assertTrue(pc.isVersionBelow215("1.1.1")); + assertTrue(pc.isVersionBelow215("<2.8")); + assertTrue(pc.isVersionBelow215("2")); + assertTrue(pc.isVersionBelow215("2.0")); + assertTrue(pc.isVersionBelow215("2.0.19")); + assertTrue(pc.isVersionBelow215("2.14")); + assertTrue(pc.isVersionBelow215("2.14.99")); + + assertFalse(pc.isVersionBelow215("2.15")); + assertFalse(pc.isVersionBelow215("2.15.0")); + assertFalse(pc.isVersionBelow215("2.15.99")); + assertFalse(pc.isVersionBelow215("2.16")); + assertFalse(pc.isVersionBelow215("3")); + assertFalse(pc.isVersionBelow215("3.0")); + assertFalse(pc.isVersionBelow215("3.0.0")); + assertFalse(pc.isVersionBelow215("3.1")); + + assertFalse(pc.isVersionBelow215(null)); + assertFalse(pc.isVersionBelow215("")); + assertFalse(pc.isVersionBelow215(" ")); + assertFalse(pc.isVersionBelow215(".")); + assertFalse(pc.isVersionBelow215("..")); + assertFalse(pc.isVersionBelow215("Error")); + } +}