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;
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;
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) {
}
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);
}
}, 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
return issue;
}
+ @Override
+ public Settings projectSettings() {
+ return projectSettings;
+ }
+
@Override
public Function.Context setAttribute(String key, @Nullable String value) {
updater.setAttribute(issue, key, value, changeContext);
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;
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;
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() {
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));
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));
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){
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){
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){
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);
}
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();
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();
}