Hi there,
How does the script work?
The listener checks if the issue has an Epic and if so, checks all the issues in it. Sums the time for field "Original Estimate" from all issues and sets the sum in the Epic's field.
I have a script that I finished 2 days ago and tested it on all scenarios. The script worked as it should in each test and stopped working the next day, namely, everything is the same as before in the logs, but the specified time does not appear in the epic. The field is on the screen, everything is in order with the permissions. Nothing has changed in this regard.
You can see from the logs that the value appears in the epic, but if you immediately check it through the Scriptrunner console, then the epic has a value of 0.
Maybe someone has thoughts about what I did wrong.
If you have any thoughts on how to improve the script, please let me know.
Thank you.
Edit: The listener works for 3 events (issue created, updated, deleted)
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.component.ComponentAccessor
// Curent issue and "Epic Link" field
def issue = event?.issue
def cfEpicLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(14500)
log.warn("The issue type is ${issue.issueType.name}, the issue summary is \"${issue.summary}\"")
// If issue type is Epic or it doesn't have Epic Link - exit the script
if (issue.getIssueTypeId() == '6' || !issue.getCustomFieldValue(cfEpicLink)) {
return
}
log.warn('Inside the listener')
// Epic of the issue and current user
def epicOfIssue = issue.getCustomFieldValue(cfEpicLink) as MutableIssue
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Managers
def issueManager = ComponentAccessor.getIssueManager()
// Temporary variable for the sum of values from "Original Estimate" field.
Long originalEstimateTotal = 0
// If the issue is created with the value for "Epic Link"
if (event?.getEventTypeId() == EventType.ISSUE_CREATED_ID) {
!issue.getOriginalEstimate() ?: (originalEstimateTotal += issue.getOriginalEstimate())
}
// Managers for JQL search
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchService = ComponentAccessor.getComponent(SearchService)
// JQL query and Search to retrieve all issues in Epic
def query = jqlQueryParser.parseQuery("issueFunction in issuesInEpics(\'key = ${epicOfIssue.getKey()}\')")
def search = searchService.search(currentUser, query, PagerFilter.getUnlimitedFilter())
def searchResults = search.getResults().findAll()
searchResults?.each {
!it.getOriginalEstimate() ?: (originalEstimateTotal += it.getOriginalEstimate())
}
if (epicOfIssue.getOriginalEstimate() == originalEstimateTotal) {
log.warn("The values ARE the same: ${epicOfIssue.getOriginalEstimate() == originalEstimateTotal}, so make no change.")
log.warn("epic's original estimate = " + epicOfIssue.getOriginalEstimate())
} else if (epicOfIssue.getOriginalEstimate() != originalEstimateTotal) {
log.warn("The values are NOT the same: ${epicOfIssue.getOriginalEstimate() != originalEstimateTotal}, so make the change.")
log.warn("BEFORE - epic's original estimate = " + epicOfIssue.getOriginalEstimate())
log.warn("BEFORE - originalEstimateTotal = " + originalEstimateTotal)
epicOfIssue.setOriginalEstimate(originalEstimateTotal)
epicOfIssue.setEstimate(0)
issueManager.updateIssue(currentUser, epicOfIssue, EventDispatchOption.ISSUE_UPDATED, false)
log.warn("AFTER - epic's original estimate = " + epicOfIssue.getOriginalEstimate())
log.warn("AFTER - originalEstimateTotal = " + originalEstimateTotal)
}
log.warn('Outside the listener')
log.warn("epic's original estimate = " + epicOfIssue.getOriginalEstimate())
log.warn("originalEstimateTotal = " + originalEstimateTotal)
I found the answer.
I thought "setEstimate()" method was only responsible for "Remaining Estimate", but it turns out it also clears "Original Estimate".
As soon as I commented out this line, it started working as before.
Join the largest European gathering of the Atlassian Community and reimagine what’s possible when great teams and transformative technology come together. Plus, grab your Super Fan ticket now and save over €1,000 on your pass before prices rise on 3 June.
Register nowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.