Merge branch 'stable-2.14' into stable-2.15
* stable-2.14:
Fix broken build status badge link
Allow to protect specific projects against deletion
Add tests for protected projects
Change-Id: I507c76291c1467f42260f7e7d90c6b4724ffa985
diff --git a/README.md b/README.md
index 92bea1f..1444d1a 100644
--- a/README.md
+++ b/README.md
@@ -3,4 +3,5 @@
A plugin which allows projects to be deleted from Gerrit via an SSH command,
REST API or the Project settings screen.
-[![Build Status](https://gerrit-ci.gerritforge.com/job/plugin-delete-project-bazel-master/badge/icon)
+[![Build Status](https://gerrit-ci.gerritforge.com/view/Plugins-master/job/plugin-delete-project-bazel-master/badge/icon
+)](https://gerrit-ci.gerritforge.com/view/Plugins-master/job/plugin-delete-project-bazel-master/)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/Configuration.java b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/Configuration.java
index be7186d..cd11508 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/Configuration.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/Configuration.java
@@ -14,11 +14,16 @@
package com.googlesource.gerrit.plugins.deleteproject;
+import static java.util.stream.Collectors.toList;
+
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.server.config.PluginConfig;
import com.google.gerrit.server.config.PluginConfigFactory;
import com.google.inject.Inject;
import com.google.inject.Singleton;
+import java.util.Arrays;
+import java.util.List;
+import java.util.regex.Pattern;
@Singleton
public class Configuration {
@@ -27,6 +32,7 @@
private final boolean allowDeletionWithTags;
private final boolean hideProjectOnPreserve;
private final String deletedProjectsParent;
+ private final List<Pattern> protectedProjects;
@Inject
public Configuration(PluginConfigFactory pluginConfigFactory, @PluginName String pluginName) {
@@ -34,6 +40,11 @@
allowDeletionWithTags = cfg.getBoolean("allowDeletionOfReposWithTags", true);
hideProjectOnPreserve = cfg.getBoolean("hideProjectOnPreserve", false);
deletedProjectsParent = cfg.getString("parentForDeletedProjects", DELETED_PROJECTS_PARENT);
+ protectedProjects =
+ Arrays.asList(cfg.getStringList("protectedProject"))
+ .stream()
+ .map(Pattern::compile)
+ .collect(toList());
}
public boolean deletionWithTagsAllowed() {
@@ -47,4 +58,8 @@
public String getDeletedProjectsParent() {
return deletedProjectsParent;
}
+
+ public List<Pattern> protectedProjects() {
+ return protectedProjects;
+ }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/ProtectedProjects.java b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/ProtectedProjects.java
index 7074149..c1e697d 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/deleteproject/ProtectedProjects.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/deleteproject/ProtectedProjects.java
@@ -14,6 +14,7 @@
package com.googlesource.gerrit.plugins.deleteproject;
+import com.google.common.annotations.VisibleForTesting;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.config.AllProjectsNameProvider;
@@ -27,18 +28,31 @@
public class ProtectedProjects {
private final AllProjectsName allProjectsName;
private final AllUsersName allUsersName;
+ private final Configuration config;
@Inject
ProtectedProjects(
- AllProjectsNameProvider allProjectsNameProvider, AllUsersNameProvider allUsersNameProvider) {
+ AllProjectsNameProvider allProjectsNameProvider,
+ AllUsersNameProvider allUsersNameProvider,
+ Configuration config) {
this.allProjectsName = allProjectsNameProvider.get();
this.allUsersName = allUsersNameProvider.get();
+ this.config = config;
}
- public boolean isProtected(Project.NameKey name) {
+ private boolean isDefaultProtected(Project.NameKey name) {
return name.equals(allProjectsName) || name.equals(allUsersName);
}
+ private boolean isCustomProtected(Project.NameKey name) {
+ return config.protectedProjects().stream().anyMatch(p -> p.matcher(name.get()).matches());
+ }
+
+ @VisibleForTesting
+ public boolean isProtected(Project.NameKey name) {
+ return isDefaultProtected(name) || isCustomProtected(name);
+ }
+
public boolean isProtected(ProjectResource rsrc) {
return isProtected(rsrc.getNameKey());
}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index cbb1719..65f996d 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -43,3 +43,13 @@
is set to true.
By default `Deleted-Projects`.
+
+<a id="protectedProject">
+`plugin.@PLUGIN@.protectedProject`
+: The name of a project that is protected against deletion. May be an exact
+ name or a regular expression.
+
+ May be specified more than once to specify multiple project names or
+ patterns.
+
+ By default not set.
diff --git a/src/test/java/com/googlesource/gerrit/plugins/deleteproject/ProtectedProjectsTest.java b/src/test/java/com/googlesource/gerrit/plugins/deleteproject/ProtectedProjectsTest.java
new file mode 100644
index 0000000..9133f64
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/deleteproject/ProtectedProjectsTest.java
@@ -0,0 +1,99 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// 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 com.googlesource.gerrit.plugins.deleteproject;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableList;
+import com.google.gerrit.reviewdb.client.Project;
+import com.google.gerrit.server.config.AllProjectsName;
+import com.google.gerrit.server.config.AllProjectsNameProvider;
+import com.google.gerrit.server.config.AllUsersName;
+import com.google.gerrit.server.config.AllUsersNameProvider;
+import com.google.gerrit.server.config.PluginConfig;
+import com.google.gerrit.server.config.PluginConfigFactory;
+import java.util.List;
+import org.eclipse.jgit.lib.Config;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ProtectedProjectsTest {
+ private static final String PLUGIN_NAME = "delete-project";
+
+ @Mock private AllProjectsNameProvider allProjectsMock;
+ @Mock private AllUsersNameProvider allUsersMock;
+ @Mock private PluginConfigFactory pluginConfigFactoryMock;
+
+ private PluginConfig pluginConfig;
+ private Configuration deleteConfig;
+ private ProtectedProjects protectedProjects;
+
+ @Before
+ public void setup() throws Exception {
+ when(allProjectsMock.get()).thenReturn(new AllProjectsName("All-Projects"));
+ when(allUsersMock.get()).thenReturn(new AllUsersName("All-Users"));
+ pluginConfig = new PluginConfig(PLUGIN_NAME, new Config());
+ when(pluginConfigFactoryMock.getFromGerritConfig(PLUGIN_NAME)).thenReturn(pluginConfig);
+ deleteConfig = new Configuration(pluginConfigFactoryMock, PLUGIN_NAME);
+ protectedProjects = new ProtectedProjects(allProjectsMock, allUsersMock, deleteConfig);
+ }
+
+ @Test
+ public void allProjectsIsProtected() throws Exception {
+ assertProtected("All-Projects");
+ }
+
+ @Test
+ public void allUsersIsProtected() throws Exception {
+ assertProtected("All-Users");
+ }
+
+ @Test
+ public void otherProjectIsNotProtected() throws Exception {
+ assertNotProtected("test-project");
+ }
+
+ @Test
+ public void customProjectIsProtected() throws Exception {
+ List<String> projects = ImmutableList.of("Custom-Parent", "^protected-.*");
+ pluginConfig.setStringList("protectedProject", projects);
+ when(pluginConfigFactoryMock.getFromGerritConfig(PLUGIN_NAME)).thenReturn(pluginConfig);
+ deleteConfig = new Configuration(pluginConfigFactoryMock, PLUGIN_NAME);
+ assertThat(deleteConfig.protectedProjects()).hasSize(projects.size());
+ protectedProjects = new ProtectedProjects(allProjectsMock, allUsersMock, deleteConfig);
+
+ assertProtected("protected-project-1");
+ assertProtected("protected-project-2");
+ assertProtected("Custom-Parent");
+ assertNotProtected("test-project");
+ assertNotProtected("protected");
+ assertNotProtected("my-protected-project");
+ assertNotProtected("Another-Custom-Parent");
+ assertNotProtected("Custom-Parent-2");
+ }
+
+ private void assertProtected(String name) {
+ assertThat(protectedProjects.isProtected(new Project.NameKey(name))).isTrue();
+ }
+
+ private void assertNotProtected(String name) {
+ assertThat(protectedProjects.isProtected(new Project.NameKey(name))).isFalse();
+ }
+}