Hello everyone,
We have a custom field where a URL is entered. Parts of this URL will change in the future. Is there a Scriptrunner solution to go over each issue, looking into a custom field. If the field value contains a particular string, replace that part of the value with a new string value.
An Example.
Issues that have in custom field: CF, value: "https://abc.d.com/browse/ABC-1" should be change by the script to: "https://xyz.d.net/browse/ABC-1"
Issues that haven't a Substring "abc.d.com" in the custom field should be skippted.
Thanks in Advance.
Hi, @Andreas Lorz
Yes, it's possible. It's not very complicated case.
You need to select issues by JQL, which contain value in required custom field.
Then, for each of issue replace part of string with required text.
You can use something like this:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
IssueManager issueManager = ComponentAccessor.getIssueManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
String jqlQuery = "project = YOUR_PROJECT_KEY AND \"URL Custom Field Name\" IS NOT EMPTY" // Update JQL accordingly
def issues = Issues.search(jqlQuery)
CustomField customField = customFieldManager.getCustomFieldObjectByName('URL') // Update to your field name
String textToReplace = "part_of_string_to_replace"
String replacementText = "new_text"
issues.each { issueObject ->
MutableIssue issue = Issues.getByKey(issueObject.key) as MutableIssue
String customFieldValue = issue.getCustomFieldValue(customField)
if (customFieldValue && customFieldValue.contains(textToReplace)) {
String updatedValue = customFieldValue.replace(textToReplace, replacementText)
issue.setCustomFieldValue(customField, updatedValue)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
log.info("Updated issue ${issue.key} with new value: $updatedValue")
}
}
log.info("Completed updating issues.")
Thanks, the script works like a charm and is exactly what I needed.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
ScriptRunner can easily work on every issue in a query result: https://library.adaptavist.com/entity/update-cf-values-all-issues
Matching one (or more) URL strings in a custom field will require some care, since regular expressions are a language unto themselves. This post on Stack Overflow has some examples and references:
https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url
Once you tell it what a URL looks like, ScriptRunner can use matches for flow control:
https://docs.adaptavist.com/sr4js/7.9.0/features/workflows/conditions/regular-expression-condition
...and then do the replacement with Groovy's "replaceAll()" method:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the explanation and the resources with the background knowledge. This is exactly what I was missing.
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.