Custon JQL Function, User in Group from custom field

David Harkins
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.
May 4, 2023

Trying to get the following custom JQL Function working in data center.

To compare a custom field and check if its group value is in the list of groups that the current user is a member of.

 

package com.onresolve.jira.groovy.jql

import com.atlassian.jira.JiraDataType

import com.atlassian.jira.JiraDataTypes

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.jql.operand.QueryLiteral

import com.atlassian.jira.jql.query.QueryCreationContext

import com.atlassian.jira.permission.ProjectPermissions

import com.atlassian.jira.project.version.VersionManager

import com.atlassian.query.clause.TerminalClause

import com.atlassian.query.operand.FunctionOperand

class AssignedToMyGroups extends AbstractScriptedJqlFunction implements JqlFunction {

    def versionManager = ComponentAccessor.getComponent(VersionManager)

    def permissionManager = ComponentAccessor.getPermissionManager()

 

    @Override

    String getDescription() {

        "Issues where Group field is one of My Groups"

    }

 

    @Override

    List<Map> getArguments() {

        Collections.EMPTY_LIST // <1>

    }

 

    @Override

    String getFunctionName() {

        "AssignedToMyGroups"

    }

 

    @Override

    JiraDataType getDataType() {

        JiraDataTypes.VERSION // <2>

    }

 

    def groupManager = ComponentAccessor.groupManager

    def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

    final username = loggedInUser.username

    @Override

    List<QueryLiteral> getValues(

        groupManager.getGroupNamesForUser(username)

    )

         

}

3 answers

1 accepted

0 votes
Answer accepted
David Harkins
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.
May 16, 2023

Made some progress, Now showing up in the Scriptrunner JQL Functions, but not having the desired effect.

package com.onresolve.jira.groovy.jql

import com.atlassian.jira.JiraDataType
import com.atlassian.jira.JiraDataTypes
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand

class AssignedToMyGroups extends AbstractScriptedJqlFunction implements JqlFunction {

    def versionManager = ComponentAccessor.getComponent(VersionManager)
    def permissionManager = ComponentAccessor.getPermissionManager()

    @Override
    String getDescription() {
        "Issues where Group Custom Field is one of the Current Users Group"
    }

    @Override
    List<Map> getArguments() {
        Collections.EMPTY_LIST // <1>
    }

    @Override
    String getFunctionName() {
        "AssignedToMyGroups"
    }

    @Override
    JiraDataType getDataType() {
        ArrayList // <2>
    }

    @Override
    List<QueryLiteral> getValues(
        QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause
    ) {
            def groupManager = ComponentAccessor.groupManager
            def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
            def username = loggedInUser.username
            def groups = groupManager.getGroupNamesForUser(username) as ArrayList
            return groups
    }
}

The purpose is to return all the groups that the current user is a member of so that a Single Group Custom field can be checked to confirm if it is in the list of groups.

0 votes
David Harkins
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.
June 7, 2023

So, after some digging around and research, I have a working version now :-) 

I located a comment on a similar topic by @JamieA, which let me to GitHub and the following JQL Function which gave me my missing pieces. CurrentTeams.groovy · GitHub

My final code is:

 

package com.onresolve.jira.groovy.jql

import com.atlassian.jira.JiraDataType
import com.atlassian.jira.JiraDataTypes
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.operand.QueryLiteral
import com.atlassian.jira.jql.query.QueryCreationContext
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.query.clause.TerminalClause
import com.atlassian.query.operand.FunctionOperand


class groupsOfCurrentUser extends AbstractScriptedJqlFunction implements JqlFunction {
def versionManager = ComponentAccessor.getComponent(VersionManager)
def permissionManager = ComponentAccessor.getPermissionManager()

@Override
String getDescription() {
"Get Current Users Groups"
}
@Override
List<Map> getArguments() {
Collections.EMPTY_LIST
}

List<String> getGroups() {
def groupManager = ComponentAccessor.groupManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def username = loggedInUser.username
def groups = groupManager.getGroupNamesForUser(username) as ArrayList
groups
}

@Override
JiraDataType getDataType() {
JiraDataTypes.TEXT
}

@Override
String getFunctionName() {
"groupsOfCurrentUser"
}

@Override
List<QueryLiteral> getValues(QueryCreationContext queryCreationContext, FunctionOperand operand, TerminalClause terminalClause
) {
getGroups().collect{
new QueryLiteral(operand, it)
}
}
}

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 10, 2023

Hi @David Harkins

Could you provide more information on what exactly you are trying to do?

What type of custom field are you using? Is it a group picker or a user picker? I am requesting this information to try to provide a sample code.

I am looking forward to your feedback.

Thank you and Kind regards,

Ram

David Harkins
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.
May 10, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you for taking the time to reply :-)

The Custom Field that will be used is a Group Picker (Single Group)

 

This will then allow us to better associated / assign issue to a group of agents that have the respective skills based on group membership.

Many Thanks

David

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 10, 2023

Hi @David Harkins

Thank you for providing the information.

I have a couple of questions to ask, i.e. what approach do you want to use to assign the issue, i.e. Post-Function, Listener or Behaviour?

Secondly, since you are taking this by groups, the Assignee field can only add one person at a time. What other conditions do you intend to use to filter out the most appropriate person from the group to assign the issue to?

Thank you and Kind regards,

Ram

David Harkins
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.
May 10, 2023

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

The value of the 'Assignee Group' custom field will be populated by listeners and a combination of defined class methods.

What we are trying to do here is create a filter to show all issues where the current user is a member of the group name that is in the 'Assignee Group'

Agents can then see all issues that they have the skills to progress, they can then assign the issues to themselves.  But still remain with the group to handle agent working hours and vacations for 24/7 coverage.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 11, 2023

Hi @David Harkins

Going through your latest comment, this is not doable.

You can restrict the Issue Type by Role or Group visibility during the Create / Edit / Transition screen using ScriptRunner's Behaviour

However, once the issue is created, all users can view it on the view screen.

A similar question was asked in this Community Post.

Thank you and Kind regards,

Ram

David Harkins
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.
May 12, 2023

@Ram Kumar Aravindakshan _Adaptavist_ 

Maybe I've not explained myself very well, or over explained.

One Custom Field on the issue.  How it gets populated is already handled.

The reason for this custom scriptrunner JQL is to return issues where the current user is a member of the single group that is in the custom field.

Yes, all agents can view all issues in the project, this JQL will allows queues and dashboards to be created that are specific for the currents users skill set.

If the above code returns all the groups that the current user is a member of,

then if the group in the custom field is in the returned list, the issue will be returned in the JQL output.

Suggest an answer

Log in or Sign up to answer