]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4315 Add projectSettings in Action API
authorJulien Lancelot <julien.lancelot@gmail.com>
Mon, 3 Jun 2013 10:32:42 +0000 (12:32 +0200)
committerJulien Lancelot <julien.lancelot@gmail.com>
Mon, 3 Jun 2013 10:32:42 +0000 (12:32 +0200)
pom.xml
sonar-plugin-api/src/main/java/org/sonar/api/issue/action/Function.java
sonar-server/src/main/java/org/sonar/server/issue/ActionService.java
sonar-server/src/test/java/org/sonar/server/issue/ActionServiceTest.java

diff --git a/pom.xml b/pom.xml
index 73cac67243df86b38ec6d70802fa8cfd459e6f89..54b1a54c98f8ab486fe2f0cd45e8697d833be985 100644 (file)
--- a/pom.xml
+++ b/pom.xml
         <plugin>
           <groupId>org.codehaus.sonar</groupId>
           <artifactId>sonar-packaging-maven-plugin</artifactId>
-          <!-- TODO version has to be replaced by ${sonarJava.version} when update center release will be done -->
           <version>1.6</version>
         </plugin>
       </plugins>
index 70401abc17da7c549f034cb8c476d253a559d2d8..a213670ec4775c67a6b04d743127a9dfc9291934 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.api.issue.action;
 
+import org.sonar.api.config.Settings;
 import org.sonar.api.issue.Issue;
 
 import javax.annotation.Nullable;
