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

This widget could not be displayed.

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);
    }
This widget could not be displayed.

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.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Published Jul 25, 2018 in Marketplace Apps

Jira Cloud and Bitbucket Cloud Integration with Microsoft Teams

One of the newest products in the Microsoft family - Microsoft Teams,  is a chat-based hub for teamwork that integrates all the people, content, and tools your team needs to be more engaged and ...

716 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you