Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/main/java/jenkins/plugins/gerrit/ProjectChanges.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,11 +34,48 @@ class ProjectChanges {

public Optional<ChangeInfo> get(int changeNumber) {
try {
return Optional.ofNullable(gerritApi.changes().id(changeNumber).get());
EnumSet<ListChangesOption> 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;
}
}
53 changes: 53 additions & 0 deletions src/test/java/jenkins/plugins/gerrit/ProjectChangesTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}