Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get all children under a issue type 'Project' using script runner in the Script Listener

Pranitha_Gaddam January 28, 2018

We have setup heirarchy using  portfolio for JIRA like below.

Program -> Project -> Epic -> Story -> Sub-Task

We have the following issue types

Program

Project

Epic

Story

Sub-task

1 answer

0 votes
Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 29, 2018

Hello again Pranitha, 

I saw your question on your product support ticket, though it would be helpful if you could log any questions for this script here in the future. If you could include the code you have currently and a description of what you're trying to do in case someone else wants to help you out also, that'd be great. :)

As far as your question from product support: "Now I am stuck at the point where I want to get list of TO DO,In Progress, In Review and Closed Epics under issue type 'Project' in the script listener."

I'm not really sure why you want to do this with the knowledge I have of what you're working on. I'm also not really sure if you're referring to Epics or another issue type. Can you explain what you're trying to do and why with a bit more detail?

Regards, 

Jenna

Pranitha Gaddam January 29, 2018

Hi Jenna,

Based on the EPICs status under Project, I want move Project to the correct status.

For ex: Assume Project and all EPICS under it are in TO DO status.

As soon as one of Epic moved to 'In Progress' automatically the Project should move to 'In Progress'

For that purpose I want to get the status of all Epics under Project, so that I can move the Project to correct status.

Pranitha Gaddam January 29, 2018

Using Issuelinkmanager is there any way we can query for parent-child portfolio relationship

Pranitha_Gaddam January 29, 2018

I have the below code in Script Listener for EPIC issue updated. It works fine but it doesn't take the current status of the EPIC that just got updated. Because of that reason I manipulated the code by adding....

 if (issue.getStatus().name == "TO DO")
        todoEpics += 1;
    else if (issue.getStatus().getName() == "In Progress")
        inprogressEpics += 1;
    else if (issue.getStatus().getName() == "In Review")
        inreviewEpics += 1;
    else if (issue.getStatus().getName() == "Closed")
        closedEpics += 1;

def jqlSearch = "'Parent Link'=" + project.getKey() + " AND key != " + issue.getKey();

That's why I want to know is there any other better way.

 

Complete Working Code:

-----------------------------

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;

Issue issue = event.issue
log.warn('issue.getIssueType().name: ' + issue.getIssueType().name)
log.warn('issue.status.name: ' + issue.status.name)
if(issue.getIssueType().name == 'Epic' && issue.status.name != 'Closed')
{
    def inprogressEpics = 0;
 def inreviewEpics = 0;
 def todoEpics = 0;
 def closedEpics = 0;
   
    if (issue.getStatus().name == "TO DO")
        todoEpics += 1;
    else if (issue.getStatus().getName() == "In Progress")
        inprogressEpics += 1;
    else if (issue.getStatus().getName() == "In Review")
        inreviewEpics += 1;
    else if (issue.getStatus().getName() == "Closed")
        closedEpics += 1;
   
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def cField = customFieldManager.getCustomFieldObjects(issue).find { it.name == 'Parent Link' };  
 String cFieldValue = issue.getCustomFieldValue(cField);
    if(cFieldValue)
 {
        def issueManager = ComponentAccessor.issueManager;
      def project = issueManager.getIssueByCurrentKey(cFieldValue);
        def jqlSearch = "'Parent Link'=" + project.getKey() + " AND key != " + issue.getKey();
  log.warn('JQL: ' + jqlSearch);
  getFilterResult (jqlSearch).each{Issue issue1 -> 
     log.warn(issue1.getKey() + " " + issue1.getStatus().name + " " + issue1.getStatus().getName());    
           if (issue1.getStatus().name == "TO DO")
                todoEpics += 1;
           else if (issue1.getStatus().getName() == "In Progress")
                inprogressEpics += 1;
           else if (issue1.getStatus().getName() == "In Review")
                inreviewEpics += 1;
           else if (issue1.getStatus().getName() == "Closed")
                closedEpics += 1; 
         }  
    
        log.warn('Epic:todo:' + todoEpics + ' inprogress:' + inprogressEpics + ' inreview:' + inreviewEpics + ' closed:' + closedEpics);
        String currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser.name; 
        if(inreviewEpics > 0 && (inprogressEpics <= 0 &&  todoEpics <= 0)) //If at least one child item is in 'In Review' and all other child items moved out of 'In Progress' then move parent to 'In Review'. There is no backward action from 'In Review' on parent
        {
            log.warn('move project to Inreview ');
            if(project.getStatus().name != 'In Review') // check if the issue is not already in 'In Review'
            {
                WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
                workflowTransitionUtil.setIssue(project);
                workflowTransitionUtil.setUserkey(currentUser);  
                workflowTransitionUtil.setAction(51);
                workflowTransitionUtil.validate();
                workflowTransitionUtil.progress(); 
            }
        }
        else if(inprogressEpics > 0 || inreviewEpics > 0 || closedEpics > 0) //check if any one of epic is in In Progress/In Review/Closed then move parent to 'In Progress'. There can be a backward action from 'In Review' to 'In Progress' on parent
        {   
            log.warn('Move project to In Progress:' + project.getStatus().name);
            if(project.getStatus().name != 'In Progress') // check if the issue is not already in 'In Progress'
            {
            WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
            workflowTransitionUtil.setIssue(project);
            workflowTransitionUtil.setUserkey(currentUser);
            workflowTransitionUtil.setAction(11); 
            workflowTransitionUtil.validate();
            workflowTransitionUtil.progress(); 
            }     
        }
        else // otherwise move parent to 'TO DO'
        {  
            log.warn('Move project to To Do');
            if(project.getStatus().name != 'TO DO') // check if the parent is not already in 'To Do'
            {
                if(project.getStatus().name == 'In Progress' || project.getStatus().name == 'Closed') // check if the parent is in 'In Progress' or 'Closed'
                {
                    WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
                        workflowTransitionUtil.setIssue(project);
                        workflowTransitionUtil.setUserkey(currentUser); 
                    if(project.getStatus().name == 'In Progress') // if the parent is in 'In Progress' mode then move it backward to 'TO DO'(i.e. Stop Progress action)
                        workflowTransitionUtil.setAction(31);
                    else  //if the parent is in 'Closed' mode then move it backward to 'TO DO'(i.e. Reopen action)
                        workflowTransitionUtil.setAction(41);
                        workflowTransitionUtil.validate();
                        workflowTransitionUtil.progress(); 
                }        
            } 
        }  
    }   
}

