Forums

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

Trying to transition a list of issue with Scriptrunner's post function

support for vo March 17, 2021

Hello,

We are currently trying to do this : when we create an issue with a given summary, check the existing issues of the same issue type ("VO Skills") and transition them to "Inactive" (21) if they have the same summary.

We came up with this code, that is working perfectly in the script console (with a given issue) but not in the post function. Here is the code :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.workflow.JiraWorkflow;
import com.atlassian.jira.workflow.WorkflowManager;
import com.opensymphony.workflow.loader.ActionDescriptor
import com.opensymphony.workflow.loader.StepDescriptor

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser
def issueService = ComponentAccessor.getIssueService()

// here is the issue used for the tests in the script console
//def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("HR-138")


def workflowManager = ComponentAccessor.getWorkflowManager()
def workflow = ComponentAccessor.getWorkflowManager().getWorkflow(issue)

// code used to be sure the transition ID is the right one
/*def allactions = workflow.getAllActions()
log.debug("All actions ID and names" + allactions.collect {it.name+ ' '+ it.id})*/


def query = jqlQueryParser.parseQuery("project = HR and summary ~ \"" + issue.summary + "\"")
log.debug("JQL => project = HR and summary ~ \"" + issue.summary + "\"")

def search = searchService.search(currentUser, query, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${search.total}")

search.results.each { documentIssue ->
log.debug(documentIssue.key)
log.debug(issue.getStatus().getName())

def currentIssue = issueManager.getIssueObject(documentIssue.id)
log.debug(currentIssue.summary)

def transitionValidationResult
def transitionResult
def actionID = 21
def customFieldManager = ComponentAccessor.getCustomFieldManager()
log.debug("The issue type is: " + issue.getIssueType().name)

if (issue.getIssueType().name == "VO Skills") {

transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionID,new IssueInputParametersImpl())

if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)
if (transitionResult.isValid())
{ log.debug("Transitioned issue " + issue.key + " through action " + actionID) }
else
{ log.debug("Transition result is not valid") }
log.debug("The transitionValidation is valid")
}
else {
log.debug("The transitionValidation is not valid")
}
}
}

 

And here is an output example with the only difference between script console and post-function being the last line saying "The transitionValidation is not valid" when it's in post-function instead of the lines saying it transitioned and it's valid.

2021-03-17 14:44:29,835 DEBUG [runner.ScriptBindingsManager]: JQL => project = HR and summary ~ "TOTO toto"
2021-03-17 14:44:29,841 DEBUG [runner.ScriptBindingsManager]: Total issues: 7
2021-03-17 14:44:29,842 DEBUG [runner.ScriptBindingsManager]: HR-138
2021-03-17 14:44:29,842 DEBUG [runner.ScriptBindingsManager]: Active
2021-03-17 14:44:29,843 DEBUG [runner.ScriptBindingsManager]: TOTO toto
2021-03-17 14:44:29,843 DEBUG [runner.ScriptBindingsManager]: The issue type is: VO Skills
2021-03-17 14:44:29,952 DEBUG [runner.ScriptBindingsManager]: Transitioned issue HR-138 through action 21
2021-03-17 14:44:29,952 DEBUG [runner.ScriptBindingsManager]: The transitionValidation is vali

 What could be the reason for that ?

1 answer

1 accepted

1 vote
Answer accepted
Nic Brough -Adaptavist-
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.
March 17, 2021

You can't transition an issue while it is already being transitioned (and "create" is a transition)  The validation code you've got fails because you've put the issue into an unknown state, effectively in both transitions.

You can transition different issues from a post-function, but not the current issue.   You could try a listener, or, if you have one of the scripting apps or automation apps, they have "fast track" transitions that enable you to chain transitions together (conditionally)

support for vo March 18, 2021

And you gave me the solution ! I was trying to transition different issues, not the one currently being created, but now I see that I was using

transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionID,new IssueInputParametersImpl())

with "issue.id" and not "documentIssue.id", and doing the same mistake later, so I was only trying to transition, every time, the issue being created.

Thanks !

Suggest an answer

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

Atlassian Community Events