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 find all workflows which are using postfunctions of a plugin

We need to upgrade a JIRA, but one of the plugins is not compatible.
How can we find all the workflows which are using the workflow functions from this plugin ?

7 answers

1 accepted

You could use the Script Runner plugin to search for more detailed uses. I wrote a script to create a list which than can be used e.g. in Excel for further investigations.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import java.util.regex.Matcher

String searchText = 'com.googlecode.jsu.workflow.'

WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager()
Collection<JiraWorkflow> workflows = workflowManager.getWorkflows()

String result = "Workflow;Function;Type;Action;Step\r\n"

workflows.each{workflow ->
    workflowXml = workflow.descriptor.asXML()
    Matcher m = workflowXml =~ /${searchText}/
    if (m.find()) {
        String workflow_name = "${workflow.name}${if(!workflow.isActive()){"(inactive)"} else ''}${if(workflow.isDraftWorkflow()){"(draft)"} else ''}"
        def wf = new XmlParser().parseText(workflowXml)
        List<Node> wf_flat = wf.depthFirst()
        wf_flat.each{
            if (it.text() ==~ /.*${searchText}.*/){
                result += "$workflow_name;${getNodeInfo(it,searchText)}\r\n"
            }
        }
    }
}
result

String getNodeInfo(Node n, String search) {
    String nodetext = ''
    nodetext += "${n.text() - search}"
    def p = n.parent()
    while (p) {
        switch (p.name()) {
            case 'post-functions':
                nodetext += ";post-function"; break
            case 'validators':
                nodetext += ";validator"; break
            case 'conditions':
                nodetext += ";condition"; break
            case 'action':
                nodetext += ";${p.attribute('name')} (${p.attribute('id')})"; break
            case 'step':
                nodetext += ";${p.attribute('name')} (${p.attribute('id')})"; break
            case 'global-actions':
                nodetext += ";GLOBAL"; break
            case 'initial-actions':
                nodetext += ";INITIAL"; break

        }
        p = p.parent()
    }
    return nodetext
}

You have to adapt the searchText to match the plugin.

Henning

 

That's a cool approach, not requiring to access JIRA 'behind the scenes'
Certainly a candidate for a build-in script (IMO)

Henning - this a beautiful solution. Thanks for sharing!

@Henning Tietgens  – quick question. in the getNodeInfo method – why are you using the minus operator here:

 nodetext += "${n.text() - search}"

 

It looks like it's removing the search term from nodetext...?  Not sure why you are doing that. Any tips / insight much appreciated!

You're right. It's only for shorter list entries.

Hi @Henning Tietgens

This is brilliant!  I am planning a Jira upgrade with numerous plugins and numerous workflows.  Remembering what post-functions I used and where I used them is now infinitely easier for doing my testing.  Thank you!

Hi,

here is a refined version of my script.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import java.util.regex.Matcher

String searchText = 'customfield'

WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager()
Collection<JiraWorkflow> workflows = workflowManager.getWorkflows()

String result = "Workflow;Function;Type;Action;Source-Step<br>"

workflows.each{workflow ->
def workflowXml = workflow.descriptor.asXML()
Matcher m = workflowXml =~ /${searchText}/
if (m.find()) {
String workflow_name = "${workflow.name}${if(!workflow.isActive()){"(inactive)"} else ''}${if(workflow.isDraftWorkflow()){"(draft)"} else ''}"
def wf = new XmlParser().parseText(workflowXml)
List<Node> wf_flat = wf.depthFirst()
wf_flat.each{
if (it.text() ==~ /.*${searchText}.*/){
def nodeInfo = getNodeInfo(it,searchText)
if (nodeInfo){
result += "$workflow_name;$nodeInfo<br>"
}
}
}
}
}
result

String getNodeInfo(Node n, String search) {
String nodetext = ''
def valid_action = false
def valid_function = false
def valid_step = false
if (n.name() == 'function') {
def m = n.value() =~ /[^\[{]*/ + search + /[^\]}]*/
if (m) {
nodetext += m[0].toString() + ";"
}
def p = n.parent()
while (p) {
switch (p.name()) {
case 'post-functions':
valid_function = true
nodetext += "post-function;"; break
case 'validators':
valid_function = true
nodetext += "validator;"; break
case 'conditions':
valid_function = true
nodetext += "condition;"; break
case 'action':
valid_action = true
nodetext += "${p.attribute('name')} (${p.attribute('id')});"; break
case 'step':
valid_step = true
nodetext += "${p.attribute('name')} (${p.attribute('id')})"; break
case 'global-actions':
valid_step = true
nodetext += "GLOBAL"; break
case 'initial-actions':
valid_step = true
nodetext += "INITIAL"; break
}
p = p.parent()
}
}
if (valid_action && valid_function && valid_step) {
return nodetext
} else {
return ""
}
}

