Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

1 vote
Answer accepted
brbojorque Community Leader Dec 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.

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 Dec 26, 2019

@Xu Man ,

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.

Hi @Xu Man ,

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

@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 Jul 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!

@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 Jul 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
Community showcase
Published in Jira Software

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

4,623 views 22 33
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you