JMWE Cloud: How to identify the customer with the most comments in a Jira (JSM) work item
Summary
In many support scenarios, the original reporter of a ticket can not be the primary person interacting with support. When sending automated feedback requests or notifications (e.g., using the Email issue(s) post-function in JMWE), it is often more effective to target the user who has been most active in the comment thread.
This article explains and provides a Nunjucks script to identify the accountId of the customer who has posted the highest number of comments on a specific Jira issue.
Use Case
You want to send an automated email to the most active participant on a ticket, rather than to the reporter. This ensures that the person who provides the most feedback receives the follow-up, which is particularly useful for feedback surveys or for clarifying questions.
Solution
Use the following Nunjucks script within a JMWE post function or template (such as the Users from template field in the Email issue post-function). This script filters users having a "customer" accountType and calculates which one appears most frequently in the comment history.
{% set accountIds = [] %}
{% set accountIdsLengths = [] %}
{# Filter comments to collect accountIds of customers only #}
{% for comment in issue.fields.comment.comments %}
{% if comment | field("author.accountType") == "customer" %}
{% set ignored = accountIds.push(comment.author.accountId) %}
{% endif %}
{% endfor %}
{# Group the accountIds to count occurrences #}
{% set accountIdCounts = accountIds | groupby %}
{# Map the counts into a sortable array of objects #}
{% for key,val in accountIdCounts %}
{% set ignored = accountIdsLengths.push({val:val.length, key:key}) %}
{% endfor %}
{# Sort by frequency descending and return the accountId of the top commenter #}
{{ accountIdsLengths | sort(true, null, "val") | first | field("key") }}NOTES:
The code currently considers only the comments made by JSM "customer." You can remove lines #5 and #7 to consider all comments, regardless of who created them.
The user account type can take the following values:
atlassian,app,customerIf required, the user account type can be changed by modifying line #5
How the script works:
Filtering: It iterates through all comments and adds the
accountIdto a list only if the author is a "customer".Grouping: It uses the
groupbyfilter to cluster identicalaccountIdtogether.Counting: It creates an array of objects containing the
accountId(key) and the number of comments (val).Sorting: Sorts the array in descending order by the number of comments and returns the
accountIdof the first entry (the most frequent commenter).