Transition a linked issue when parent/source issue moves with postfuntion : Scriptrunner

Hi everyone,

After doing some research online, I found a postfuntion that can transition a linked issue if the parent issue does a certain post function. I am not sure if I am using this script correctly. The transition on the linked issue/child issue is 61. The linked issue is currently on the display status and I want to move it to the Not Displayed status with the remove transition(61). The Parent/source issue is on the Populate status and it gets moved to the Review status.

 

When the parent issue gets moved from populate to review, I want the other issue(linked issue) from another project to get moved from Display to Not Display.  This post funtion lies in the parent/source issue transition. Currently, the transition gives no errors and although I have logs in the script, I am not displaying any logs because I don't know how. Can somone help me and let me know what I need to change to make the issues transition properly.

 

The issue is linked with the "is caused by" link

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
def issueService = ComponentAccessor.getIssueService()
def linkType = ["is caused by"]
// 6.x validateTransition wants a User, so we have to use getDirectoryUser()
// 7.x validateTransition wants an ApplicationUser, so remove the .getDirectoryUser() after we upgrade
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
// Look through the outward links
for (IssueLink link in linkMgr.getOutwardLinks(issue.id)) {
    def destIssue = link.getDestinationObject()
    // Does the name of the link match "Escalated" ?
    if (linkType.contains(link.issueLinkType.name)) {
        def destStatusObject = destIssue.getStatus()
	// Is the status of the linked issue "Display" ?
        if (destStatusObject.name == "Display") {
            // Prepare our input for the transition
            def issueInputParameters = issueService.newIssueInputParameters()
            issueInputParameters.with {
                setComment("Cancelled Vacation Request by HR")
                setSkipScreenCheck(true)
            }
            // Validate transitioning the linked issue to "Signs Needed"
            def validationResult = issueService.validateTransition(user, destIssue.id, 61, issueInputParameters)
            if (validationResult.isValid()) {
                // Perform the transition
                def issueResult = issueService.transition(user, validationResult)
                if (! issueResult.isValid()) {
                    log.warn("Failed to transition task ${destIssue.key}, errors: ${issueResult.errorCollection}")
                }
            } else {
                log.warn("Could not transition task ${destIssue.key}, errors: ${validationResult.errorCollection}")
            }
        } else {
            log.warn("Skipping link: ${link.issueLinkType.name} ${destIssue.key} ${destStatusObject.name} (wrong status)")
        }
    } else {
        log.warn("Skipping link: ${link.issueLinkType.name} ${destIssue.key} (wrong type)")
    }
}

2 answers

1 accepted

1 vote
Accepted answer

I got it to work here: 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issueService = ComponentAccessor.getIssueService()
//def linkType = ["relates to"]
// 6.x validateTransition wants a User, so we have to use getDirectoryUser()
// 7.x validateTransition wants an ApplicationUser, so remove the .getDirectoryUser() after we upgrade
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
// Look through the outward links
log.debug("commence for loop")
for (IssueLink link in linkMgr.getOutwardLinks(issue.id)) {
    log.debug("entered for loop")
    def destIssue = link.getDestinationObject()
    // Does the name of the link match "relates to" ?
   // if (linkType.contains(link.issueLinkType.name)) {
        log.debug("relates to is the link type")
        def destStatusObject = destIssue.getStatus()
 // Is the status of the linked issue "Display" ?
        if (destStatusObject.name == "Display") {
            // Prepare our input for the transition
            log.debug("Status == Display")
            def issueInputParameters = issueService.newIssueInputParameters()
            issueInputParameters.with {
                
            }
            // Validate transitioning the linked issue to "Signs Needed"
            def validationResult = issueService.validateTransition(user, destIssue.id, 61, issueInputParameters)
            if (validationResult.isValid()) {
                log.debug("Validation Result is valid")
                // Perform the transition
                def issueResult = issueService.transition(user, validationResult)
                if (! issueResult.isValid()) {
                    log.debug("Failed to transition task ${destIssue.key}, errors: ${issueResult.errorCollection}")
                }
            } else {
                log.debug("Could not transition task ${destIssue.key}, errors: ${validationResult.errorCollection}")
            }
        } else {
            log.debug("Skipping link: ${link.issueLinkType.name} ${destIssue.key} ${destStatusObject.name} (wrong status)")
        }
    }// else {
        //log.debug("Skipping link: ${link.issueLinkType.name} ${destIssue.key} (wrong type)")
   // }
//}
0 votes

The script looks fairly correct... I agree that having the logs would be helpful, because you'd be able to see whether or not the script is failing or even being ran at all. You can get the entire log file using directions here: https://confluence.atlassian.com/jira063/where-are-the-application-server-logs-683542381.html

