Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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 Champions.
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

1 accepted

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events