Can I change the parent field of sub-task in Jira via REST API ?

Yakir Giladi March 15, 2018

Can I change the parent field of sub-task in Jira via REST API ?
I tried the command:

/usr/bin/curl -u admin:zubur1 -X POST -H Content-Type:application/json 'http://<my-jira>:<port>/rest/api/2/issue/<issue_key>' --data @@@jsonfile.json

jsonfile content:
{
   "fields": 
    {
       "parent": 
        {
              "key": "<issue_key>",
              "id": "<issue_id>"
         }
    }
}

I got HTTP response code 405 

 

Thanks,

Yakir Giladi

3 answers

1 vote
scott_boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 3, 2019

If you have ScriptRunner you can change the parent like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.pico.ComponentManager

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Issue childIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DEMO-69")
Issue parentIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("DEMO-9")

ComponentAccessor.getSubTaskManager().changeParent(childIssue, parentIssue, user)
IT Accounts July 13, 2020

Actions of this script are not visible in the history of the issue's changes =(

Slava Starovoitov December 2, 2021

Hi @scott_boisvert !

Is this solution could be performed by using REST API?

With kind regards
Viacheslav

Scott Boisvert December 2, 2021

@IT Accounts You can update the script to add an entry to the history log if you want.

@Slava Starovoitov I do not believe there is a built in API call from Atlassian. However, using ScriptRunner you could create a REST EndPoint that uses this script, and then yes, you could do this with REST API. 

Viacheslav Starovoytov December 2, 2021

@Scott Boisvert thanks for the quick reply!

bq. However, using ScriptRunner you could create a REST EndPoint that uses this script, and then yes, you could do this with REST API. 

@Scott Boisvert Yes, I mean using SR. Could you give a little information about how to create a REST EndPoint? We need to do it in SR settings or you mean our self application?

Scott Boisvert December 2, 2021

@Viacheslav Starovoytov Adapavist has some good documentation on how to create a REST Endpoint: https://scriptrunner.adaptavist.com/latest/jira/rest-endpoints.html

You need to be an administrator to create one though. You would need to pull in your parameters and assign them to a variable for example the issue key(s).

Like Viacheslav Starovoytov likes this
Viacheslav Starovoytov December 3, 2021

@Scott Boisvert thank you so much! I'll share here my results.
Have a nice day!

Viacheslav Starovoytov December 6, 2021

Hi @Scott Boisvert  thank you very much, with your help I have managed with changing parent issue (y)

Additionally I put in the script this line in the code:

 issueManager.updateIssue(user,mIssue,EventDispatchOption.ISSUE_UPDATED, false)

to update the history (where mIssue is childissue), but I do not get any update. Could you help with this last thing? 

With kind regards
Viacheslav

Viacheslav Starovoytov December 6, 2021

Right after

ComponentAccessor.getSubTaskManager().changeParent(childIssue, parentIssue, user)
Scott Boisvert December 7, 2021

@Slava Starovoitov  if your using this in post function, make sure it is before the standard: Update change history for an issue and store the issue in the database.

Viacheslav Starovoytov December 9, 2021

@Scott Boisvert Hi! No, it is not a post-function. As you advised we raised the self endpoint. Is it appropriate code for this?

With kind regards

Scott Boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 9, 2021

@Viacheslav Starovoytov  The line you added from what I understand should update the history, but looks like your referencing a different issue (mIssue) where you have childIssue and parentIssue variables in use. 

See this post: https://community.atlassian.com/t5/Jira-questions/Why-doesn-t-Script-Listener-write-to-Issue-History/qaq-p/1174927

Viacheslav Starovoytov December 9, 2021

@Scott Boisvert @Scott Boisvert firstly I had this

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate 
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.servlet.http.HttpServletRequest

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption

@BaseScript CustomEndpointDelegate delegate

def issueManager = ComponentAccessor.issueManager
changeParent(
httpMethod: "POST", groups: ["jira-administrators","jira-robots"]
) { MultivaluedMap queryParams, String body, HttpServletRequest request ->
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Issue childIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TEST-361")
Issue parentIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TEST-531")

ComponentAccessor.getSubTaskManager().changeParent(childIssue, parentIssue, user)
issueManager.updateIssue(user,childIssue,EventDispatchOption.ISSUE_UPDATED, true)

return Response.ok(new JsonBuilder([abc: 42]).toString()).build()
}

 

