From 6ce3ec7f1b3f0b3c1baf823037873bdd672af4e5 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 31 May 2013 19:05:13 +0200 Subject: [PATCH] SONAR-4315 Improve Actions API but using directly Action without using Actions --- .../org/sonar/api/issue/action/Action.java | 3 +- .../org/sonar/api/issue/action/Actions.java | 1 + .../org/sonar/server/issue/ActionService.java | 28 ++++++++++++++++--- .../org/sonar/server/platform/Platform.java | 10 ++++++- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java index 8af8b13e09c..113b1ea7fe3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Action.java @@ -22,13 +22,14 @@ package org.sonar.api.issue.action; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.Lists; +import org.sonar.api.ServerComponent; import org.sonar.api.issue.Issue; import org.sonar.api.issue.condition.Condition; import java.util.Arrays; import java.util.List; -public class Action { +public class Action implements ServerComponent { private final String key; private final List conditions; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java index 02f3fc6cbe1..d883ab8a5c7 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Actions.java @@ -26,6 +26,7 @@ import java.util.Set; /** * @since 3.6 */ +// TODO remove it public interface Actions extends ServerComponent { Actions addAction(Action action); diff --git a/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java b/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java index 1f6bd2e87cc..41b9a79a12e 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/ActionService.java @@ -37,9 +37,12 @@ import org.sonar.core.issue.IssueUpdater; import org.sonar.core.issue.db.IssueStorage; import org.sonar.server.user.UserSession; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.util.Date; import java.util.List; +import java.util.Map; import static com.google.common.collect.Lists.newArrayList; @@ -48,9 +51,10 @@ public class ActionService implements ServerComponent { private final DefaultIssueFinder finder; private final IssueStorage issueStorage; private final IssueUpdater updater; - private final DefaultActions actions; + private final List actions; +// private final DefaultActions actions; - public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, DefaultActions actions) { + public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, List actions) { this.finder = finder; this.issueStorage = issueStorage; this.updater = updater; @@ -61,7 +65,7 @@ public class ActionService implements ServerComponent { IssueQueryResult queryResult = loadIssue(issueKey); final DefaultIssue issue = (DefaultIssue) queryResult.first(); - return newArrayList(Iterables.filter(actions.getActions(), new Predicate() { + return newArrayList(Iterables.filter(actions, new Predicate() { @Override public boolean apply(Action action) { return action.supports(issue); @@ -69,6 +73,17 @@ public class ActionService implements ServerComponent { })); } + @CheckForNull + public Action getAction(final String actionKey) { + return Iterables.find(actions, new Predicate() { + @Override + public boolean apply(Action action) { + return action.key().equals(actionKey); + } + }, null); + } + + public Issue execute(String issueKey, String actionKey, UserSession userSession, Map parameters) { public Issue execute(String issueKey, String actionKey, UserSession userSession) { Preconditions.checkArgument(!Strings.isNullOrEmpty(actionKey), "Missing action"); @@ -78,7 +93,7 @@ public class ActionService implements ServerComponent { throw new IllegalStateException("Issue is not found : " + issueKey); } - Action action = actions.getAction(actionKey); + Action action = getAction(actionKey); if (action == null) { throw new IllegalStateException("Action is not found : " + actionKey); } @@ -117,6 +132,11 @@ public class ActionService implements ServerComponent { return issue; } + @Override + public Map parameters() { + return parameters; + } + @Override public Function.Context setAttribute(String key, @Nullable String value) { updater.setAttribute(issue, key, value, changeContext); diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index 47225cd44a6..57972aa6eb0 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -22,6 +22,9 @@ package org.sonar.server.platform; import org.apache.commons.configuration.BaseConfiguration; import org.slf4j.LoggerFactory; import org.sonar.api.config.EmailSettings; +import org.sonar.api.issue.Issue; +import org.sonar.api.issue.action.Action; +import org.sonar.api.issue.condition.HasResolution; import org.sonar.api.platform.ComponentContainer; import org.sonar.api.platform.Server; import org.sonar.api.profiles.AnnotationProfileParser; @@ -34,6 +37,7 @@ import org.sonar.api.rules.XMLRuleParser; import org.sonar.api.utils.HttpDownloader; import org.sonar.api.utils.TimeProfiler; import org.sonar.api.utils.UriReader; +import org.sonar.api.workflow.function.CommentFunction; import org.sonar.core.component.SnapshotPerspectives; import org.sonar.core.config.Logback; import org.sonar.core.i18n.GwtI18n; @@ -272,7 +276,11 @@ public final class Platform { servicesContainer.addSingleton(ActionService.class); // TODO only for test - servicesContainer.addSingleton(ExtendWorkflow.class); + servicesContainer.addSingleton(Action.builder("fake") + .conditions(new HasResolution(Issue.RESOLUTION_FIXED)) + .functions(new CommentFunction()) + .build() + ); // rules servicesContainer.addSingleton(RubyRuleService.class); -- 2.39.5