Set assignee on parent based on next subtask

LaurieC July 21, 2021

I'm not a developer and could use some help.

I'm trying to look at all subtasks on a parent issue and use a post function on the "Pass" transition to find the next subtask, look at who it is assigned to, and reassign the parent issue to the person assigned to the next subtask.

For example, 

Parent - ParentAssignee

  • Subtask1 - Subtask1Assignee - Status is Done
  • Subtask2 - Subtask2Assignee - just transition through Pass transition
  • Subtask3 - Subtask3Assignee

 

If the user moves Subtask2 through Pass, as a postfunction, I want to look at Subtask3 and see who it is assigned to (Subtask3Assignee) and then reassign the parent to Subtask3Assignee.

I've pulled together code from various things I've done before and have successfully gotten a list of subtasks, my current issue is changing the subtask number that is an integer, back to the subtask key to then get the assignee.

Here's what I have so far that is cycling through the subtasks:

import com.atlassian.jira.component.ComponentAccessor

log.setLevel(org.apache.log4j.Level.DEBUG)

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("OAK-25198")
log.debug "issue is $issue"
def parent = issue.getParentObject()
log.debug "parent issue is $parent"

def subTasks = parent.getSubTaskObjects()

for (int i = 0; i < subTasks.size(); i++) {
def subtaskNumber = subTasks[i].getKey().findAll( /\d+/ )*.toInteger()[0]
log.debug "subtaskNumber is $subtaskNumber"

def nextSubtaskNumber = subtaskNumber + 1
log.debug "Next subtask Issue number is $nextSubtaskNumber"
}

My log looks as follows:

2021-07-21 16:20:38,654 DEBUG [runner.ScriptBindingsManager]: issue is OAK-25198 2021-07-21 16:20:38,658 DEBUG [runner.ScriptBindingsManager]: parent issue is OAK-25197 2021-07-21 16:20:38,661 DEBUG [runner.ScriptBindingsManager]: subtaskNumber is 25198 2021-07-21 16:20:38,661 DEBUG [runner.ScriptBindingsManager]: Next subtask Issue number is 25199 2021-07-21 16:20:38,661 DEBUG [runner.ScriptBindingsManager]: subtaskNumber is 25200 2021-07-21 16:20:38,661 DEBUG [runner.ScriptBindingsManager]: Next subtask Issue number is 25201 2021-07-21 16:20:38,661 DEBUG [runner.ScriptBindingsManager]: subtaskNumber is 25199 2021-07-21 16:20:38,661 DEBUG [runner.ScriptBindingsManager]: Next subtask Issue number is 25200

 

So I'm able to cycle through the subtasks, and add 1 to the subtask to find the next one. The problem is, my issue key is an integer. So I think I need to get it back to an issue key and then find the assignee on nextSubtaskNumber.

I've tried a couple of things, but no luck yet. Any developers out there able to help me? :)

Many thanks!!
Laurie

1 answer

0 votes
LaurieC July 21, 2021

I'm getting closer... now all I need to do is assign the parent issue - can't seem to figure out the correct code for that...

import com.atlassian.jira.component.ComponentAccessor

log.setLevel(org.apache.log4j.Level.DEBUG)

def issue = ComponentAccessor.issueManager.getIssueByCurrentKey("OAK-25198")
log.debug "issue is $issue"
def parent = issue.getParentObject()
log.debug "parent issue is $parent"

def subTasks = parent.getSubTaskObjects()

for (int i = 0; i < 1; i++) {
def subtaskNumber = subTasks[i].getKey().findAll( /\d+/ )*.toInteger()[0]
log.debug "subtaskNumber is $subtaskNumber"

def nextSubtaskNumber = subtaskNumber + 1
log.debug "Next subtask Issue number is $nextSubtaskNumber"

def nextSubtaskUser = subTasks[i+1].getAssignee()
log.debug "Next subtask user is $nextSubtaskUser"

parent.setAssignee(nextSubtaskUser)
log.debug "parent issue was reassigned to $nextSubtaskUser"
}

 

That last line (parent.setAssignee(nextSubtaskUser)) isn't working.

Thoughts?

Amirul _Adaptavist_ July 31, 2021

Hi @LaurieC ,

That is because you did not saving the issue in the database, you can add something like below:

issue.store()

This will persist the changes on database but the drawback is you will not be able to search using the new value unless you have run reindex.

The proper way to change assignee is by using IssueService:

def issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def validateAssignResult = issueService.validateAssign(user, issue.id, issue.reporterId)
issueService.assign(user, validateAssignResult)

 

Regards,

Amirul 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.5.9
TAGS
AUG Leaders

Atlassian Community Events