|
| 1 | +package de.codescape.jira.plugins.multiplesubtasks.rest; |
| 2 | + |
| 3 | +import com.atlassian.jira.issue.IssueManager; |
| 4 | +import com.atlassian.jira.issue.MutableIssue; |
| 5 | +import com.atlassian.jira.permission.ProjectPermissions; |
| 6 | +import com.atlassian.jira.security.JiraAuthenticationContext; |
| 7 | +import com.atlassian.jira.security.PermissionManager; |
| 8 | +import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; |
| 9 | +import de.codescape.jira.plugins.multiplesubtasks.rest.entities.CreatedSubtaskEntity; |
| 10 | +import de.codescape.jira.plugins.multiplesubtasks.service.SubtasksCreationService; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | + |
| 13 | +import javax.ws.rs.*; |
| 14 | +import javax.ws.rs.core.MediaType; |
| 15 | +import javax.ws.rs.core.Response; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
| 19 | +/** |
| 20 | + * Rest API to create multiple subtasks. |
| 21 | + * |
| 22 | + * @since 24.08.0 |
| 23 | + */ |
| 24 | +@Path("/subtasks") |
| 25 | +public class SubtasksResource { |
| 26 | + |
| 27 | + private final JiraAuthenticationContext jiraAuthenticationContext; |
| 28 | + private final IssueManager issueManager; |
| 29 | + private final PermissionManager permissionManager; |
| 30 | + private final SubtasksCreationService subtasksCreationService; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + public SubtasksResource(@ComponentImport JiraAuthenticationContext jiraAuthenticationContext, |
| 34 | + @ComponentImport IssueManager issueManager, |
| 35 | + @ComponentImport PermissionManager permissionManager, |
| 36 | + SubtasksCreationService subtasksCreationService) { |
| 37 | + this.jiraAuthenticationContext = jiraAuthenticationContext; |
| 38 | + this.issueManager = issueManager; |
| 39 | + this.permissionManager = permissionManager; |
| 40 | + this.subtasksCreationService = subtasksCreationService; |
| 41 | + } |
| 42 | + |
| 43 | + @POST |
| 44 | + @Path("/{issue}") |
| 45 | + @Consumes(MediaType.TEXT_PLAIN) |
| 46 | + @Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN}) |
| 47 | + public Response put(@PathParam("issue") String issueKey, String content) { |
| 48 | + // TODO consider to remove this check because it is already implemented in SubtasksCreationService |
| 49 | + MutableIssue issue = issueManager.getIssueByCurrentKey(issueKey); |
| 50 | + if (issue == null) { |
| 51 | + return Response |
| 52 | + .status(Response.Status.BAD_REQUEST) |
| 53 | + .entity("Issue not found") |
| 54 | + .type(MediaType.TEXT_PLAIN_TYPE) |
| 55 | + .build(); |
| 56 | + } |
| 57 | + |
| 58 | + // TODO consider to move this check into SubtasksCreationService |
| 59 | + if (!jiraAuthenticationContext.isLoggedInUser() && |
| 60 | + !permissionManager.hasPermission(ProjectPermissions.CREATE_ISSUES, issue, jiraAuthenticationContext.getLoggedInUser())) { |
| 61 | + return Response |
| 62 | + .status(Response.Status.FORBIDDEN) |
| 63 | + .build(); |
| 64 | + } |
| 65 | + |
| 66 | + try { |
| 67 | + List<CreatedSubtaskEntity> createdSubtasks = subtasksCreationService.subtasksFromString(issueKey, content) |
| 68 | + .stream() |
| 69 | + .map(CreatedSubtaskEntity::new) |
| 70 | + .collect(Collectors.toList()); |
| 71 | + return Response.ok(createdSubtasks).build(); |
| 72 | + } catch (RuntimeException e) { |
| 73 | + return Response |
| 74 | + .status(Response.Status.BAD_REQUEST) |
| 75 | + .entity(e.getMessage()) |
| 76 | + .type(MediaType.TEXT_PLAIN_TYPE) |
| 77 | + .build(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments