How to update changehistory in jira issue via groovy code automatically

xiaoxue chen September 9, 2015

I write a groovy script in post-function to calculate the value of field "Effort Split" in Epic based on the Stories (Sum of all "Effort Split" values in Stories), and now i can get the sum value and show it in Epic issue. But these change cannot be updated in history of Epic issue.
Could you please give me some suggestions? If there is some groovy sentence can achieve this?

My code like below:

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

ComponentManager componentManager = ComponentManager.getInstance()
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
String issuekey=issue.toString()
IssueLinkManager issueLinkManager = componentManager.getIssueLinkManager()
List list = issueLinkManager.getOutwardLinks(componentManager.getIssueManager().getIssueByCurrentKey(issuekey).id)
Map<String, String> effortSplitMap = new HashMap<String, String>();

for(i=0;i<list.size();i++){
String effortSplit = list[i].destinationObject.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Effort Split'))
List effortSplitList = new ArrayList()
effortSplitList = effortSplit.split("\r\n")
for(j=0;j<effortSplitList.size();j++){
if(effortSplitMap.get(effortSplitList[j].split("&")[0]))

{ effortSplitMap.put(effortSplitList[j].split("&")[0].trim(),(Integer.parseInt(effortSplitList[j].split("&")[1].trim()) + Integer.parseInt(effortSplitMap.get(effortSplitList[j].split("&")[0]))).toString()) }

else

{ effortSplitMap.put(effortSplitList[j].split("&")[0].trim(),(Integer.parseInt(effortSplitList[j].split("&")[1].trim())).toString()) }

}
}

StringBuffer stringBuffer = new StringBuffer()
stringBuffer.append("HLR_DEV")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("HLR_DEV"))
stringBuffer.append("\r\n")
stringBuffer.append("HSS_DEV")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("HSS_DEV"))
stringBuffer.append("\r\n")
stringBuffer.append("LTE_DEV")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("LTE_DEV"))
stringBuffer.append("\r\n")
stringBuffer.append("DB_DEV")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("DB_DEV"))
stringBuffer.append("\r\n")
stringBuffer.append("HLR_FT")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("HLR_FT"))
stringBuffer.append("\r\n")
stringBuffer.append("HSS_FT")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("HSS_FT"))
stringBuffer.append("\r\n")
stringBuffer.append("LTE_FT")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("LTE_FT"))
stringBuffer.append("\r\n")
stringBuffer.append("DB_FT")
stringBuffer.append("&")
stringBuffer.append(effortSplitMap.get("DB_FT"))

issue.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Effort Split'),stringBuffer.toString())
DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
def cf_object=customFieldManager.getCustomFieldObjectByName('Effort Split')
cf_object.updateValue(null, issue, new ModifiedValue(null, issue.getCustomFieldValue(cf_object)), issueChangeHolder);

5 answers

0 votes
xiaoxue chen September 20, 2015

Hello,

Now the Epic effort value can be updated based on the story effort via my code, but this update cannot be displayed in the history log of issue. So what we should do if i want this change can be displayed in the History activity.

 

Thanks and Regards

 

JamieA
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.
September 20, 2015

If you want it displayed in the changelog you should probably use a real field, and update it in a listener.

0 votes
JamieA
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.
September 18, 2015

I don;t think you want to store in the change history the value of a calculated field. What's the purpose, to see how the story effort changed over time?

0 votes
xiaoxue chen September 18, 2015

Hello,

Is there anyone can help me on this issue, how i can update my change history in JIRA issue when the issue's field value is calculated by its linked issue.

Thanks and Regards

0 votes
xiaoxue chen September 10, 2015
import com.atlassian.jira.issue.ModifiedValue
 import com.atlassian.jira.issue.link.IssueLinkManager
 import com.atlassian.jira.ComponentManager
 import com.atlassian.jira.issue.CustomFieldManager
 import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
ComponentManager componentManager = ComponentManager.getInstance()
 CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
 String issuekey=issue.toString()
 IssueLinkManager issueLinkManager = componentManager.getIssueLinkManager()
 List list = issueLinkManager.getOutwardLinks(componentManager.getIssueManager().getIssueByCurrentKey(issuekey).id)
 Map&lt;String, String&gt; effortSplitMap = new HashMap&lt;String, String&gt;();
for(i=0;i&lt;list.size();i++){
 String effortSplit = list[i].destinationObject.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Effort Split'))
 List effortSplitList = new ArrayList()
 effortSplitList = effortSplit.split("\r\n")
 for(j=0;j&lt;effortSplitList.size();j++){
 if(effortSplitMap.get(effortSplitList[j].split("&amp;")[0]))
{
 
effortSplitMap.put(effortSplitList[j].split("&amp;")[0].trim(),(Integer.parseInt(effortSplitList[j].split("&amp;")[1].trim())
 + 
Integer.parseInt(effortSplitMap.get(effortSplitList[j].split("&amp;")[0]))).toString())
 }
else
{ 
effortSplitMap.put(effortSplitList[j].split("&amp;")[0].trim(),(Integer.parseInt(effortSplitList[j].split("&amp;")[1].trim())).toString())
 }
}
 }
StringBuffer stringBuffer = new StringBuffer()
 stringBuffer.append("HLR_DEV")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("HLR_DEV"))
 stringBuffer.append("\r\n")
 stringBuffer.append("HSS_DEV")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("HSS_DEV"))
 stringBuffer.append("\r\n")
 stringBuffer.append("LTE_DEV")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("LTE_DEV"))
 stringBuffer.append("\r\n")
 stringBuffer.append("DB_DEV")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("DB_DEV"))
 stringBuffer.append("\r\n")
 stringBuffer.append("HLR_FT")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("HLR_FT"))
 stringBuffer.append("\r\n")
 stringBuffer.append("HSS_FT")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("HSS_FT"))
 stringBuffer.append("\r\n")
 stringBuffer.append("LTE_FT")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("LTE_FT"))
 stringBuffer.append("\r\n")
 stringBuffer.append("DB_FT")
 stringBuffer.append("&amp;")
 stringBuffer.append(effortSplitMap.get("DB_FT"))
issue.setCustomFieldValue(customFieldManager.getCustomFieldObjectByName('Effort Split'),stringBuffer.toString())
 DefaultIssueChangeHolder issueChangeHolder = new DefaultIssueChangeHolder();
 def cf_object=customFieldManager.getCustomFieldObjectByName('Effort Split')
 cf_object.updateValue(null, issue, new ModifiedValue(null, issue.getCustomFieldValue(cf_object)), issueChangeHolder);
0 votes
JamieA
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.
September 9, 2015

Can you format your code using the {code} macro so that it's readable. Please add some logging so you know that it's being executed at all first, eg log the value that it's going to set.

Suggest an answer

Log in or Sign up to answer