Skip to content

Commit a24dde8

Browse files
committed
feat: introduce first version of REST API for subtasks creation
1 parent 0922bb7 commit a24dde8

File tree

4 files changed

+175
-1
lines changed

4 files changed

+175
-1
lines changed

docs/changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ category: Administration
1111

1212
### [Unreleased]
1313

14-
* ...
14+
* feat: introduce first version of REST API for subtasks creation
1515

1616
### [24.06.0] - 2024-06-30
1717

docs/rest-api.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
layout: default
3+
title: Rest API
4+
category: User Guide
5+
---
6+
7+
This page explains how to use the Rest API to create subtasks provided by Multiple Subtasks for Jira.
8+
9+
Please note:
10+
The Rest API requires a minimum version `24.08.0` of Multiple Subtasks for Jira.
11+
Please make sure you have updates Multiple Subtasks for Jira to the latest version for full support of all endpoints.
12+
13+
* Table of Contents
14+
{:toc}
15+
16+
### Create Subtasks `POST /subtasks/{issueKey}`
17+
18+
Request URL: /rest/multiple-subtasks/1.0/subtasks/{issueKey}
19+
Request Method: POST
20+
Content-Type: text/plain
21+
Accept: application/json, text/plain
22+
23+
Create the subtasks from the payload for the given `issueKey`.
24+
25+
#### Status Codes
26+
27+
| Status | Reason | Payload |
28+
|--------|--------------|------------------------------------------------------------------------------------------------------------|
29+
| 200 | Success | The request was successfull and returns a list of created subtasks and all warnings in `application/json`. |
30+
| 400 | Bad Request | The request failed and returns an error message in `text/plain`. |
31+
| 401 | Unauthorized | - |
32+
33+
#### Example Request
34+
35+
- Hallo World
36+
fixVersion: unknown
37+
- Hallo Moon
38+
label: rest-api-demo
39+
40+
#### Example Response
41+
42+
[
43+
{
44+
"issueKey": "YEAH-43",
45+
"issueSummary": "Hallo World",
46+
"warnings": [
47+
"Invalid fixVersion: unknown"
48+
]
49+
},
50+
{
51+
"issueKey": "YEAH-44",
52+
"issueSummary": "Hallo Moon",
53+
"warnings": []
54+
}
55+
]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package de.codescape.jira.plugins.multiplesubtasks.rest.entities;
2+
3+
import de.codescape.jira.plugins.multiplesubtasks.model.CreatedSubtask;
4+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
5+
import org.codehaus.jackson.annotate.JsonProperty;
6+
import org.codehaus.jackson.annotate.JsonPropertyOrder;
7+
8+
import java.util.List;
9+
10+
/**
11+
* JSON entity to transport the created subtask.
12+
*/
13+
@JsonIgnoreProperties(ignoreUnknown = true)
14+
@JsonPropertyOrder({"issueKey", "issueSummary", "warnings"})
15+
public class CreatedSubtaskEntity {
16+
17+
private final CreatedSubtask createdSubtask;
18+
19+
public CreatedSubtaskEntity(CreatedSubtask createdSubtask) {
20+
this.createdSubtask = createdSubtask;
21+
}
22+
23+
@JsonProperty
24+
public String getIssueKey() {
25+
return createdSubtask.getIssue().getKey();
26+
}
27+
28+
@JsonProperty
29+
public String getIssueSummary() {
30+
return createdSubtask.getIssue().getSummary();
31+
}
32+
33+
@JsonProperty
34+
public List<String> getWarnings() {
35+
return createdSubtask.getWarnings();
36+
}
37+
38+
}

0 commit comments

Comments
 (0)