Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,463,336
Community Members
 
Community Events
176
Community Groups

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

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

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)

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

Hi @scott_boisvert !

Is this solution could be performed by using REST API?

With kind regards
Viacheslav

@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. 

@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?

@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

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

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

Right after

ComponentAccessor.getSubTaskManager().changeParent(childIssue, parentIssue, user)

@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.

@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

@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

@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 Dec 09, 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

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 Dec 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.

@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 I'm New Here Jan 02, 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 Mar 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

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

 

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

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

Andy Heinzer Atlassian Team 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

Harsh Raj I'm New Here Jan 02, 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