Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
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

Groovy script changing the permissions for search filter

Edited

Hi Gurus,

 

I am am trying to write a script to change the permisson for some filters from filterId but getting some error.

 

    import com.atlassian.jira.bc.JiraServiceContextImpl
    import com.atlassian.jira.bc.filter.SearchRequestService
    import com.atlassian.jira.bc.issue.search.SearchService
    import com.atlassian.jira.component.ComponentAccessor
    import com.atlassian.jira.issue.search.SearchRequest
    import com.atlassian.jira.sharing.SharePermissionImpl
    import com.atlassian.jira.sharing.SharedEntity
    import com.atlassian.jira.sharing.type.ShareType
    import com.atlassian.jira.issue.search.SearchRequestManager
    import com.atlassian.jira.issue.search.SearchRequest


	
    def searchRequestService = ComponentAccessor.getComponent(SearchRequestService)
    def user = ComponentAccessor.jiraAuthenticationContext?.getLoggedInUser()
    def searchService = ComponentAccessor.getComponent(SearchService)
    def serviceContext = new JiraServiceContextImpl(user)
    long filterId = 92399
    def ab = SharedRequest.getSearchRequestById(filterId)

    def sharePerm = new SharePermissionImpl(null, ShareType.Name.GROUP, "jira-administrators", null)
    ab.setPermissions(new SharedEntity.SharePermissions([sharePerm] as Set))

 when running getting following error

 

2017-06-12 14:00:35,084 WARN [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingPropertyException: No such property: SharedRequest for class: Script345 at Script345.run(Script345.groovy:19)

 

Please help me.

 

Regards,

Ansh

2 answers

1 accepted

0 votes
Answer accepted

This solved the problem

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.issue.search.SearchRequestEntity
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.sharing.SharePermission
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity.SharePermissions
import com.atlassian.jira.sharing.type.ShareType.Name
list = [58130,99187]
for (String item : list) {
 long l = Long.parseLong(item);
 ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
 SearchRequestManager srm = ComponentAccessor.getComponent(SearchRequestManager.class);
 SearchRequest sr = srm.getSearchRequestById(l)
 Set<SharePermission> permissionsSet = new HashSet<SharePermission>(
     sr.getPermissions().getPermissionSet()
            );
 permissionsSet.add(new SharePermissionImpl(Name.GLOBAL, null, null));
 sr.setPermissions(new SharePermissions(permissionsSet));
    srm.update(sr);
    }

Hi there ! 

Thanks for sharing your work !

May I ask what should i change to add not a group but a user ?

Hi Ansh,

try to change the line:

def ab = SharedRequest.getSearchRequestById(filterId)

to:

 def ab = searchService.getSearchRequestById(filterId)

regards,

Karol 

Hi Karol,

Thanks for helping. 

After changing suggested change getting following error 

2017-06-13 07:07:03,446 WARN [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.bc.issue.search.DefaultSearchService.getSearchRequestById() is applicable for argument types: (java.lang.Long) values: [99974] at Script20.run(Script20.groovy:20)

sorry, i don't have any jira at hand right now.

but looking closer at the script, try to change it this way:

// import com.atlassian.jira.bc.JiraServiceContextImpl
// import com.atlassian.jira.bc.filter.SearchRequestService
// import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
// import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity
import com.atlassian.jira.sharing.type.ShareType
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.issue.search.SearchRequest



// def searchRequestService = ComponentAccessor.getComponent(SearchRequestService)
def user = ComponentAccessor.jiraAuthenticationContext?.getLoggedInUser()
// def searchService = ComponentAccessor.getComponent(SearchService)
// def serviceContext = new JiraServiceContextImpl(user)
long filterId = 92399
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager)
def ab = searchRequestManager.getSearchRequestById(filterId)

def sharePerm = new SharePermissionImpl(null, ShareType.Name.GROUP, "jira-administrators", null)
ab.setPermissions(new SharedEntity.SharePermissions([sharePerm] as Set))

i commented out unused code.

hopefully it'll be better.

karol

Hi Karol,

Appriciate quick response.

Error is gone now with but permissions of the filter is not getting changed

import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.filter.SearchRequestService
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
// import com.atlassian.jira.issue.search.SearchRequest
import com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.SharedEntity
import com.atlassian.jira.sharing.type.ShareType
import com.atlassian.jira.issue.search.SearchRequestManager
import com.atlassian.jira.issue.search.SearchRequest



def searchRequestService = ComponentAccessor.getComponent(SearchRequestService)
def user = ComponentAccessor.jiraAuthenticationContext?.getLoggedInUser()
def searchService = ComponentAccessor.getComponent(SearchService)
def serviceContext = new JiraServiceContextImpl(user)
long filterId = 99974
def searchRequestManager = ComponentAccessor.getComponent(SearchRequestManager)
def ab = searchRequestManager.getSearchRequestById(filterId)
log.error(ab)
def sharePerm = new SharePermissionImpl(null, ShareType.Name.GLOBAL, null, null)
ab.setPermissions(new SharedEntity.SharePermissions([sharePerm] as Set))

I am logging "ab" variable its getting the jql search query associated with filterId same can be see in the output below.

2017-06-13 08:24:33,745 ERROR [runner.ScriptRunnerImpl]: Search Request: name: Jira work done
query = {project = "JIRA"}

But i have to create an object of the filterId that can be used to change the permisson of the Filter at ab.setPermissions.

 

Thanks Karol for helping. I got the solution.

Hi Anshul Agarwal,

                                i am also working on it, could u please show the final groovy script for changing the permissions of filters.

Thanks

Dinesh.

Hi Anshul Agarwal,

I working on a variation of the original question. 
How to grant Filter-Edit permissions for someone 
...
sp = new SharePermissionImpl(Name.USER, userName, null);

How to set "Rights" attribute to VIEW_EDIT? Default seems to be VIEW.

sp.getRights() 
always return VIEW on SharePermissionImpl's generated with API. SharePermission's created in the WEB GUI can be either VIEW_EDIT or VIEW.

Thanks

Like andybandy89 likes this

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: Fine Software

Happy New Year, everybody! What better way to kick off 2022 than with a new Marketplace Partner Spotlight ✨ This month, we're getting to know Daniel Franz, co-founder of Fine Software. Fine Software ...

430 views 2 15
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