]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6798 cancel a pending task in Compute Engine
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 18 Sep 2015 21:33:50 +0000 (23:33 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Sat, 19 Sep 2015 06:56:40 +0000 (08:56 +0200)
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeCancelWsAction.java [new file with mode: 0644]
server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeTaskWsAction.java
server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/CeCancelWsActionTest.java [new file with mode: 0644]

diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeCancelWsAction.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ws/CeCancelWsAction.java
new file mode 100644 (file)
index 0000000..89031c1
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.computation.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.web.UserRole;
+import org.sonar.core.util.Uuids;
+import org.sonar.server.computation.CeQueue;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.user.UserSession;
+
+public class CeCancelWsAction implements CeWsAction {
+
+  public static final String PARAM_TASK_ID = "id";
+  public static final String PARAM_ALL = "all";
+
+  private final UserSession userSession;
+  private final CeQueue queue;
+
+  public CeCancelWsAction(UserSession userSession, CeQueue queue) {
+    this.userSession = userSession;
+    this.queue = queue;
+  }
+
+  @Override
+  public void define(WebService.NewController controller) {
+    WebService.NewAction action = controller.createAction("cancel")
+      .setDescription("Cancels a pending task. Requires system administration permission.")
+      .setInternal(true)
+      .setPost(true)
+      .setHandler(this);
+
+    action
+      .createParam(PARAM_TASK_ID)
+      .setDescription("Optional id of the task to cancel.")
+      .setExampleValue(Uuids.UUID_EXAMPLE_01);
+
+    action
+      .createParam(PARAM_ALL)
+      .setDescription("Cancels all pending tasks if this parameter is set. Ignored if the parameter " + PARAM_TASK_ID + " is set.")
+      .setBooleanPossibleValues()
+      .setDefaultValue("false");
+  }
+
+  @Override
+  public void handle(Request wsRequest, Response wsResponse) throws Exception {
+    userSession.checkGlobalPermission(UserRole.ADMIN);
+    String taskId = wsRequest.param(PARAM_TASK_ID);
+    if (taskId != null) {
+      queue.cancel(taskId);
+    } else if (wsRequest.paramAsBoolean(PARAM_ALL)) {
+      queue.cancelAll();
+    } else {
+      throw new BadRequestException("Missing parameters");
+    }
+    wsResponse.noContent();
+  }
+}
index eb5aabae5a5c20cd6f390ed763a1495f1578e9bd..6f4ded4540c7060310e40ac0930fdb6120b45391 100644 (file)
@@ -37,7 +37,6 @@ import org.sonarqube.ws.WsCe;
 public class CeTaskWsAction implements CeWsAction {
 
   public static final String ACTION = "task";
-
   public static final String PARAM_TASK_ID = "id";
 
   private final DbClient dbClient;
index 0b06121221f09432e9b9925531e6ee2b44ac7f22..85ca5601521331317e2fc736f343476fe1870938 100644 (file)
@@ -74,6 +74,7 @@ import org.sonar.server.computation.ReportSubmitter;
 import org.sonar.server.computation.monitoring.CEQueueStatusImpl;
 import org.sonar.server.computation.monitoring.ComputeEngineQueueMonitor;
 import org.sonar.server.computation.ws.CeActivityWsAction;
+import org.sonar.server.computation.ws.CeCancelWsAction;
 import org.sonar.server.computation.ws.CeQueueWsAction;
 import org.sonar.server.computation.ws.CeSubmitWsAction;
 import org.sonar.server.computation.ws.CeTaskWsAction;
@@ -728,6 +729,7 @@ public class PlatformLevel4 extends PlatformLevel {
       CeTaskWsAction.class,
       CeSubmitWsAction.class,
       CeActivityWsAction.class,
+      CeCancelWsAction.class,
       CeQueueWsAction.class,
       IsQueueEmptyWs.class,
       DefaultPeriodCleaner.class,
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ws/CeCancelWsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ws/CeCancelWsActionTest.java
new file mode 100644 (file)
index 0000000..8102b93
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.computation.ws;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.utils.System2;
+import org.sonar.api.web.UserRole;
+import org.sonar.db.DbTester;
+import org.sonar.server.computation.CeQueue;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+public class CeCancelWsActionTest {
+
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+
+  @Rule
+  public DbTester dbTester = DbTester.create(System2.INSTANCE);
+
+  CeQueue queue = mock(CeQueue.class);
+  CeCancelWsAction underTest = new CeCancelWsAction(userSession, queue);
+  WsActionTester tester = new WsActionTester(underTest);
+
+  @Test
+  public void cancel_all_pending_tasks() {
+    userSession.setGlobalPermissions(UserRole.ADMIN);
+
+    tester.newRequest()
+      .setParam("all", "true")
+      .execute();
+
+    verify(queue).cancelAll();
+  }
+
+  @Test
+  public void cancel_pending_task() {
+    userSession.setGlobalPermissions(UserRole.ADMIN);
+
+    tester.newRequest()
+      .setParam("id", "T1")
+      .execute();
+
+    verify(queue).cancel("T1");
+  }
+
+  @Test(expected = BadRequestException.class)
+  public void missing_parameters() {
+    userSession.setGlobalPermissions(UserRole.ADMIN);
+
+    tester.newRequest().execute();
+
+    verifyZeroInteractions(queue);
+  }
+
+  @Test(expected = ForbiddenException.class)
+  public void not_authorized() {
+    tester.newRequest()
+      .setParam("id", "T1")
+      .execute();
+
+    verifyZeroInteractions(queue);
+  }
+}