Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

how to check if array only has "A" or "B" or "A and B" ?


I want to check if the user is only in either Users and/or Watchers role .. I  have this working but wondering if there is a more elegant way ? 

if ( (remoteUsersRoles == ["Users"]) || ( remoteUsersRoles == ["Users","Watching"] ) || ( remoteUsersRoles == ["Watching", "Users"] ) )

Thanks in advance 

2 answers

0 votes

Another approach to achieve what Nic suggested is:

def rolesToCheck = ['Users', 'Watching']
if( rolesToCheck.any{it in remoterUserRoles})

With 2 roles to check, it's not bad, but should you have a situation with a log more, having a bunch of OR groups would be tedious.

But if you desire to check if the user has some other roles or not, you can do it this way:

def rolesToCheck = ['Users', 'Watching']
def otherRoles = remoteUserRoles - rolesToCheck
if(remoteUserRoles && !otherRoles) {
//user has some roles, but no roles when you subtract both Users and Watching
} else if(remoteUserRoles && otherRoles) {
//user has remoteUsersRoles, and some other roles, you can inspect otherRoles to see what remains once you've removed users and watching
} else {
//remoteUserRoles was empty to start with, subtracting has no effect
0 votes

I want to question the "watching" - do you genuinely have a project role of "watching" alongside the project roles like User, developer and administrator?  (Assuming you've not removed the default project roles)

If you do, then I'd write your query as

if ("Users" in remoteUsersRoles || "Watching" in remoteUsersRoles )

Suggest an answer

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

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

148 views 1 3
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