Cannot get worklog attributes from worklogs

Hi

 

I've been trying to get worklog attributes from worklogs that are being entered. I want to find out if the worklog has the attribute _overtime_ and if so which value it has. I have a groovy script that is being called when the event worklogadd is executed.

The worklog element simply does not contain any attributes that have been created in Tempo so I have tried several things. I've tried to contact the Tempo plugin by using Rest:

String JIRA_API_URL = "http://OUR_SERVER/rest/tempo-timesheets/3/worklogs/" + worklog.id

HTTPBuilder jira = new HTTPBuilder(JIRA_API_URL)
jira.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'ProgramUser:admin'.bytes.encodeBase64().toString())
}
})

def tempoWorklog = jira.get(path: JIRA_API_URL )
log.warn("\nLogFlexTime worklogproperties issue " + tempoWorklog + "\n" + worklog.id)

This does give a result, but the object that is returned contains an empty workAttributeValues list. I have no idea why the workAttributeValues is empty. All the other values are there in the worklog object that is returned.

 

I've also tried the non rest way decribed here:

https://scriptrunner.adaptavist.com/4.3.0/jira/working-with-tempo.html

 

import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.worklog.attribute.WorklogAttributeService
@WithPlugin("is.origo.jira.tempo-plugin")
@PluginModule
WorklogAttributeService worklogAttributeService
boolean isOvertime = worklogAttributeService.getWorklogAttributes(11014).containsKey("_Overtime_")
log.warn("\nLogFlexTime isOvertime " + isOvertime)

 

This fails with the following error:

LogFlexTime.groovy: 24: unable to resolve class com.tempoplugin.worklog.attribute.WorklogAttributeService
@ line 24, column 1.
import com.tempoplugin.worklog.attribute.WorklogAttributeService
^

1 error

 

So I'm a bit lost here. I really need to figure out if the time logged on an issue is overtime or not. Anybody got any tips on this one?

2 answers

This widget could not be displayed.

What version of JIRA & Tempo are you using? We updated the docs for working with Tempo in 4.3.7 to work with Tempo 8; prior versions should work with Tempo 7.

Hi Jonny. We're using the latest versions of both:

Scriptrunner - 4.3.19

Tempo Timesheets - 8.2.5 

 

I see now that I have looked at the old documentation and that in the new documentation some of the imports have changed packaga path. Might be the problem. I'll check and get back to you.

 

Thanks so far Jonny.

So I've tried the new package paths and I'm now able to use WorkAttributeService and WorkAttributeValueService.

 

This is great I'm able to get worklog attribute Ids:

 

WorkAttribute attribute = workAttributeService.getWorkAttributeByKey("_Overtime_").returnedValue
WorkAttributeValue overtimeValue = workAttributeValueService.getWorkAttributeValueByWorklogAndWorkAttribute(11049, attribute.getId()).returnedValue

 

The call to getWorkAttributeValueByWorklogAndWorkAttribute unfortunatley returns null.

 

Could it be that I need to index the issue before trying to get the attribute value for it?

 

To start from the beginning. The user records a worklog and there selects a dropdown item called Overtime. I then have a listener active for getting worklog added events. This is triggered and I then try to call the code above on the worklog item that comes in the event. Am I doing this too soon or something? 

 

I've tried to run the same code in the Scriptrunner console, but then by hardcoding a worklog id. It then works perfectly. It's only when I do this from the event that it resturns null

Do you have any idea Jonny?

This widget could not be displayed.

I've found the reason for the problem.

Well at least the workaround. The original issue that the worklog attributes are not included in the event is still an issue, but the workaround to get them form the DB is now solved. The problem is that the events in JIRA isn't asynchronous. So the events (worklog created, worklog updated, worklog deleted) are all waiting for the script in the listener to run before they finish. So the worklog isn't actually stored in the DB before the scripts attached to the listeners are ran. No wonder the searches returned null ;)

So my way of solving this is to add a Thread in the script and look for the needed values in that thread instead. That way the values are stored while the script is running and I can fetch them from the DB.

Hey there,

Any chance you could share that Thread part?

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

146 views 2 0
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