Custom field value to comment

Rumceisz April 28, 2013

Hi All,

we got a difficult request from a project lead: they want to copy the value of a custom field to comment. I.e. on each issue they would like to create a comment with the value of this custom field. The field is of free text type.

Now we don't know a method how to do this. Have you ever done such? Any idea?

Thanks in advance!

Rumi

5 answers

1 accepted

0 votes
Answer accepted
Nadir MEZIANI
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 28, 2013

Hi,

I think that you can do this with JJUPIN or JJS scripting plugin, or you develop a plugin.

In jira 4.1.2 , we can do this with Javascript

1 vote
MJ
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
April 28, 2013

The tools currently provided within JIRA do not provide a solution for this, not have I been able to find a plugin on our marketplace that provides such functionality. I can't directly see the purpose of doing this, perhaps you can explain more on why exaclty you want to have this functionality in place, maybe we can find another (better fitting) solution.

0 votes
William Crighton _CCC_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 17, 2014

Our use case for this functionality was a system wide un-changing identifier that could be shared with external parties who don't care 'where' the issue is in JIRA but rather wanted to leverage the quicksearch option to rapidly/easily locate an issue (that you knew the global key of beforehand).

So we created a simple GUID plugin that used existing JIRA sequence rules to create a Support ID that wouldn't change. At which point everyone went 'hey, great, now how do I find it via quicksearch?'.

The solution was a Groovy Listener that took the generated GUID value and added it as the first comment to the issue. The groovy code we used is as follows - it wasn't as classy or sophiscated as is could have been but it was a nice 12 hour problem to solution stretch so everyone was generally happy.

NOTE: We had to use a listener instead of a post-function on issue create due to how you cannot control the order post functions are executed - the listener just made it simpler.

package com.awesomenesscanbeuncool.jira.groovy.listeners
/**
 * Created: 20130101.2358
 */

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.crowd.embedded.api.User
import org.apache.log4j.Logger

public class AddCommentToEnableStaticSupportIDQuickSearchListener extends AbstractIssueEventListener 
{

    Logger log = Logger.getLogger(AddCommentToEnableStaticSupportIDQuickSearchListener.class);

    // -- ------------------------------------------------------------------ --
    // -- Defines
    // -- ------------------------------------------------------------------ --
    // CF_STATICSUPPORTID is the name of the custom field
    public static final String CF_STATICSUPPORTID = "GUID";
    // DATA_MIGRATION_USER is the username of the user used to migrate data into JIRA from external systems
    public static final String DATA_MIGRATION_USER = "datamigration.user";


    void issueCreated(IssueEvent event) 
    {
        log.debug("Issue Created - calling addComment");
        addComment(event)
    }

    void addComment(IssueEvent event) 
    {
        // Set the listenerIssue variable
        // issue is supplied by the groovy script engine automatically
        Issue listenerIssue = event.getIssue();

        // Get the remote user
        // use the user object returned by getJIRAAuthenticationContext as it won't need to be converted
        User currentUser = ComponentManager.getInstance().getJiraAuthenticationContext().getLoggedInUser();

        // Don't add the comment if the issue is being migrated from an external system
        if (currentUser != null && !DATA_MIGRATION_USER.equals(currentUser.getName())) 
        {
            CommentManager commentManager = (CommentManager) ComponentManager.getComponentInstanceOfType(CommentManager.class);
            CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

            String comment;
            CustomField cf = customFieldManager.getCustomFieldObjectByName(CF_STATICSUPPORTID);
            if (cf != null)  
            {
                String value = listenerIssue.getCustomFieldValue(cf);
                comment = "-->Static Support ID: " + value + "<--";
            } 
            else 
            {
                comment = "CustomField " + CF_STATICSUPPORTID + " cannot be found";
            }

            if (comment != null) 
            {
                commentManager.create(listenerIssue, currentUser.getName(), comment, false);
            }
        }
    }
}

----

Because of the specific situation we added other unique'ing type symbols around the ID just in case the ID was referenced by a user in another issue comment.

0 votes
Rumceisz April 28, 2013

Hi,

I have a script which saves the issue key of each issue to comment.

How can it be changed if we want to save the value of a custom field to comment?

<?xml version="1.0"?>
<!--
// ===========================================================================
// Writes an issue key as a comment
// This version of the script checks if previously an issue key was already
// saved in one of the comments. This is slower but it is handy when a
// previous run has crashed.
// ---------------------------------------------------------------------------
// Author...........: Arpad Madocsai
// Last change date.: 2012-11-01
// Last change by...: George Lewe
// ===========================================================================
-->
<JiraJelly xmlns:jira="jelly:com.atlassian.jira.jelly.enterprise.JiraTagLib" xmlns:core="jelly:core" xmlns:log="jelly:log" >
 
   <log:info>Running Jelly script: Issue key saver</log:info>
  
   <!-- Set the project key -->
   <core:set var="projectId" value="10092" />
   
   <core:invokeStatic className="com.atlassian.jira.ComponentManager" method="getInstance" var="componentManager"/>
   
   <!-- Get CommentManager and IssueManager from ComponentManager -->
   <core:invoke on="${componentManager}" method="getCommentManager" var="commentManager" />
   <core:invoke on="${componentManager}" method="getIssueManager" var="issueManager" />
  
   <!-- Get all issue IDs from this project -->
   <core:invoke on="${issueManager}" method="getIssueIdsForProject" var="issues">
      <core:arg type="java.lang.Long" value="${projectId}"/>
   </core:invoke>
    
   <!-- Loop through all issues -->
   <core:forEach var="issueId" items="${issues}">
  
      <!-- Get the issue -->
      <core:invoke on="${issueManager}" method="getIssueObject" var="issue" >
         <core:arg type="java.lang.Long" value="${issueId}"/>
      </core:invoke>
  
      <!-- Get the issue's comments -->
      <core:invoke on="${commentManager}" method="getComments" var="comments">
         <core:arg type="com.atlassian.jira.issue.Issue" value="${issue}"/>
      </core:invoke>
      
      <!-- Check if the key already is in one of the comments -->
      <core:set var="found" value="false" />
      <core:forEach var="comment" items="${comments}">
         <core:invoke on="${comment.body}" method="startsWith" var="found">
            <core:arg type="java.lang.String" value="Pre-migration issue key"/>
         </core:invoke>
      </core:forEach>
  
      <!-- If no previous key was found then add it now -->
      <core:if test="${!found}">
         ${issue.key}
         <core:invoke on="${commentManager}" method="create" var="newcomment">
            <core:arg type="com.atlassian.jira.issue.Issue" value="${issue}"/>
            <core:arg type="java.lang.String" value="u204497"/>
            <core:arg type="java.lang.String" value="Pre-migration issue key: ${issue.key}"/>
            <core:arg type="boolean" value="false"/>
         </core:invoke>
      </core:if>
  
   </core:forEach>
         
</JiraJelly>

0 votes
Mizan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 28, 2013

You can write a groovy script which will

-> get all issues of the project

-> iterate these issues and get the value of the field for each issue .

-> add comment programmatically where value will be the customfield value

You can refer the in built script https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts#Built-InScripts-CopyCustomFieldValues

EDIT : you will need to run this script in the script runner panel and test on a test instance before you apply it to the live instance

Suggest an answer

Log in or Sign up to answer