And then I changed it to this

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate 
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import javax.servlet.http.HttpServletRequest

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.pico.ComponentManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue;

@BaseScript CustomEndpointDelegate delegate

def issueManager = ComponentAccessor.issueManager
def MutableIssue mIssue = issueManager.getIssueByCurrentKey("TEST-361")
changeParent(
httpMethod: "POST", groups: ["jira-administrators","jira-robots"]
) { MultivaluedMap queryParams, String body, HttpServletRequest request ->
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Issue childIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TEST-361")
Issue parentIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("TEST-531")

ComponentAccessor.getSubTaskManager().changeParent(childIssue, parentIssue, user)
issueManager.updateIssue(user,mIssue,EventDispatchOption.ISSUE_UPDATED, true)

return Response.ok(new JsonBuilder([abc: 42]).toString()).build()
}

cause I found some ticket related to this problem and there was this advise.

But both variants do not have impact on history. What may be wrong here?

 

With kind regards
Viacheslav

Scott Boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 9, 2021

@Viacheslav Starovoytov Based on my limited knowledge that should work, but you may need to use ChangeHistoryItem.Builder: https://docs.atlassian.com/software/jira/docs/api/7.0.6/com/atlassian/jira/issue/changehistory/ChangeHistoryItem.Builder.html

Viacheslav Starovoytov December 13, 2021

Hi @Scott Boisvert !

It seems that ChangeHistoryItem.Builder is for reading information from history, is not it?

With kind regards
Slava

Scott Boisvert
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 13, 2021

@Viacheslav Starovoytov nope, reading history is a different class.  ChangeHistoryItem.Builder should be the correct class to "Build" a history item. I've not used it before (so i dont have any direction to give you on how to use the class), but that "Builder" usually means you're creating an instance of that class so you can post it to the issue.

Viacheslav Starovoytov December 13, 2021

@Scott Boisvert ok. I was searching by "ChangeHistoryItem.Builder", and found only getting a history examples.

In any case huge thanks!
Will try to do it with myself and may be with someone else)

Good night!

0 votes
Harsh Raj January 2, 2023

@Andy Heinzer It's been almost 4 years now and still, this API is not available in the server version. An API was created for the Cloud Version of the JIRA last year but no love for JRASERVER-68763. It has gathered enough interest. Can this be picked up now, please?

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 16, 2018

Unfortunately, not all fields of an issue can be edited in this way.  In this case, you can't just change the parent ID field for a subtask.  Even when in the web front end of Jira, you can't just edit that field on this issue.   This is because this is a system field that is specific to that issue type (subtasks are special snowflakes in this regard).   Outside the REST API, the only way to change a subtask to be under a new parent issue is to call the move function in Jira, and in turn move the issue to a new parent.  

The problem as I see it is the REST API's current inability to move issues.  There is an existing feature request for this in https://jira.atlassian.com/browse/JRASERVER-61359

Should this someday be implemented in Jira, then I would expect that you could then move subtasks via a REST call.

I have not found any other work-arounds for this problem, but you are not alone here, this question has been asked a number of times before:

https://community.atlassian.com/t5/Answers-Developer-Questions/REST-API-move-sub-task/qaq-p/510350

https://community.atlassian.com/t5/Answers-Developer-Questions/Can-I-change-sub-task-parent-using-REST/qaq-p/466646

Kevin Chen January 28, 2019

I added a ticket that is specific to sub-tasks: 

 

https://jira.atlassian.com/browse/JRASERVER-68763

Varrun SS May 16, 2019

Does the latest API allow the parent of sub-task to be changed? 

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 17, 2019

Hi @Varrun SS 

The pair of feature requests in JRASERVER-61359 and JRASERVER-68763 are still accurate.  Unfortunately, even in the current versions of Jira Server (8.1.0 today), you cannot move an issue or change a subtask's parent via the REST API.

I would recommend watching these two tickets.  Should this feature come to Jira Server, I would expect these to be updated to reflect which version of Jira would gain this feature.

Andy

Varrun SS May 20, 2019

Thank you @Andy Heinzer

Harsh Raj January 2, 2023

It's been almost 4 years now and still, this API is not available in the server version. An API was created for the Cloud Version of the JIRA last year but no love for JRASERVER-68763. It has gathered enough interest. Can this be picked up now, please? @Andy Heinzer 

Suggest an answer

Log in or Sign up to answer