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

Scriptrunner: How to list approvers?

Kirill Ilinich May 10, 2018

I'm trying to write script to notify approvers via slack ot issue transition.
I choose script listener (not post-finction) due to large number of possible transitions.
So how to do following:

  1. Detect if issue require approval
  2. Get list of approvers (later with their descisions)

I've done that before on cloud version via REST API, there is no problem with that, but in server version I've got original issue (not after transition) so there is no approval yet in moment of transition. I've try to access approval custom field, but there is no approvers info in it.  I've try to search examples how to work with approvals in scriptrunner, but no dice. I've try following code to test jira java api, but it's not working...

@WithPlugin ("com.atlassian.servicedesk")
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.servicedesk.api.approval
def issue = ComponentAccessor.getIssueManager().getIssueObject("DVPT-1")
ApprovalQuery query = approvalService.newQueryBuilder().issue(issue).build()

it shows error about "ApprovalQuery" class. What I need to import to have it?

How to work with approvals?

5 answers

0 votes
Thanos Batagiannis [Adaptavist] Community Leader Jul 17, 2018

Hi Kirill,

The Approvals and the Approvers are actually custom fields, therefore you can retrieve their values in the usual way 

import com.atlassian.jira.component.ComponentAccessor

def issue = ComponentAccessor.getIssueManager().getIssueObject("SD-2")

def approversCF = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find {it.name == "Approvers"}
def approvers = issue.getCustomFieldValue(approversCF)

log.debug approvers //a list of ApplicationUsers

def approvalsCF = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find {it.name == "Approvals"}
def approvals = issue.getCustomFieldValue(approvalsCF)

log.debug approvals // a list of Approval see https://docs.atlassian.com/jira-servicedesk/3.9.0-m0124/com/atlassian/servicedesk/api/approval/Approval.html
Kirill Ilinich Jul 17, 2018 • edited

Jira Server 7.11?
Actually, you are waaay wrong /-)

You script returns:


2018-07-17 16:38:48,068 INFO [runner.ScriptRunnerImpl]:
2018-07-17 16:38:48,072 INFO [runner.ScriptRunnerImpl]: com.atlassian.servicedesk.plugins.approvals.internal.customfield.ApprovalsCFValue@475f642

 I found the way to get those values, but another bug (no values on transition) prevents me from implement listener script:

import com.atlassian.jira.component.ComponentAccessor


def issue = ComponentAccessor.getIssueManager().getIssueObject('DVPT-1')
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
log.info("User: $currentUser.name")

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.atlassian.servicedesk.api.approval.ApprovalService

@WithPlugin
("com.atlassian.servicedesk")

def approvalService = ComponentAccessor.getOSGiComponentInstanceOfType(ApprovalService)
def approvalQuery = approvalService.newQueryBuilder().issue(issue.id).build()
def approvalEither = approvalService.getApprovals(currentUser, approvalQuery)
def approvals = approvalEither.isRight() ? approvalEither.right : []
def pendingApproval = approvals.find{!it.getDecisionDetails().getOrNull()?.getDecision()}
def approversList = []
if (pendingApproval) {
def approversEither = approvalService.getApprovers(currentUser, pendingApproval)
def approvers = approversEither.isRight() ? approversEither.right : []
approversList
= approvers.findAll{!it.getApproverDecision().getOrNull()}.collect{it.getApproverUser().name}
log.info("Pending approval of: $approversList")
}
Thanos Batagiannis [Adaptavist] Community Leader Jul 18, 2018

Kirill, 

The ApprovalCFValue is a collection of approvals

public ApprovalsCFValue(Collection<Approval> approvals) {
this.approvals = approvals;
}

Do not try to overcomplicate things, those are custom fields treat them as custom fields. 

Kirill Ilinich Jul 18, 2018 • edited

Colud you drop me a working code for listing pending approvers please? 

Code should return just a list of login...

Thank you!

Vadim Lutsevich Mar 01, 2019

@Kirill Ilinich do you have any working code? Share it please, I can't make my script (that based on this topic) works.

Kirill Skachkov Mar 01, 2019

Nope, Adaptavista confirm that problem but there is no solution yet, so I add notification in post-function instead of listener

0 votes
Kirill Ilinich Apr 17, 2019

Not working in Jira 8 anymore. Need code update!

0 votes
Kirill Ilinich Apr 17, 2019 • edited

That's what I came up with:

import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.servicedesk.api.approval.ApprovalService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.atlassian.servicedesk")
logs = Logger.getLogger("com.acme.customListener")
logs.setLevel(Level.DEBUG)

def issue = ComponentAccessor.getIssueManager().getIssueObject("DVPS-7011") // console testing

def robotUser = ComponentAccessor.getUserManager().getUserByKey("robot-manager")
def approvalService = ComponentAccessor.getOSGiComponentInstanceOfType(ApprovalService)
def approvalQuery = approvalService.newQueryBuilder().issue(issue.id).build()
def approvalOpt = approvalService.getApprovals(robotUser, approvalQuery).findFirst()
if (approvalOpt.isPresent() && !approvalOpt.get().getDecision().isPresent()) {
def approvalsNeeded = approvalService.getApprovers(robotUser, approvalOpt.get()).findAll{!it.getApproverDecision().isPresent()}.collect{it.getApproverUser().name}
logs.info("Approvals needed: $approvalsNeeded")
//
// Remind to approve
//
} else {
logs.info("No pending approvals found for issue [$issue.key]")
}

 

0 votes
Kirill Ilinich Apr 17, 2019

@Vadim Lutsevich I've got code for Jira 8 now.

Vadim Lutsevich Apr 18, 2019

Thanks! But I realized this feature via JQL :)

Kirill Ilinich Apr 18, 2019

To list approvers? 

0 votes
Kirill Ilinich Apr 18, 2019 • edited

A bit simpler via customField:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.servicedesk.api.approval.ApprovalService
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.atlassian.servicedesk")
def issue = ComponentAccessor.getIssueManager().getIssueObject("DVPS-7010") // For console testing
def robotUser = ComponentAccessor.getUserManager().getUserByKey("robot-manager")
def approvalService = ComponentAccessor.getOSGiComponentInstanceOfType(ApprovalService)
def approval = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName('Approvals').first()).getApprovals()
if (approval && !approval.first().getDecision().isPresent()) {
def approvalsNeeded = approvalService.getApprovers(robotUser, approval.first()).findAll{!it.getApproverDecision().isPresent()}.collect{it.getApproverUser().name}
log.info("Approvals needed: $approvalsNeeded")
//
// Remind to approve
//
} else {
log.warn("No pending approvals found for issue [$issue.key]")
}

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Webinar alert! An IT Admin's Guide to Securing Collaboration at Scale

Hello, Atlassian Community! My name is Dave Meyer and I'm a Principal Product Manager at Atlassian. I wanted to give this community a heads up about an upcoming Webinar that might be of interest...

177 views 2 7
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