Dave, please try this.

Henning

Hi Henning

Many thanks for all your efforts

I'm getting the error shown below

error.PNGThis links https://community.atlassian.com/t5/Jira-questions/Scriptrunner-for-jira-Copy-Project-Error-Timeout-No-stack-trace/qaq-p/209892 says this can be expected and the routune still runs. If so, is there a directory location  ca search for the output?

Regards

Dave  

In this case you have to replace the line 

result += "$workflow_name;$nodeInfo<br>"

with

log.error "$workflow_name;$nodeInfo"

Or you can add this line below the above line if you want both outputs. This way the output is in the atlassian-jira.log (you can view the location of the atlassian-jira.log in the 'File Paths' section of the system information page.).

Henning

Many thanks Henning, that seemed to work

All the best

Dave

Sorry Henning, but it seems to be case sensitive as is correctly matching the lowercase "customfield"  text. 

When I try to run for "CustomField" it runs iis a split second and does not produce any output.

Any idea?

Regards

Dave

Hi Dave,

Yes, the search is case sensitive. It's searching for workflow postfunctions, conditions and validators from specific plugins by parsing the XML output of the workflow.

If you know what you are searching for and you know a workflow which uses the functionality you can export this workflow as XML and try to find the specific identifier of it. This identifier can than be used in my script to find all workflows using it.

What are you searching for while searching for "CustomField"?

Best regards,
Henning

Hi Henning,

I'm trying to identify all references to a custom fields that use the native number extension (e.g. customFieldManager.getCustomFieldObject(11203L)).

In your script I am using:

String searchText = 'getCustomFieldObject'

but all I'm getting back in a sub-second response In the Results tab is 

Workflow;Function;Type;Action;Source-Step

Because I've have now also searched the XML export with no luck I presume the problem is that the code is references within Post Functions the ScriptRunner workflow function - Custom script post-function (inline script).

Any idea how to search through this?

Regards

Dave

Hi Dave, the scriptrunner parameters are base64 encoded since 4.3.3, see https://scriptrunner.adaptavist.com/4.3.3/jira/releases/current-release.html#_base64_encoding_for_workflow_function_arguments. On this page is a link to a script to decode workflow XMLs containing scriptrunner base64 encoded parameters. You could try to incorporate this script into my script to decode all parameters after getting the XML and before searching, between those lines.

    def workflowXml = workflow.descriptor.asXML()
Matcher m = workflowXml =~ /${searchText}/

Henning

Like Damien Davis likes this

Hi @Henning Tietgens ,

Good day! Just want to ask why is that only post-function returns onto result tab?

Hi Alvin,

It should not. Maybe your search term is to specific?

Henning

Hi @Henning Tietgens ,

I'm using this:

String searchText = 'com.innovalog.jmwe.jira-misc-workflow-extensions'

and still no validators even if I put one validator just to check. It returns post-functions perfectly but not the validators. 

Results:

Sample project - Subtask (Simplified);com.innovalog.jmwe.jira-misc-workflow-extensionsCopyFieldValueFromParent-function;post-function;Create (1);INITIAL

Sample Project - Subtask (Simplified) v1(inactive);com.innovalog.jmwe.jira-misc-workflow-extensionsCopyFieldValueFromParent-function;post-function;Create (1);INITIAL

can you suggest? Thank you!

Mmmh. Maybe you could try to export the workflow with the condition as XML and take a look into the xml file to determine which search string should match.

Hi @Henning Tietgens ,

Thank you for your very quick response, even tho I export the workflow and look for the validator descriptor, it does not display on the script console Results, it just return:

Workflow;Function;Type;Action;Source-Step

Do we have any other work-around here? Thank you!

Best Regards,
Alvin

Could you try my new answer? I hope this helps. Be sure to use the App Key for your app (could be found by expanding the app infos in "Manage Apps").

Best regards,
Henning

Hi @Henning Tietgens ,

Thank you for keeping an extra eye on this problem, but there is no workflow step returned.

Workflow;Function;Type;Action;Source-Step

