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

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

lujmo May 3, 2022

Hi 

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

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
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 3, 2022

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
Nic Brough -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 3, 2022

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 )
TAGS
AUG Leaders

Atlassian Community Events