Our teams will paste several Jira tickets link in description field to indicate related changes. But we found the input tickets could not be linked without add the links manually one by one. Is it possible to automatically find the links included in description and configure them automatically as issue links?
This is possible by using ScriptRunner, I will provide the Pseudo code.
Now, there are nuances here that needs to be resolved first.
By answering the questions above you should narrow down the requirements accordingly.
How could I use ScriptRunner in my Jira projects? And how could I configure it?
Thank you very much.
Best regards,
Catherine
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@[deleted] ,
Give me few hours, I will write the script for you.
Also keep in mind you need to install the ScriptRunner add-on in your Jira instance.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @[deleted] ,
Add this script to the ScriptRunner Script Listener.
Specify the Project where the script will trigger and make sure that the event is Issue Updated.
import com.atlassian.jira.component.ComponentAccessor
def ALLOWED_ISSUETYPES = ['Bug', 'Story', 'Task']
def LINKID = 10001 // Clone link type, Change this if you want different link type
def SEQUENCE = 10001 // Don't change the default value
def PROJECT_KEY = 'DSWCPA'
def PATTERN = /$PROJECT_KEY-[0-9]/
if(!issue.getIssueType().getName() in ALLOWED_ISSUETYPES){
return
}
if(event?.getChangeLog()?.getRelated('ChildChangeItem').find {it.field == 'description'}){
def matcher = issue.description =~ PATTERN
matcher.find()
if(matcher.size()){
def destIssue
def applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
matcher.each{
try{
destIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it)
ComponentAccessor.getIssueLinkManager().createIssueLink(issue.id, destIssue.id, LINKID, SEQUENCE, applicationUser)
}catch(Exception ex){
log.debug("Error linking the issue.")
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@brbojorque Because our teams are sharing Jira instance with some other projects, our Jira admin hope to use the script more widely for all projects on this instance and all issue types. Could we modify the script as below and just remove the lines about Issue type and project key? Or do we need to modify the script so that it matches all issue types and Jira projects on this Jira instance? Thank you very much.
import com.atlassian.jira.component.ComponentAccessor
def LINKID = 10001 // Clone link type, Change this if you want different link type
def SEQUENCE = 10001 // Don't change the default value
def PATTERN = /$PROJECT_KEY-[0-9]/
if(!issue.getIssueType().getName() in ALLOWED_ISSUETYPES){
return
}
if(event?.getChangeLog()?.getRelated('ChildChangeItem').find {it.field == 'description'}){
def matcher = issue.description =~ PATTERN
matcher.find()
if(matcher.size()){
def destIssue
def applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
matcher.each{
try{
destIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it)
ComponentAccessor.getIssueLinkManager().createIssueLink(issue.id, destIssue.id, LINKID, SEQUENCE, applicationUser)
}catch(Exception ex){
log.debug("Error linking the issue.")
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Catherine,
Just remove the following line and it should be good to go.
if(!issue.getIssueType().getName() in ALLOWED_ISSUETYPES){
return
}
And change the pattern definition, like so below.
def PATTERN = issue.getProjectObject().getKey()
I have not tested this but I imagine it should work, let me know if it won't.
Also, I would appreciate it if you accept my answer.
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@brbojorqueSorry that my Jira system admin told me the script is not working. Could you please help to check whether this script is completed and verified it's working? Thank you very much.
import com.atlassian.jira.component.ComponentAccessor
def LINKID = 10300 // Breaks down link type, Change this if you want different link type

def SEQUENCE = 10001 // Don't change the default value

def PATTERN = issue.getProjectObject().getKey()


def PROJECT_KEY = 'DSWCCLOUD’
def PATTERN = /$PROJECT_KEY-[0-9]/
if(event?.getChangeLog()?.getRelated('ChildChangeItem').find {it.field == 'description'}){

def matcher = issue.description =~ PATTERN

matcher.find()

if(matcher.size()){

def destIssue

def applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

matcher.each{

try{

destIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it)
ComponentAccessor.getIssueLinkManager().createIssueLink(issue.id, destIssue.id, LINKID, SEQUENCE, applicationUser)
}
catch(Exception ex){

log.debug("Error linking the issue.")

}

}

}

}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Catherine,
Here's the full working code, I have tested this and it works fine, take note that it is case sensitive and it will only accept capitalized letters e.g ISSUE-123.
It should work in all cases as long as the issue exists in the same instance.
import com.atlassian.jira.component.ComponentAccessor
def LINKID = 10001 // Clone link type, Change this if you want different link type
def SEQUENCE = 10001 // Don't change the default value
def PATTERN = /(?:\s|^)([a-zA-Z]+-[0-9]+)(?=\s|$)/
if(event?.getChangeLog()?.getRelated('ChildChangeItem').find {it.field == 'description'}){
def matcher = issue.description =~ PATTERN
matcher.find()
if(matcher.size()){
def destIssue
def applicationUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
matcher.each{
try{
destIssue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(it.first().trim())
ComponentAccessor.getIssueLinkManager().createIssueLink(issue.id, destIssue.id, LINKID, SEQUENCE, applicationUser)
}catch(Exception ex){
log.debug("Error linking the issue.")
}
}
}
}
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.