Customfields
Calculated Date-Time Field (JWT Migration) (Calculated Date-Time Field (by JWT), com.atlassian.jira.plugin.system.customfieldtypes:datetimerange (Allow searching for a date and time that is between two other dates and times))
Calculated Text Field (JWT Migration) (Calculated Text Field (by JWT), com.atlassian.jira.plugin.system.customfieldtypes:textsearcher (Search for values using a free text search.))

If you are sure, you use the correct App Key for the script I'm out of ideas. In your first comment you wrote the key for "Jira Misc Workflow Extensions", now it's "Jira Workflow Toolbox", so I'm not sure if everything matches (is the validator from "Jira Workflow Toolbox"?).

Henning

I love it - made my day

We are currently using a query on the database, but I'm wondering if there is another approach.
The query is something like

select workflowname
from jira.jiraworkflows
where descriptor like '%iamhuy%'

Hi,

Another updated version, now building the search string from the modules used by the app identified by an app key. Reporting used custom fields, too.

package eventim.scripts

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.plugin.workflow.AbstractWorkflowModuleDescriptor
import com.atlassian.jira.workflow.JiraWorkflow
import com.atlassian.jira.workflow.WorkflowManager
import com.atlassian.plugin.PluginAccessor

import java.util.regex.Matcher

String appKey = "com.innovalog.jmcf.jira-misc-custom-fields"
String result = ""

PluginAccessor pluginAccessor = ComponentAccessor.getPluginAccessor()
WorkflowManager workflowManager = ComponentAccessor.getWorkflowManager()
def p = pluginAccessor.getPlugin(appKey)
if (p) {
def modules = p.getModuleDescriptors()
def implementationClasses = modules.findAll{
it instanceof AbstractWorkflowModuleDescriptor
}.collect{
it as AbstractWorkflowModuleDescriptor
}*.implementationClass.name
def searchText = implementationClasses.join('|')
log.debug "searchText: $searchText"
Collection<JiraWorkflow> workflows = workflowManager.getWorkflows()

result = "Workflow;Function;Type;Action;Source-Step<br>"

workflows.each{workflow ->
def workflowXml = workflow.descriptor.asXML()
Matcher m = workflowXml =~ /${searchText}/
if (m.find()) {
String workflow_name = "${workflow.name}${workflow.isActive()?"":"(inactive)"}${workflow.isDraftWorkflow()?"(draft)":""}"
log.debug "Found: $workflow_name"
def wf = new XmlParser().parseText(workflowXml)
List<Node> wf_flat = wf.depthFirst()
wf_flat.each{ node ->
if (implementationClasses.contains(node.text())) {
log.debug "Found node $node"
def implementationClassName = implementationClasses.find{it == node.text()}
def nodeInfo = getNodeInfo(node, implementationClassName)
if (nodeInfo) {
result += "$workflow_name;$nodeInfo<br>"
}
}
}
}
}

result += "<br>Customfields<br>"

def customfieldmanager = ComponentAccessor.customFieldManager
result += customfieldmanager.customFieldObjects.findAll{
it.customFieldType.key ==~ appKey + /.*/ ||
it.customFieldSearcher?.descriptor?.toString() ==~ appKey + /.*/
}.collect{
"$it.name ($it.customFieldType.name, ${it.customFieldSearcher?.descriptor})"
}.join("<br>")

}
result

static String getNodeInfo(Node n, String implementationClassName) {
String nodetext = ''
def valid_action = false
def valid_function = false
def valid_step = false
n = n.parent()
if (n.name() in ['function','condition','validator']) {
nodetext += implementationClassName + ";"
def p = n.parent()
while (p) {
switch (p.name()) {
case 'post-functions':
valid_function = true
nodetext += "post-function;"; break
case 'validators':
valid_function = true
nodetext += "validator;"; break
case 'conditions':
valid_function = true
nodetext += "condition;"; break
case 'action':
valid_action = true
nodetext += "${p.attribute('name')} (${p.attribute('id')});"; break
case 'step':
valid_step = true
nodetext += "${p.attribute('name')} (${p.attribute('id')})"; break
case 'global-actions':
valid_step = true
nodetext += "GLOBAL"; break
case 'initial-actions':
valid_step = true
nodetext += "INITIAL"; break
}
p = p.parent()
}
}
if (valid_action && valid_function && valid_step) {
return nodetext
} else {
return ""
}
}

Have fun,
Henning

