How to update changehistory in jira issue via groovy code automatically

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 vote

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.

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);

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 vote

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?

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

 

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

98 views 0 1
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you