--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.issue;
+
+import java.util.List;
+import org.sonar.db.issue.IssueDto;
+import org.sonar.server.user.UserSession;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
+
+/**
+ * @since 3.6
+ */
+public class ActionFinder {
+
+ private final UserSession userSession;
+
+ public ActionFinder(UserSession userSession) {
+ this.userSession = userSession;
+ }
+
+ public List<String> listAvailableActions(IssueDto issue) {
+ List<String> availableActions = newArrayList();
+ String login = userSession.getLogin();
+ if (login != null) {
+ availableActions.add("comment");
+ if (issue.getResolution() == null) {
+ availableActions.add("assign");
+ availableActions.add("set_tags");
+ availableActions.add("set_type");
+ if (!login.equals(issue.getAssignee())) {
+ availableActions.add("assign_to_me");
+ }
+ String projectUuid = issue.getProjectUuid();
+ if (projectUuid != null && userSession.hasComponentUuidPermission(ISSUE_ADMIN, projectUuid)) {
+ availableActions.add("set_severity");
+ }
+ }
+ }
+ return availableActions;
+ }
+
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.issue;
-
-import java.util.List;
-import org.sonar.api.issue.Issue;
-import org.sonar.api.server.ServerSide;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.server.user.UserSession;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-
-/**
- * @since 3.6
- */
-@ServerSide
-public class ActionService {
-
- private final DbClient dbClient;
- private final UserSession userSession;
- private final IssueService issueService;
-
- public ActionService(DbClient dbClient, UserSession userSession, IssueService issueService) {
- this.dbClient = dbClient;
- this.userSession = userSession;
- this.issueService = issueService;
- }
-
- public List<String> listAvailableActions(String issueKey) {
- DbSession session = dbClient.openSession(false);
- try {
- return listAvailableActions(issueService.getByKeyForUpdate(session, issueKey).toDefaultIssue());
- } finally {
- dbClient.closeSession(session);
- }
- }
-
- public List<String> listAvailableActions(Issue issue) {
- List<String> availableActions = newArrayList();
- String login = userSession.getLogin();
- if (login != null) {
- availableActions.add("comment");
- if (issue.resolution() == null) {
- availableActions.add("assign");
- availableActions.add("set_tags");
- availableActions.add("set_type");
- if (!login.equals(issue.assignee())) {
- availableActions.add("assign_to_me");
- }
- String projectUuid = issue.projectUuid();
- if (projectUuid != null && userSession.hasComponentUuidPermission(ISSUE_ADMIN, projectUuid)) {
- availableActions.add("set_severity");
- }
- }
- }
- return availableActions;
- }
-
-}
return comment;
}
- public boolean canEditOrDelete(IssueChangeDto dto) {
- return userSession.isLoggedIn() && userSession.getLogin().equals(dto.getUserLogin());
- }
-
private void verifyLoggedIn(UserSession userSession) {
if (!userSession.isLoggedIn()) {
throw new UnauthorizedException("User is not logged in");
this.format = format;
}
- public void write(String issueKey, Request request, Response response) throws Exception {
+ public void write(String issueKey, Request request, Response response) {
SearchResponseLoader.Collector collector = new SearchResponseLoader.Collector(
ALL_ADDITIONAL_FIELDS, singletonList(issueKey));
SearchResponseData data = loader.load(collector, null);
- Issues.Operation responseBody = this.format.formatOperation(data);
+ Issues.Operation responseBody = format.formatOperation(data);
WsUtils.writeProtobuf(responseBody, request, response);
}
import org.sonar.db.issue.IssueDto;
import org.sonar.db.protobuf.DbIssues;
import org.sonar.server.es.Facets;
-import org.sonar.server.issue.ActionService;
-import org.sonar.server.issue.IssueCommentService;
+import org.sonar.server.issue.ActionFinder;
import org.sonar.server.issue.TransitionService;
+import org.sonar.server.user.UserSession;
import org.sonarqube.ws.client.issue.IssuesWsParameters;
import static com.google.common.collect.Lists.newArrayList;
*/
public class SearchResponseLoader {
+ private final UserSession userSession;
private final DbClient dbClient;
- private final ActionService actionService;
- private final IssueCommentService commentService;
+ private final ActionFinder actionService;
private final TransitionService transitionService;
- public SearchResponseLoader(DbClient dbClient, ActionService actionService, IssueCommentService commentService, TransitionService transitionService) {
+ public SearchResponseLoader(UserSession userSession, DbClient dbClient, ActionFinder actionService, TransitionService transitionService) {
+ this.userSession = userSession;
this.dbClient = dbClient;
this.actionService = actionService;
- this.commentService = commentService;
this.transitionService = transitionService;
}
result.setComments(comments);
for (IssueChangeDto comment : comments) {
collector.add(USERS, comment.getUserLogin());
- if (commentService.canEditOrDelete(comment)) {
+ if (canEditOrDelete(comment)) {
result.addUpdatableComment(comment.getKey());
}
}
}
}
+ private boolean canEditOrDelete(IssueChangeDto dto) {
+ return userSession.isLoggedIn() && userSession.getLogin().equals(dto.getUserLogin());
+ }
+
private void loadRules(Collector collector, DbSession dbSession, SearchResponseData result) {
if (collector.contains(RULES)) {
result.setRules(dbClient.ruleDao().selectByKeys(dbSession, collector.<RuleKey>get(RULES)));
for (IssueDto dto : result.getIssues()) {
// so that IssueDto can be used.
if (collector.contains(ACTIONS)) {
- result.addActions(dto.getKey(), actionService.listAvailableActions(dto.toDefaultIssue()));
+ result.addActions(dto.getKey(), actionService.listAvailableActions(dto));
}
if (collector.contains(TRANSITIONS)) {
// TODO workflow and action engines must not depend on org.sonar.api.issue.Issue but on a generic interface
import org.sonar.server.es.IndexCreator;
import org.sonar.server.es.IndexDefinitions;
import org.sonar.server.event.NewAlerts;
-import org.sonar.server.issue.ActionService;
+import org.sonar.server.issue.ActionFinder;
import org.sonar.server.issue.AddTagsAction;
import org.sonar.server.issue.AssignAction;
import org.sonar.server.issue.CommentAction;
IssueCommentService.class,
InternalRubyIssueService.class,
IssueChangelogService.class,
- ActionService.class,
+ ActionFinder.class,
IssueBulkChangeService.class,
WsResponseCommonFormat.class,
IssueWsModule.class,
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.issue;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.db.issue.IssueDto;
+import org.sonar.server.tester.UserSessionRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
+import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
+import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.db.rule.RuleTesting.newXooX1;
+import static org.sonar.server.issue.IssueTesting.newDto;
+
+public class ActionFinderTest {
+
+ static final String PROJECT_KEY = "PROJECT_KEY";
+ static final String PROJECT_UUID = "PROJECT_UUID";
+
+ static final String ISSUE_KEY = "ISSUE_KEY";
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
+ public UserSessionRule userSession = UserSessionRule.standalone().login("arthur");
+
+ private ComponentDto project = newProjectDto(PROJECT_UUID).setKey(PROJECT_KEY);
+ private IssueDto issue = newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY);
+
+ private ActionFinder underTest = new ActionFinder(userSession);
+
+ @Test
+ public void return_provided_actions_without_set_severity_when_not_issue_admin() {
+ assertThat(underTest.listAvailableActions(issue)).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me");
+ }
+
+ @Test
+ public void return_provided_actions_with_set_severity_when_issue_admin() {
+ userSession.addProjectUuidPermissions(ISSUE_ADMIN, PROJECT_UUID);
+ assertThat(underTest.listAvailableActions(issue)).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me", "set_severity");
+ }
+
+ @Test
+ public void return_no_actions_when_not_logged() {
+ userSession.anonymous();
+ assertThat(underTest.listAvailableActions(issue)).isEmpty();
+ }
+
+ @Test
+ public void doest_not_return_assign_to_me_action_when_issue_already_assigned_to_user() {
+
+ userSession.login("julien");
+ IssueDto issue = newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setAssignee("julien");
+ assertThat(underTest.listAvailableActions(issue)).doesNotContain("assign_to_me");
+ }
+
+ @Test
+ public void return_only_comment_action_when_issue_has_a_resolution() {
+ IssueDto issue = newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setResolution(RESOLUTION_FIXED);
+ assertThat(underTest.listAvailableActions(issue)).containsOnly("comment");
+ }
+
+}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.issue;
-
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.sonar.db.DbClient;
-import org.sonar.db.DbSession;
-import org.sonar.db.component.ComponentDto;
-import org.sonar.db.issue.IssueDto;
-import org.sonar.server.tester.UserSessionRule;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-import static org.sonar.api.issue.Issue.RESOLUTION_FIXED;
-import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
-import static org.sonar.db.component.ComponentTesting.newFileDto;
-import static org.sonar.db.component.ComponentTesting.newProjectDto;
-import static org.sonar.db.rule.RuleTesting.newXooX1;
-
-public class ActionServiceTest {
-
- static final String PROJECT_KEY = "PROJECT_KEY";
- static final String PROJECT_UUID = "PROJECT_UUID";
-
- static final String ISSUE_KEY = "ISSUE_KEY";
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Rule
- public UserSessionRule userSession = UserSessionRule.standalone().login("arthur");
-
- DbClient dbClient = mock(DbClient.class);
- DbSession session = mock(DbSession.class);
-
- IssueService issueService = mock(IssueService.class);
- ActionService underTest;
-
- ComponentDto project;
- IssueDto issue;
-
- @Before
- public void before() {
- when(dbClient.openSession(false)).thenReturn(session);
-
- project = newProjectDto(PROJECT_UUID).setKey(PROJECT_KEY);
- issue = IssueTesting.newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY);
-
- underTest = new ActionService(dbClient, userSession, issueService);
- }
-
- @Test
- public void return_provided_actions_without_set_severity_when_not_issue_admin() {
- assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me");
- }
-
- @Test
- public void return_provided_actions_with_set_severity_when_issue_admin() {
- userSession.addProjectUuidPermissions(ISSUE_ADMIN, PROJECT_UUID);
- assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).containsOnly("comment", "assign", "set_tags", "set_type", "assign_to_me", "set_severity");
- }
-
- @Test
- public void return_no_actions_when_not_logged() {
- userSession.anonymous();
- assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).isEmpty();
- }
-
- @Test
- public void doest_not_return_assign_to_me_action_when_issue_already_assigned_to_user() {
- userSession.login("julien");
- IssueDto issue = IssueTesting.newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setAssignee("julien");
- assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).doesNotContain("assign_to_me");
- }
-
- @Test
- public void return_only_comment_action_when_issue_has_a_resolution() {
- IssueDto issue = IssueTesting.newDto(newXooX1().setId(10), newFileDto(project, null), project).setKee(ISSUE_KEY).setResolution(RESOLUTION_FIXED);
- assertThat(underTest.listAvailableActions(issue.toDefaultIssue())).containsOnly("comment");
- }
-
- @Test
- public void return_actions_by_issue_key() {
- when(issueService.getByKeyForUpdate(session, ISSUE_KEY)).thenReturn(issue);
- assertThat(underTest.listAvailableActions(ISSUE_KEY)).isNotEmpty();
- }
-
-}