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 ?
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)
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 !
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.