Hi @Henning Tietgens ,

Thank you for keeping an extra eye on this problem, but there is no workflow step returned, only custom fields

Workflow;Function;Type;Action;Source-Step

Customfields
Calculated Date-Time Field (JWT Migration) (Calculated Date-Time Field (by JWT), com.atlassian.jira.plugin.system.customfieldtypes:datetimerange (Allow searching for a date and time that is between two other dates and times))
Calculated Text Field (JWT Migration) (Calculated Text Field (by JWT), com.atlassian.jira.plugin.system.customfieldtypes:textsearcher (Search for values using a free text search.))

I updated the script again to look for the implementation class to find workflow functions. Please try again.

Hi @Henning Tietgens ,

Good day! Sorry to disturb you but still it does not return the validators, only post functions, I have tried to download xml file and search for validators, I found com.innovalog.jmwe.plugins.validators.FieldRequiredValidator and use it on appKey. 

Still returning null as result. Have you tried to check validators also using the script? Thank you!

Best Regards,
Alvin

Hi Alvin,

com.innovalog.jmwe.plugins.validators.FieldRequiredValidator is not the appKey. Please go to Manage Apps in Jira and click on the corresponding app (plugin). There you could see the app key. Maybe it's com.innovalog.jmwe but this is just a guess.

E.g. this is for Jira Toolkit Plugin

Capture 2019-06-18_10-28-49_AM.pngBest regards,
Henning

Hi @Henning Tietgens ,

Thank you for that, I have used it before but it only returns post-functions, not the validators.

Could you please add

import org.apache.log4j.Level
log.setLevel(Level.DEBUG)

after the imports at the beginning of the script and tell me the log output and the validator part of the xml export of the workflow?

If you want to limit the output to your test workflow you could add

