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

How do I fetch work attribute value in groovy? (Tempo+scriptrunner)

Edited

Hello!

I'm trying to fetch the value of a work attribute (In Tempo; a 'Select list' called 'Type') on a work log using a custom listener script.

I can successfully fetch the value of the work attribute when executing the script in Scriptrunner's Script Console using the following script;

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogManager
// Tempo>>>
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.tempoplugin.core.workattribute.api.WorkAttributeValueService
@WithPlugin("is.origo.jira.tempo-plugin")
@PluginModule
WorkAttributeService workAttributeService
@PluginModule
WorkAttributeValueService workAttributeValueService
// <<<Tempo

// Dev-mode>>>
import com.atlassian.jira.issue.managers.DefaultIssueManager
def issueManager = ComponentAccessor.getComponent(DefaultIssueManager)
def issue = issueManager.getIssueObject("TEST-1") as Issue
log.warn(issue)
// <<<Dev-mode

/* Define current issue on which the event is triggered
def issue = event.issue
log.warn(issue)
*/

// Define Manager
def worklogManager = ComponentAccessor.getComponent(WorklogManager)

// Get latest work log id
def logId = worklogManager.getByIssue(issue).last()
log.warn(logId)
log.warn(logId.id)

// Define Work Attribute
def attType = workAttributeService.getWorkAttributeByKey("_Type_").returnedValue
log.warn(attType)
log.warn(attType.id)

// Get Work Attribute Value
def attributeValue = workAttributeValueService.getWorkAttributeValueByWorklogAndWorkAttribute(logId.id, attType.id).returnedValue
log.warn(attributeValue)
log.warn(attributeValue.value)

 Above script returns the following;

2018-09-09 05:09:06,462 WARN [runner.ScriptRunnerImpl]: TEST-1
2018-09-09 05:09:06,462 WARN [runner.ScriptRunnerImpl]: com.atlassian.jira.issue.worklog.WorklogImpl2@7e33b887
2018-09-09 05:09:06,463 WARN [runner.ScriptRunnerImpl]: 10657
2018-09-09 05:09:06,463 WARN [runner.ScriptRunnerImpl]: com.tempoplugin.core.workattribute.api.WorkAttribute@5d3c9709
2018-09-09 05:09:06,463 WARN [runner.ScriptRunnerImpl]: 5
2018-09-09 05:09:06,465 WARN [runner.ScriptRunnerImpl]: com.tempoplugin.core.workattribute.api.WorkAttributeValue@598fce84
2018-09-09 05:09:06,465 WARN [runner.ScriptRunnerImpl]: Testing

"Testing" is the name of the value of the attribute called 'Type' in this particular worklog.

This is what I want so it is working as expected in the script console.

 

However, when using this script (Slightly modified) as a custom listener, listening on the event "Work Logged On Issue", the 'attributeValue' returns nothing.

The only difference is how I define 'issue'. Here's that script;

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.WorklogManager
//###Tempo>>>
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.tempoplugin.core.workattribute.api.WorkAttributeService
import com.tempoplugin.core.workattribute.api.WorkAttributeValueService
@WithPlugin("is.origo.jira.tempo-plugin")
@PluginModule
WorkAttributeService workAttributeService
@PluginModule
WorkAttributeValueService workAttributeValueService
//<<<Tempo###

/*
//###Dev-mode>>>
import com.atlassian.jira.issue.managers.DefaultIssueManager
def issueManager = ComponentAccessor.getComponent(DefaultIssueManager)
def issue = issueManager.getIssueObject("TEST-1") as Issue
log.warn(issue)
//<<<Dev-mode###
*/

// Define current issue on which the event is triggered
def issue = event.issue
log.warn(issue)

// Define Manager
def worklogManager = ComponentAccessor.getComponent(WorklogManager)

// Get latest work log id
def logId = worklogManager.getByIssue(issue).last()
log.warn(logId)
log.warn(logId.id)

// Define Work Attribute
def attType = workAttributeService.getWorkAttributeByKey("_Type_").returnedValue
log.warn(attType)
log.warn(attType.id)

// Get Work Attribute Value
def attributeValue = workAttributeValueService.getWorkAttributeValueByWorklogAndWorkAttribute(logId.id, attType.id).returnedValue
log.warn(attributeValue)
log.warn(attributeValue.value)

 Above script returns the following;

2018-09-09 05:24:45,306 WARN [runner.ScriptRunnerImpl]: TEST-1
2018-09-09 05:24:45,309 WARN [runner.ScriptRunnerImpl]: com.atlassian.jira.issue.worklog.WorklogImpl2@44dbc585
2018-09-09 05:24:45,309 WARN [runner.ScriptRunnerImpl]: 10657
2018-09-09 05:24:45,309 WARN [runner.ScriptRunnerImpl]: com.tempoplugin.core.workattribute.api.WorkAttribute@5d3c9709
2018-09-09 05:24:45,309 WARN [runner.ScriptRunnerImpl]: 5
2018-09-09 05:24:45,310 WARN [runner.ScriptRunnerImpl]:

 

As you can see, the 'attributeValue' doesn't return anything here.

The strange thing is that it actually works sometimes if I add the "Issue Worklog Deleted" event and delete a worklog (At least one worklog needs to be left).

 

I'm using the following versions;

  • JSW Server v7.11.1
  • Tempo Core v4.12.1
  • Tempo Timesheets v8.13.1
  • Scriptrunner v5.4.12

 

Any thoughts on why this only seem to work in the Script Console and not as a listener?

 

Thanks in advance!

Best regards,

Markus Fredén

1 answer

Hi,

I personnaly fetched it from AO_013613_WA_VALUE table in the database,

 

import groovy.sql.Sql
import java.sql.Driver

def driver = Class.forName('com.mysql.jdbc.Driver').newInstance() as Driver

def props = new Properties()
props.setProperty("user", "username")
props.setProperty("password", "password")

String querydb = "SELECT Value,Worklog_ID FROM jiradb.AO_013613_WA_VALUE where value = '_Type_'"

def conn = driver.connect("jdbc:mysql://localhost:3306/jiradb", props)

def sql = new Sql(conn)
sql.eachRow(querydb) {
astreinte.put(it['Worklog_ID'],it['Value'])
}

 

regards.

Hi Florent,

Thanks for this, much appreciated.

I will most definitely use this if I dont find the way to do it without calling the db.

I will wait with accepting this as the answer until I have looked into it some more.

Best regards,

Markus

I think you should avoid executing direct database queries. The crux of the problem is that Tempo does not save all the work attributes until after your listener fires. However, I was able to get it to work using the Java API in the listener by spinning off a different thread that sleeps for a small time before trying to access the work attributes.

https://community.atlassian.com/t5/Adaptavist-questions/Scriptrunner-scripted-fields-integration-with-Tempo-fails/qaq-p/704521

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence Cloud

🎨 Add some visual life to your templates

Hi Atlassian Community, My name is Avni Barman, and I am a Product Manager on the Confluence Cloud team. Based on feedback from you, we are giving admins more power to create templates that a...

69 views 1 1
Read article

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