Now that I have gotten my code to work without error, my last issue I am seeing is that when a copy the version value from a single version picker custom field to a linked issue's Affect/s Version field, I show that the value should have been set, but unfortunately I am not seeing it persist.
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkType
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.fields.IssueLinksSystemField
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.comments.CommentManager //testing only
import com.atlassian.jira.user.ApplicationUser //testing only
import com.atlassian.jira.project.version.Version
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().loggedInUser //testing only
try
{
def issue = event.issue as MutableIssue
// define issuetype we will look for the even on
def isAnEscalation = issue.getIssueType()?.name == "Escalation";
// Is this an Escalation issuetype? If not, return
if (!isAnEscalation)
{
return;
}
// Defining links
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
// Defining Product Version and Affect(s) Version
def productVersionCf = customFieldManager.getCustomFieldObject(11622) //Product Version
def productVersionValue = issue.getCustomFieldValue(productVersionCf) //as Version
if (isAnEscalation) //is this an escalation issuetype?
{
List<String> linkTypes = ["Root Cause", "Fixes"] //The list of Link Type you want this script to work on
def linkedIssues = ComponentAccessor.issueLinkManager
.getInwardLinks(issue.id)
.findAll { it.issueLinkType.name in linkTypes
}
linkedIssues.each {
def linkedIssue = it.sourceObject as MutableIssue
log.info "Attempting to set the affectedversion field on $linkedIssue.key to $productVersionValue which is an object of type ${productVersionValue.getClass()}"
linkedIssue.setAffectedVersions([productVersionValue]) //set affected version/s
log.info "$linkedIssue.key affectedVersion is now: $linkedIssue.affectedVersions"
log.info "Attempting to update $linkedIssue.key after setting version"
def IssueManager issueManager = ComponentAccessor.getIssueManager()
// issueManager.updateIssue(currentUser,issue,EventDispatchOption.DO_NOT_DISPATCH, false)
issueManager.updateIssue(currentUser,issue,EventDispatchOption.ISSUE_UPDATED,false)
log.info "$linkedIssue.key update complete"
}
}}
catch(Exception ex)
{
log.info("Error Message, Listener - ES_Version_Copy - Exception:"+ex.getStackTrace());
// log.info("Stack Trace :"+ex.getStackTrace());
}
catch(Throwable ex)
{
log.info("Error Message, Listener - ES_Version_Copy - Throwable:"+ex.getStackTrace());
// log.info("Stack Trace :"+ex.getStackTrace());
}
catch(SocketTimeoutException ex)
{
log.info("Error Message, Listener - ES_Version_Copy - socket timeout exception:"+ex.getStackTrace());
// log.info("Stack Trace :"+ex.getMessage());
}
From looking at my info.log outputs things look to have run successfully.
2021-01-19 14:23:24,580 INFO [runner.ScriptBindingsManager]: Attempting to set the affectedversion field on XXXX-177104 to [5.3.1] which is an object of type class java.util.ArrayList
2021-01-19 14:23:24,581 INFO [runner.ScriptBindingsManager]: XXXX-177104 affectedVersion is now: [[5.3.1]]
2021-01-19 14:23:24,581 INFO [runner.ScriptBindingsManager]: Attempting to update XXXX-177104 after setting version
2021-01-19 14:23:24,581 INFO [runner.ScriptBindingsManager]: XXXX-177104 update complete
Is there something I am missing here as to why things look to have gone perfectly fine, but the value does not persist in the Affect/s Version field of the linked issue? I verified this is not a permissions issue as I am the user shown in the logs and can manually update the field myself on the linked issue.
You are attempting to update the wrong issue I think
linkedIssue.setAffectedVersions([productVersionValue])
[...]
issueManager.updateIssue(currentUser,issue,EventDispatchOption.ISSUE_UPDATED,false)
You set the value of "linkedIssue", but then store the "issue" object.
Also, keep in mind that you will need to manually index the linkedissue after updating, otherwise, you won't be able to search by the new affects version until something else indexes the issue
Thanks Peter. When I changed the value from issue to linkedIssue in the call to store the value I get an exception error for that line of code, which is why I kept is as issue.
I cannot make heads or tails of what the exception is saying, is there any useful that can be gleaned from the large error output that could tell me why
issueManager.updateIssue(currentUser,linkedIssue,EventDispatchOption.ISSUE_UPDATED,false)
is giving me a problem?
As for reindexing, I have one of your previous recommendations to another user on performing that, I just had not added it into code just yet until I can at least see a value persist.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ok, looking at this:
2021-01-19 14:23:24,580 INFO [runner.ScriptBindingsManager]: Attempting to set the affectedversion field on XXXX-177104 to [5.3.1] which is an object of type class java.util.ArrayList
It appears that productVersionValue is already an array. So we don't want to put it inside another array.
So using this instead (back to something simliar to what you had before):
linkedIssue.setAffectedVersions(productVersionValue)
Also, I don't see your issueManager defined anywhere. So you may want to expand that call and fully qualify it:
ComponentAccessor.issueManager.updateIssue(currentUser,linkedIssue,EventDispatchOption.ISSUE_UPDATED,false)
And finally... any chance that issue and linked issues are from different projects?
Version entities are unique to each project. If there is a chance they will be different, then you need to be able to find matching Version entities based on label/name and ensure that they are consistent (by training/process/convention).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi peter, you are correct, these are two different projects, though the version I am testing this on exists in both projects and the projects use version synchronizer to keep version lists in sync (so far at least). When you say version entities may be different, should that be an issue if the version lists are synced and should share the same values?
I will check into trying without [ ] on the version set.
issueManager is defined, but just buried a little...
log.info "Attempting to update $linkedIssue.key after setting version"
def IssueManager issueManager = ComponentAccessor.getIssueManager()
// issueManager.updateIssue(currentUser,issue,EventDispatchOption.DO_NOT_DISPATCH, false)
issueManager.updateIssue(currentUser,issue,EventDispatchOption.ISSUE_UPDATED,false)
log.info "$linkedIssue.key update complete"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I mean that even if they look the same, in the db they are 2 separate things each with unique IDs.
So you'll need to find the match.
Something like this inside the linkedIssues.each{}
def versionManager = ComponentAccessor.versionManager
linkedIssues.each {
def linkedIssue = it.sourceObject as MutableIssue
def newProductVersions = productVersionValue.collect{version->
versionManager.getVersion(linkedIssue.projectObject.id, version.name)
}
linkedIssue.setAffectedVersions(newProductVersions )
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you Peter, that solved my issues and was able to add in my code for appending the version as well.
One quick question, even though the code snippet you provided worked, any reason it gives me this error message? Is there anything you can point me to resolve it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try
def newProductVersions = productVersionValue.collect{Version version->
versionManager.getVersion(linkedIssue.projectObject.id, version.name)
}
or
def newProductVersions = productVersionValue.collect{version->
versionManager.getVersion(linkedIssue.projectObject.id, version.name as String)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.