workflows.findAll{it.name == 'Testworkflow'}.each{workflow ->

to the corresponding line (replacing Testworkflow with the name of the workflow).

Henning 

2019-06-18 20:03:42,307 DEBUG [runner.ScriptRunnerImpl]: searchText: com.fca.jira.plugins.workflowToolbox.CascadeSelectComparatorCondition
|com.fca.jira.plugins.workflowToolbox.IfMatchesRegexpCondition|com.fca.jira.plugins.workflowToolbox.AllowedLinksCondition
|com.fca.jira.plugins.workflowToolbox.AnyIssueCondition|com.fca.jira.plugins.workflowToolbox.MathExpressionsCondition
|com.fca.jira.plugins.workflowToolbox.ParsedTextsCondition
|com.fca.jira.plugins.workflowToolbox.ExceptAssigneeCondition
|com.fca.jira.plugins.workflowToolbox.ExceptReporterCondition
|com.fca.jira.plugins.workflowToolbox.NotInProjectRoleCondition
|com.fca.jira.plugins.workflowToolbox.OnlyUserInCustomFieldCondition
|com.fca.jira.plugins.workflowToolbox.ExceptUserInCustomFieldCondition
|com.fca.jira.plugins.workflowToolbox.UserInFieldInRolCondition
|com.fca.jira.plugins.workflowToolbox.InitializedCondition|com.fca.jira.plugins.workflowToolbox.ProjectPropertyCondition
|com.fca.jira.plugins.workflowToolbox.AllowedSubtasksCondition
|com.fca.jira.plugins.workflowToolbox.JQLCondition|com.fca.jira.plugins.workflowToolbox.SoftwareTriggeredCondition
|com.fca.jira.plugins.workflowToolbox.BulkOperationCondition
|com.fca.jira.plugins.workflowToolbox.IfMatchesRegexpValidator
|com.fca.jira.plugins.workflowToolbox.AllowedLinksValidator|com.fca.jira.plugins.workflowToolbox.AnyIssueValidator|com.fca.jira.plugins.workflowToolbox.MathExpressionsValidator|com.fca.jira.plugins.workflowToolbox.ParsedTextsValidator|com.fca.jira.plugins.workflowToolbox.UserInFieldInRolValidator|com.fca.jira.plugins.workflowToolbox.InitializedValidator|com.fca.jira.plugins.workflowToolbox.ProjectPropertyValidator|com.fca.jira.plugins.workflowToolbox.AllowedSubtasksValidator|com.fca.jira.plugins.workflowToolbox.JQLValidator|com.fca.jira.plugins.workflowToolbox.CopyParsedTextFunction|com.fca.jira.plugins.workflowToolbox.MathCalculatorFunction|com.fca.jira.plugins.workflowToolbox.ConditionalValueSettingFunction|com.fca.jira.plugins.workflowToolbox.AddOptionToSelectListFunction|com.fca.jira.plugins.workflowToolbox.AddOptionToCascadingSelectFunction|com.fca.jira.plugins.workflowToolbox.FormatTextFunction|com.fca.jira.plugins.workflowToolbox.RegularExpressionRendererFunction|com.fca.jira.plugins.workflowToolbox.CommentAdderFunction|com.fca.jira.plugins.workflowToolbox.AddRemoveWatcherFunction|com.fca.jira.plugins.workflowToolbox.IssueLinkCreatorFunction|com.fca.jira.plugins.workflowToolbox.IssueLinkBreakerFunction|com.fca.jira.plugins.workflowToolbox.WriteLinkedIssuesFunction|com.fca.jira.plugins.workflowToolbox.ReadLinkedIssuesFunction|com.fca.jira.plugins.workflowToolbox.SetUserPropertyFunction|com.fca.jira.plugins.workflowToolbox.ReadUserPropertyFunction|com.fca.jira.plugins.workflowToolbox.ProjectPropertyFunction|com.fca.jira.plugins.workflowToolbox.ReadProjectPropertyFunction|com.fca.jira.plugins.workflowToolbox.WorkLogger|com.fca.jira.plugins.workflowToolbox.AssignToProjectRole|com.fca.jira.plugins.workflowToolbox.WriteIssuesJQL|com.fca.jira.plugins.workflowToolbox.ReadIssuesJQL|com.fca.jira.plugins.workflowToolbox.InhibitEphemeralFieldErasure|com.fca.jira.plugins.workflowToolbox.CloneIssueLinksFunction|com.fca.jira.plugins.workflowToolbox.TextExtractorFunction|com.fca.jira.plugins.workflowToolbox.CopyCascadingSelectToTextFunction|com.fca.jira.plugins.workflowToolbox.SetReporterFromCurrentUserFunction|com.fca.jira.plugins.workflowToolbox.SendEmailFunction|com.fca.jira.plugins.workflowToolbox.CreateIssue|com.fca.jira.plugins.workflowToolbox.CopyToIssueDescriptionFunction

Hi @Henning Tietgens ,

It works like a charm, validators and postfunctions now appears.  I'll do more couple of test and we'll let you know. Thanks for your help. Really appreciated! 

Just want to ask if I can scan all the workflows, not only on a specific workflow. Thank you!


Best Regards,
Alvin

Hi @Henning Tietgens ,

Aha! I've managed to scan all the workflows by replacing it to this line :

workflows.each{workflow -> 

Thank you so much for your help! 

Best Regards,
Alvin

Oh, you're right. I forgot to remove the workflow restriction in the answer. I corrected the answer. Thanks for the hint!

Henning

Hi @Henning Tietgens ,

Thank you for this wonderful solution, just want to ask if we can also tweak the script to check plugins that is used in a project, not just on a workflow. Currently , I'm using Power Admin to check it , but its good to have an alternatives. Thank you!

Best Regards,
Alvin

Hi Alvin,

I think it's possible to add this functionality to the script, but you may have to do this on your own :-)

Best regards,
Henning

You could export all workflows as xml-files and then search in these files for that plugin (e.g. com.googlecode.jsu). But I guess searching in in the database is faster.

Hi there,

When I run the above code in the ScriptRunner Console I get the following error:

 

Error

startup failed: Script45.groovy: 13: expecting '}', found '-' @ line 13, column 25. workflows.each{workflow -&gt; ^ 1 error

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script45.groovy: 13: expecting '}', found '-' @ line 13, column 25. workflows.each{workflow -&gt; ^ 1 error 

 

Any idea how to resolve?

Regards

Dave 

Please try copying the source again. There were some html conversion problems while converting from Atlassian answers to the community site.

Henning

Many thanks Henning

I noticed some warnings as shown below

warning.PNG

and when ran it seemed to picked up everything instead of just the lines with 'customfield' listed.

output.PNGAny idea what the problem is?

Regards

Dave

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Jira

The add-in you’ve been waiting for: Jira Cloud for Excel 🙌

Introducing Jira Cloud for Excel Here at the product integrations team at Atlassian, we are thrilled to announce the new Jira Cloud for Excel add-in! This add-in lets you export Jira data directly ...

838 views 5 22
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