You can also go to Built-in Scripts in JIRA and click "View Server Log Files".

After adding this post-function, did you make sure to put in the right order? Post-functions should generally be the last item on a transition:

Screen Shot 2017-07-10 at 9.40.01 AM.png

 

I placed the post funtion as the last item in the transition and nothing changed in terms of result. The Source issue status id goes from step 9 to 3 using tranision id 131. The linked issue that is displayed on the source ticket is "is caused by".

 

The linked issue needs to go from status id 4 to 5 using the transition id 61. The linked issue is displayed as "causes" on the linked ticket. 

I don't know if this extra information helps

 

I added a logger so I can see where the code reaches now and this is  my reslut. I also changed the script a little bit

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issueService = ComponentAccessor.getIssueService()
def linkType = ["Relates"]
// 6.x validateTransition wants a User, so we have to use getDirectoryUser()
// 7.x validateTransition wants an ApplicationUser, so remove the .getDirectoryUser() after we upgrade
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
// Look through the outward links
log.debug("commence for loop")
for (IssueLink link in linkMgr.getOutwardLinks(issue.id)) {
    log.debug("entered for loop")
    def destIssue = link.getDestinationObject()
    // Does the name of the link match "relates to" ?
    if (linkType.contains(link.issueLinkType.name)) {
        log.debug("relates to is the link type")
        def destStatusObject = destIssue.getStatus()
 // Is the status of the linked issue "Display" ?
        if (destStatusObject.name == "Display") {
            // Prepare our input for the transition
            log.debug("Status == Display")
            def issueInputParameters = issueService.newIssueInputParameters()
            issueInputParameters.with {
                setComment("Cancelled Vacation Request by HR")
                setSkipScreenCheck(true)
            }
            // Validate transitioning the linked issue to "Signs Needed"
            def validationResult = issueService.validateTransition(user, destIssue.id, 61, issueInputParameters)
            if (validationResult.isValid()) {
                log.debug("Validation Result is valid")
                // Perform the transition
                def issueResult = issueService.transition(user, validationResult)
                if (! issueResult.isValid()) {
                    log.debug("Failed to transition task ${destIssue.key}, errors: ${issueResult.errorCollection}")
                }
            } else {
                log.debug("Could not transition task ${destIssue.key}, errors: ${validationResult.errorCollection}")
            }
        } else {
            log.debug("Skipping link: ${link.issueLinkType.name} ${destIssue.key} ${destStatusObject.name} (wrong status)")
        }
    } else {
        log.debug("Skipping link: ${link.issueLinkType.name} ${destIssue.key} (wrong type)")
    }
}
2017-07-10 14:00:20,608 DEBUG [acme.CreateSubtask]: commence for loop
2017-07-10 14:00:20,608 DEBUG [acme.CreateSubtask]: entered for loop
2017-07-10 14:00:20,609 DEBUG [acme.CreateSubtask]: Skipping link: Relates OAG-38 (wrong type)

 

I changed the link to "relates to" and got this log

2017-07-10 14:01:48,811 DEBUG [acme.CreateSubtask]: commence for loop
2017-07-10 14:01:48,811 DEBUG [acme.CreateSubtask]: entered for loop
2017-07-10 14:01:48,812 DEBUG [acme.CreateSubtask]: relates to is the link type
2017-07-10 14:01:48,812 DEBUG [acme.CreateSubtask]: Status == Display
2017-07-10 14:01:48,851 WARN [contact.ContactSelectCF]:  ContactSelectCF getValueFromCustomFieldParams 
2017-07-10 14:01:48,851 WARN [contact.ContactSelectCF]:  ContactSelectCF getValueFromCustomFieldParams singleParam 11417
2017-07-10 14:01:48,851 WARN [contact.ContactSelectCF]:  ContactSelectCF getValueFromCustomFieldParams option 11417 : Not defined
2017-07-10 14:01:48,851 WARN [contact.ContactSelectCF]: ContactSelectCF validateFromParams crm org option null for OAG-39
2017-07-10 14:01:48,851 WARN [contact.ContactSelectCF]:  ContactSelectCF :: store put Not defined : 19342_customfield_11711

 

Was this script used on JIRA On-Prem or JIRA Cloud?

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Oct 31, 2018 in Marketplace Apps

Marketplace Spotlight: Zephyr

Hello Atlassian Community! Each month, we run a series of Spotlights to highlight Marketplace vendors and apps that our team thinks this Community would find valuable. In last month's Spotlig...

412 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you