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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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
4,463,406
Community Members
 
Community Events
176
Community Groups

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

Atlassian Community Events