Worklog deletion and time spent adjustment

Claudio Ghisi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 11, 2021

Hi all,
I'm trying to do a simple thing: I want t delete all the worklogs created in a set timeframe.
To do this, I'm trying to use this code

def projectKey = 'TEST'
def startDate = '2020/12/01'
def endDate = '2021/01/09'
queryText = "worklogDate >= '" +startDate+ "' AND worklogDate < '" + endDate + "' AND project = " + projectKey
//Executre the query
try{
query = jqlQueryParser.parseQuery(queryText)
}catch(Exception ex){
log.info("Error executing the query: " + queryText)
}

def search = searchService.search(user, query, PagerFilter.getUnlimitedFilter())

//Iterate the issues
search.results.each { documentIssue ->
MutableIssue issue = issueManager.getIssueObject(documentIssue.id)

def workLogManager = ComponentAccessor.getWorklogManager()
def logsForIssue = workLogManager.getByIssue(issue)

//Iterate the worklog of the issue
for(Worklog worklog : logsForIssue)
{
def date = new SimpleDateFormat("yyyy/MM/dd").format(worklog.getCreated())
if((date >= startDate) && (date < endDate)){
//Delete the worklog
workLogManager.delete(worklog.getAuthorObject(), worklog, null, true)
}
}
}

The above code works fine when there's only one worklog to delete: the worklog is deleted and the time is correctly updated.

Things get different when an issue has 2 or more to be deleted. In this case I've found that:
the delete call deletes correctly all the worklogs
the time spent field is being updated taking into account the latest worklog only

This is a real trial I've made using a sleep function after the delete call.
Initial situation:
worklog 1: 1 hour
worklog 2: 3 hours
worklog 4: 8 hours
time spent: 12 hours (1+3+8)

With the above code, the result is:
worklog 1 is deleted -> time spent 11 hours
worklog 2 is deleted -> time spent 9 hours
worklog 3 is deleted -> time spent 4 hours

So, from what I saw the time spent field is being reset every time at the initial value

1 answer

0 votes
Alexander Eck [Tempo]
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 11, 2021

Hi,

not that I am an expert in this area but might it be that the Issue itself is not getting updated in time after each worklog delete? Have you tried to implement a slight delay after each delete event to see if that resolves the problem?

BR

Claudio Ghisi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 11, 2021

Hi Alexander,
I tried both: adding a 10 seconds delay and updating the issue on each cycle. Unfortunately, it's still not working as expected.

Thanks.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.2
TAGS
AUG Leaders

Atlassian Community Events