jira server script runner
how to find and link current issue by common value of customfields automatically
Exmample: New and Old ticket in the project have common value of customfield. How to find Old ticket from New ticket and linked those, and update a couple fields in New ticket from Old automatically.
Is it achievable by script runner. Please help with a script
It can be made with scriptrunner, but maybe it's better to make with Jira Automation?
You won't need to write scripts, it can be done much easier.
The automation should be applied in Jira Server 8 environment.
I'm afraid it not possible to add automation.
Could you provide guidelines how to implement that?
or if it possible to provide same script example
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
The script was find in community as example doesn't work for me.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user. ApplicationUser
def issueManager = ComponentAccessor.getIssueManager()
def currentIssue = issueManager.getIssueObject("TEST-11")
//def currentIssue = event.getIssue()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()
def jqlParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() as ApplicationUser
def queryIssue
def ipVal = currentIssue.getCustomFieldValue(cfm.getCustomFieldObjectByName("Domino Aplicativo"))
def jql = jqlParser.parseQuery("${ipVal}
def results = searchProvider.search(jql, user, PagerFilter.getUnlimitedFilter())
results.getIssues().each {documentIssue ->
queryIssue = issueManager.getIssueObject(documentIssue.id)
if(queryIssue != null) {
linkManager.createIssueLink(currentIssue.Id, queryIssue.Id, 10309, 1, user )
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank in advance.
The script was detected in community as example doesn't work for me and it not complete all needs:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user. ApplicationUser
def issueManager = ComponentAccessor.getIssueManager()
def currentIssue = issueManager.getIssueObject("TEST-11")
//def currentIssue = event.getIssue()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()
def jqlParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() as ApplicationUser
def queryIssue
def ipVal = currentIssue.getCustomFieldValue(cfm.getCustomFieldObjectByName("Coomon field"))
def jql = jqlParser.parseQuery("${ipVal}
def results = searchProvider.search(jql, user, PagerFilter.getUnlimitedFilter())
results.getIssues().each {documentIssue ->
queryIssue = issueManager.getIssueObject(documentIssue.id)
if(queryIssue != null) {
linkManager.createIssueLink(currentIssue.Id, queryIssue.Id, 10309, 1, user )
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think, something like this, added to postfunction, will work
/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser) as JqlQueryParser
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
SearchService searchService = ComponentAccessor.getComponent(SearchService)
Issue issue = issue as Issue
// Set custom field name, you're looking
String fieldName = "Common Field"
CustomField cf = customFieldManager.getCustomFieldObject(fieldName)
def cfValue = issue.getCustomFieldValue(cf)
// Set link sequence and link type ID
Long sequence = 1L
Long linkType = 10000L
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// This JQL will look for issues with similar custom fields
String jql = "project = '${issue.getProjectObject().name}' AND key != ${issue.key} AND '${fieldName}' = '${cfValue}'"
Query query = jqlQueryParser.parseQuery(jql)
SearchResults search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
List<Issue> foundIssues = search.results
foundIssues.each { Issue foundIssue ->
ComponentAccessor.issueLinkManager.createIssueLink(issue.id, foundIssue.id, linkType, sequence, user)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
My pleasure Evgeniy!
Big thanks for my problem solving.
The script works great.
One more thing : As part of assignments How does update a couple fields in New ticket from Old automatically.
For example: Update custom TEXT field in New ticket called INFO with value from Old ticket TEXT field called the same INFO
Could you please to share same example too
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes, sure
/*
* Created 2023.
* @author Evgeniy Isaenkov
* @github https://github.com/Udjin79/SRUtils
*/
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.query.Query
JqlQueryParser jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser) as JqlQueryParser
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
SearchService searchService = ComponentAccessor.getComponent(SearchService)
IssueManager issueManager = ComponentAccessor.getIssueManager()
IssueIndexingService issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService)
MutableIssue issue = issue as MutableIssue
// Set custom field name, you're looking
CustomField cf = customFieldManager.getCustomFieldObject("Common Field")
CustomField cfToCopy1 = customFieldManager.getCustomFieldObject("Common Field 1")
CustomField cfToCopy2 = customFieldManager.getCustomFieldObject("Common Field 2")
def cfValue = issue.getCustomFieldValue(cf)
// Set link sequence and link type ID
Long sequence = 1L
Long linkType = 10000L
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// This JQL will look for issues with similar custom fields
String jql = "project = '${issue.getProjectObject().name}' AND key != ${issue.key} AND '${fieldName}' = '${cfValue}'"
Query query = jqlQueryParser.parseQuery(jql)
SearchResults search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())
List<Issue> foundIssues = search.results
if (foundIssues) {
Issue foundIssue = foundIssues.first()
def cfToCopy1Value = foundIssue.getCustomFieldValue(cfToCopy1)
def cfToCopy2Value = foundIssue.getCustomFieldValue(cfToCopy2)
ComponentAccessor.issueLinkManager.createIssueLink(issue.id, foundIssue.id, linkType, sequence, user)
issue.setCustomFieldValue(cfToCopy1, cfToCopy1Value)
issue.setCustomFieldValue(cfToCopy2, cfToCopy2Value)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
issueIndexingService.reIndex(issue)
}
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.
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.