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

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

Markus Fredén
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.
September 9, 2018

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

Suggest an answer

Log in or Sign up to answer
1 vote
Florent DELRIEU
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!
September 14, 2018

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.

Markus Fredén
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.
September 17, 2018

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

Sammo Gabay November 20, 2018

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

TAGS
AUG Leaders

Atlassian Community Events