@@ -33,6 +34,8 @@ public interface Function {
   interface Context {
     Issue issue();
 
+    Settings projectSettings();
+
     Context setAttribute(String key, @Nullable String value);
 
     Context addComment(@Nullable String text);
index 301d0d2bf15878f35b99495c33d3ccd59ea1208a..b8a4ac85fbe9e6cb78c18c84a30813cb4dfccb31 100644 (file)
@@ -25,6 +25,8 @@ import com.google.common.base.Predicate;
 import com.google.common.base.Strings;
 import com.google.common.collect.Iterables;
 import org.sonar.api.ServerComponent;
+import org.sonar.api.component.Component;
+import org.sonar.api.config.Settings;
 import org.sonar.api.issue.Issue;
 import org.sonar.api.issue.IssueQuery;
 import org.sonar.api.issue.IssueQueryResult;
@@ -35,6 +37,8 @@ import org.sonar.api.issue.internal.IssueChangeContext;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.issue.IssueUpdater;
 import org.sonar.core.issue.db.IssueStorage;
+import org.sonar.core.properties.PropertiesDao;
+import org.sonar.core.properties.PropertyDto;
 import org.sonar.server.user.UserSession;
 
 import javax.annotation.CheckForNull;
@@ -51,17 +55,21 @@ public class ActionService implements ServerComponent {
   private final DefaultIssueFinder finder;
   private final IssueStorage issueStorage;
   private final IssueUpdater updater;
+  private final Settings settings;
+  private final PropertiesDao propertiesDao;
   private final List<Action> actions;
 
-  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, List<Action> actions) {
+  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao, List<Action> actions) {
     this.finder = finder;
     this.issueStorage = issueStorage;
     this.updater = updater;
+    this.settings = settings;
+    this.propertiesDao = propertiesDao;
     this.actions = actions;
   }
 
-  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater) {
-    this(finder, issueStorage, updater, Collections.<Action>emptyList());
+  public ActionService(DefaultIssueFinder finder, IssueStorage issueStorage, IssueUpdater updater, Settings settings, PropertiesDao propertiesDao) {
+    this(finder, issueStorage, updater, settings, propertiesDao, Collections.<Action>emptyList());
   }
 
   public List<Action> listAvailableActions(final Issue issue) {
@@ -101,7 +109,7 @@ public class ActionService implements ServerComponent {
     }
 
     IssueChangeContext changeContext = IssueChangeContext.createUser(new Date(), userSession.login());
-    FunctionContext functionContext = new FunctionContext(updater, issue, changeContext);
+    FunctionContext functionContext = new FunctionContext(issue, updater, changeContext, getProjectSettings(queryResult.component(issue)));
     for (Function function : action.functions()) {
       function.execute(functionContext);
     }
@@ -124,16 +132,27 @@ public class ActionService implements ServerComponent {
     }, null);
   }
 
+  public Settings getProjectSettings(Component project) {
+    Settings projectSettings = new Settings(settings);
+    List<PropertyDto> properties = propertiesDao.selectProjectProperties(project.key());
+    for (PropertyDto dto : properties) {
+      projectSettings.setProperty(dto.getKey(), dto.getValue());
+    }
+    return projectSettings;
+  }
+
   static class FunctionContext implements Function.Context {
 
     private final DefaultIssue issue;
     private final IssueUpdater updater;
     private final IssueChangeContext changeContext;
+    private final Settings projectSettings;
 
-    FunctionContext(IssueUpdater updater, DefaultIssue issue, IssueChangeContext changeContext) {
+    FunctionContext(DefaultIssue issue, IssueUpdater updater, IssueChangeContext changeContext, Settings projectSettings) {
       this.updater = updater;
       this.issue = issue;
       this.changeContext = changeContext;
+      this.projectSettings = projectSettings;
     }
 
     @Override
@@ -141,6 +160,11 @@ public class ActionService implements ServerComponent {
       return issue;
     }
 
+    @Override
+    public Settings projectSettings() {
+      return projectSettings;
+    }
+
     @Override
     public Function.Context setAttribute(String key, @Nullable String value) {
       updater.setAttribute(issue, key, value, changeContext);
index 7ef368d9f95525ee2064505aee9cc0c7a24f0a4c..c2d8eaa0400e14a88ca0fccb5aec93f7bfae2208 100644 (file)
 
 package org.sonar.server.issue;
 
+import org.junit.Before;
 import org.junit.Test;
+import org.sonar.api.component.Component;
+import org.sonar.api.config.Settings;
 import org.sonar.api.issue.Issue;
 import org.sonar.api.issue.IssueQuery;
 import org.sonar.api.issue.IssueQueryResult;
@@ -32,9 +35,11 @@ import org.sonar.api.issue.internal.IssueChangeContext;
 import org.sonar.core.issue.DefaultIssueQueryResult;
 import org.sonar.core.issue.IssueUpdater;
 import org.sonar.core.issue.db.IssueStorage;
+import org.sonar.core.properties.PropertiesDao;
 import org.sonar.server.user.UserSession;
 
 import java.util.Collections;
+import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
@@ -45,10 +50,25 @@ import static org.mockito.Mockito.*;
 
 public class ActionServiceTest {
 
+  private DefaultIssueFinder finder;
+  private IssueStorage issueStorage;
+  private IssueUpdater updater;
+  private PropertiesDao propertiesDao;
+  private Settings settings;
+
+  private List<Action> actions;
   private ActionService actionService;
-  private DefaultIssueFinder finder = mock(DefaultIssueFinder.class);
-  private IssueStorage issueStorage = mock(IssueStorage.class);
-  private IssueUpdater updater = mock(IssueUpdater.class);
+
+  @Before
+  public void before(){
+    finder = mock(DefaultIssueFinder.class);
+    issueStorage = mock(IssueStorage.class);
+    updater = mock(IssueUpdater.class);
+    propertiesDao = mock(PropertiesDao.class);
+    settings = new Settings();
+    actions = newArrayList();
+    actionService = new ActionService(finder, issueStorage, updater, settings, propertiesDao, actions);
+  }
 
   @Test
   public void should_execute_functions() {
@@ -56,12 +76,15 @@ public class ActionServiceTest {
     Function function2 = mock(Function.class);
 
     Issue issue = new DefaultIssue().setKey("ABCD");
-    IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
+    IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class);
+    when(issueQueryResult.first()).thenReturn(issue);
+
+    Component project = mock(Component.class);
+    when(issueQueryResult.component(issue)).thenReturn(project);
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function1, function2).build();
+    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function1, function2).build());
 
-    actionService = new ActionService(finder, issueStorage, updater, newArrayList(action));
     assertThat(actionService.execute("ABCD", "link-to-jira", mock(UserSession.class))).isNotNull();
 
     verify(function1).execute(any(Function.Context.class));
@@ -77,12 +100,14 @@ public class ActionServiceTest {
     when(userSession.login()).thenReturn("arthur");
 
     DefaultIssue issue = new DefaultIssue().setKey("ABCD");
-    IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList((Issue) issue));
-    when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
+    IssueQueryResult issueQueryResult = mock(DefaultIssueQueryResult.class);
+    when(issueQueryResult.first()).thenReturn(issue);
+    Component project = mock(Component.class);
+    when(issueQueryResult.component(issue)).thenReturn(project);
 
-    Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build();
+    when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actionService = new ActionService(finder, issueStorage, updater, newArrayList(action));
+    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build());
     assertThat(actionService.execute("ABCD", "link-to-jira", userSession)).isNotNull();
 
     verify(updater).addComment(eq(issue), eq("New tweet on issue ABCD"), any(IssueChangeContext.class));
@@ -96,10 +121,7 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.<Issue>emptyList());
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build();
-
-    actionService = new ActionService(finder, issueStorage, updater, newArrayList(action));
-
+    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build());
     try {
     actionService.execute("ABCD", "link-to-jira", mock(UserSession.class));
     } catch (Exception e){
@@ -116,9 +138,7 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    Action action = Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build();
-
-    actionService = new ActionService(finder, issueStorage, updater, newArrayList(action));
+    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).functions(function).build());
     try {
       actionService.execute("ABCD", "tweet", mock(UserSession.class));
     } catch (Exception e){
@@ -135,9 +155,7 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    Action action = Action.builder("link-to-jira").conditions(new NeverMatch()).functions(function).build();
-
-    actionService = new ActionService(finder, issueStorage, updater, newArrayList(action));
+    actions.add(Action.builder("link-to-jira").conditions(new NeverMatch()).functions(function).build());
     try {
       actionService.execute("ABCD", "link-to-jira", mock(UserSession.class));
     } catch (Exception e){
@@ -153,9 +171,9 @@ public class ActionServiceTest {
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
     Action action1 = Action.builder("link-to-jira").conditions(new AlwaysMatch()).build();
+    actions.add(action1);
     Action action2 = Action.builder("tweet").conditions(new NeverMatch()).build();
-
-    actionService = new ActionService(finder, issueStorage, updater, newArrayList(action1, action2));
+    actions.add(action2);
     assertThat(actionService.listAvailableActions("ABCD")).containsOnly(action1);
   }
 
@@ -164,11 +182,8 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(Collections.<Issue>emptyList());
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    Action action1 = Action.builder("link-to-jira").conditions(new AlwaysMatch()).build();
-    Action action2 = Action.builder("tweet").conditions(new NeverMatch()).build();
-
-    actionService = new ActionService(finder, issueStorage, updater, newArrayList(action1, action2));
-
+    actions.add(Action.builder("link-to-jira").conditions(new AlwaysMatch()).build());
+    actions.add(Action.builder("tweet").conditions(new NeverMatch()).build());
     try {
       actionService.listAvailableActions("ABCD");
       fail();
@@ -183,7 +198,7 @@ public class ActionServiceTest {
     IssueQueryResult issueQueryResult = new DefaultIssueQueryResult(newArrayList(issue));
     when(finder.find(any(IssueQuery.class))).thenReturn(issueQueryResult);
 
-    actionService = new ActionService(finder, issueStorage, updater);
+    actionService = new ActionService(finder, issueStorage, updater, settings, propertiesDao);
     assertThat(actionService.listAvailableActions("ABCD")).isEmpty();
   }