How to automatically link the Jira ticket if we paste Jira link in description field

Deleted user December 17, 2019

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?

 

 

1 answer

1 accepted

3 votes
Answer accepted
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 17, 2019

This is possible by using ScriptRunner, I will provide the Pseudo code.

  • Listen to the changes in the Description
  • Find specific keyword by using Regex
  • After finding the links  then automatically link it to Jira

Now, there are nuances here that needs to be resolved first.

  • What link it should use?
  • What keyword it should listen to?
  • What are the valid issuetype it  should trigger the automation?

By answering the questions above you should narrow down the requirements accordingly.

Deleted user December 20, 2019

How could I use ScriptRunner in my Jira projects? And how could I configure it?

  • What link it should use?    - Jira issue link, type is Bug, Story, Task
  • What keyword it should listen to? - If it's configured by project, maybe we could use project key as keyword.
  • What are the valid issuetype it  should trigger the automation?  - Story, Bug or Task.
  • story link.jpg

Thank you very much.

 

Best regards,

Catherine

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 26, 2019

@[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.

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 26, 2019

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.")
}
}
}
}
Deleted user July 13, 2020

@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.")
}
}
}
}

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 14, 2020

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!

Deleted user July 23, 2020

@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.")

}

}

}

}
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 24, 2020

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.")
}
}
}
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events