Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Custom Script Field for JIRA ScriptRunner?

Hi we are using Jira and we are trying to use Plugin scriptrunner for jira to update field. We would like to display the total time rendered from one status to another status of the task, but it won't display properly upon running the script compare to the actual activity.

Here is the code used:

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
StatusManager statusManager = ComponentAccessor.getComponentOfType(StatusManager.class)
StatusCategoryManager statusCategoryManager = ComponentAccessor.getComponentOfType(StatusCategoryManager.class)

def html_table = "<table class='aui'><thead><tr><th>Status (From)</th><th>Status (To)</th><th>Time in Source Status</th><th>Transition Dates</th></tr></thead><tbody>"

def status1Time
def status2Time

def statusCategory
def statusCategoryTo
def changeItems = changeHistoryManager.getChangeItemsForField(issue, "status")
def changes = changeHistoryManager.getAllChangeItems(issue)
//ApplicationUser user=ComponentAccessor.getUserManager().getUserByKey(userKey);
if (changeItems.size() < 1){ //si la peticion esta en estado inicial, no hay historico
    status1Time = System.currentTimeMillis() - issue.getCreated().getTime()
    statusCategory = colorCategory(issue.getStatus().getStatusCategory().getId())
    statusCategoryTo = colorCategory(issue.getStatus().getStatusCategory().getId())
    String timeInStatus = formatTime(((status1Time / 1000) as long ?: 0L))
    def formatcreated = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(issue.created);
   // html_table = html_table + "<tr><td><span class='aui-lozenge aui-lozenge"+statusCategory+"'>"  + issue.getStatus().getName()+ "</td><td><span class='aui-lozenge aui-lozenge"+statusCategory+"'>"  + " "+ "</td><td>"  + timeInStatus+ "</td><td>"  + formatcreated + "</td></tr>"
    //html_table = html_table + "<tr><td><span class='aui-lozenge aui-lozenge"+statusCategory+"'>" + issue.getStatus().getName() + "</td><td>" + timeInStatus + "</td></tr>" timeInStatus
} else { //Si hay historico es porque se ha ejecutado como minimo una transicion
    for (int i = 0; i < changeItems.size(); i++) {
        ChangeItemBean item = changeItems[i]
        ChangeItemBean nextItem = changeItems[i+1]
  def formatcreated = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(item.created);
        if (i == 0){
            status1Time = item.created.getTime() - issue.getCreated().getTime()
            statusCategory = colorCategory(statusManager.getStatus(item.getFrom()).getStatusCategory().getId())
            statusCategoryTo = colorCategory(statusManager.getStatus(item.getTo()).getStatusCategory().getId())
            String timeInStatus1 = formatTime(((status1Time / 1000) as long ?: 0L))
            
           html_table = html_table + "<tr><td><span data-status class='aui-lozenge aui-lozenge-"+statusCategory+"'>" + item.getFromString()+"</td><td><span data-status class='aui-lozenge aui-lozenge-"+statusCategoryTo+"'>"+ item.getToString() + "</td><td>" + timeInStatus1+ "</td><td>"+ formatcreated +"</td></tr>"
            
            if (changeItems.size() > 1) {
                status2Time = nextItem.created.getTime() - item.created.getTime()
            } else {
                status2Time = System.currentTimeMillis() - item.created.getTime()
            }
            
            String timeInStatus2 = formatTime(((status2Time / 1000) as long ?: 0L))
         
            
           // html_table = html_table + "<tr><td><span data-status class='aui-lozenge aui-lozenge-"+statusCategory+"'>" + item.getFromString()+"</td><td><span data-status class='aui-lozenge aui-lozenge-"+statusCategoryTo+"'>"+ item.getToString() + "</td><td>" + timeInStatus2+ "</td><td>"+ formatcreated +"</td></tr>"
        } else if (i == changeItems.size() -1){
            
            status1Time = status2Time
            log.warn "${System.currentTimeMillis()}"
            log.warn "test ${item.created.getTime()}"
            statusCategory = colorCategory(statusManager.getStatus(item.getTo()).getStatusCategory().getId())
            statusCategoryTo= colorCategory(statusManager.getStatus(item.getFrom()).getStatusCategory().getId())
            String timeInStatus = formatTime(((status1Time / 1000) as long ?: 0L))
            
        html_table = html_table + "<tr><td><span class='aui-lozenge aui-lozenge-"+statusCategoryTo +"'>"+ item.getFromString()+ "<td><span class='aui-lozenge aui-lozenge-"+statusCategory+"'>" + item.getToString() + "</td><td>" + timeInStatus + "</td><td>" + formatcreated +"</td></tr>"
        } else {
            status1Time = nextItem.created.getTime() - item.created.getTime()
            statusCategory = colorCategory(statusManager.getStatus(item.getTo()).getStatusCategory().getId())
            statusCategoryTo = colorCategory(statusManager.getStatus(item.getFrom()).getStatusCategory().getId())
            String timeInStatus = formatTime(((status1Time / 1000) as long ?: 0L))
            
            html_table = html_table + "<tr><td><span class='aui-lozenge aui-lozenge-"+ statusCategoryTo +"'>"+ item.getFromString()+"<td><span class='aui-lozenge aui-lozenge-"+statusCategory+"'>" + item.getToString() + "</td><td>" + timeInStatus + "</td><td>" + formatcreated + "</td></tr>"
        }
    }
}

html_table = html_table + "</tbody></table>"

String colorCategory(Long id){
    String lozenge
    switch (id){
        case 2:
            return 'default'
        case 3:
            return 'success'
        case 4:
            return 'inprogress'
    }
}

String formatTime(Long timeInSeconds) {
    def hoursRest = (timeInSeconds/ (3600))
    def hours = hoursRest as Integer
    def minutesRest = ((timeInSeconds % 3600)/60)
    def minutes = minutesRest as Integer
    def secondsRest = ((timeInSeconds % 3600)%60)
    def seconds = secondsRest as Integer
    String valueToShow = " " + (hours as Integer).toString() +"h "+ (minutes as Integer).toString() + "m " + (seconds as Integer).toString() + "s"
    
    return valueToShow
}

return html_table


The output should be like this:


But it keeps displaying like this:



As you can see, the status "from" and "to" are accurate, but the "Time In Source Status" is not accurately displayed in table.

Thanks in advance.


1 answer

You can check if the hours or minutes are not equal to zero, for example:

String valueToShow = " " + 
(hours ? (hours as Integer).toString() + "h " : "") +
(minutes ? (minutes as Integer).toString() + "m " : "") +
(seconds as Integer).toString() + "s"

Suggest an answer

Log in or Sign up to
This widget could not be displayed.
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you