List<Issue> getFilterResult(String jqlSearch)
{
    def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
    SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
    SearchService.ParseResult parseResult =  searchService.parseQuery(user, jqlSearch)
    List<Issue> issues = null
    if (parseResult.isValid()) {
       def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())      
       issues = searchResult.issues
    }
    else {
        return null;
    }
    return issues
}

   

Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 31, 2018

Is everything working and you're just wanting to improve it? Or is there an aspect that doesn't work?

I would recommend checking out the IssueLinkManager class from JIRAs API. You might find something that will work in there.  

Also, are you referring to the 'Project' above all of the 'Epics' or what specifically. Projects don't move status so I'm a little confused there. If so can you explain how everything that you're trying to setup and transfer is linked?

Sorry for asking for more information again, I'm just not understanding what your setup is entirely.

Jenna

Pranitha_Gaddam February 1, 2018

I am having issues with the above kind of code in 'Validators' because it is not taking the current status of the 'Issue' that just got updated.

'Project' is an Issue Type, which is above the Epic.

Pranitha_Gaddam February 1, 2018

HierarchyLevels.PNG

Pranitha_Gaddam February 1, 2018

HierarchyLevels.PNG

Pranitha_Gaddam February 1, 2018

HierarchyLevels.PNG

Pranitha_Gaddam February 1, 2018

On 'EPIC' we have Parent Link to 'Project'

Jenna Davis
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 2, 2018

Alright, if you're just needing the link collection try the getLinkCollection method. It may work for what you want. Or, you might find something else in the IssueLinkManager class that will be more useful for this case, like I said before. 

What kind of issues with validators do you mean? What validators are you using that are causing problems? 

Pranitha_Gaddam February 2, 2018

The issue is with the above code, for some reason it does not take the current updated status of the current issue. I think it will take sometime to sync with JQL results.

Mamta Verma August 8, 2018

@Pranitha_Gaddam Even I'm facing the same issue, status of the current issue is not being updated before the JQL runs. Did you find the solution of this?

Pranitha_Gaddam September 27, 2018

I did not find any proper solution, but I found a workaround for this issue.

As you see in my code get the status of the current issue using issue.getStatus().name

and for the rest of the issues statuses, run JQL by excluding the current issue "key != " + issue.getKey()

def jqlSearch = "'Parent Link'=" + project.getKey() + " AND key != " + issue.getKey();

Suggest an answer

Log in or Sign up to answer