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

Error with ComponentManager Edited

I am running below code in script runner console to achieve " add specific dashboards as favorite for user(s) from specific group(s)." referred from this link.

/*
This script can be run from Scriptrunner console to set Dashboards
listed in the variable portalPageIds as favourites for users from
groups listed in the variable groupNames
*/
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.portal.PortalPage
import com.atlassian.jira.portal.PortalPageManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.favourites.FavouritesManager
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("")
log.setLevel(Level.DEBUG)
def portalPageIds = [10304]
def groupNames = ['Cross_Client-Create_Access']
def groupManager = ComponentAccessor.getGroupManager()

ComponentManager componentManager = ComponentManager.getInstance()
PortalPageManager portalPageManager = (PortalPageManager) componentManager.getComponentInstanceOfType(PortalPageManager.class)
FavouritesManager favouritesManager = (FavouritesManager) componentManager.getComponentInstanceOfType(FavouritesManager.class)


for (String groupName: groupNames) {
def grpUserList = groupManager.getUsersInGroup(groupName)
for (ApplicationUser user:grpUserList){
for (Long portalPageId: portalPageIds) {
PortalPage portalPage = portalPageManager.getPortalPageById(portalPageId)
try{
favouritesManager.addFavourite(user, portalPage)
log.info("Adding portal " + portalPageId.toString() + " to user: " + user)
} catch (Exception ex){
log.warn("Failed Adding portal " + portalPageId.toString() + " to user: " + user + "\n" + ex)
} //catch
} //for portalID
} //for group users
} //for groups

 

 

But am getting this error on ComponentManager componentManager = ComponentManager.getInstance()

Cannot cast object 'com.atlassian.jira.component.pico.ComponentManager@1ee07b2a' with class 'com.atlassian.jira.component.pico.ComponentManager' to class 'com.atlassian.jira.ComponentManager'

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.atlassian.jira.component.pico.ComponentManager@1ee07b2a' with class 'com.atlassian.jira.component.pico.ComponentManager' to class 'com.atlassian.jira.ComponentManager' at Script3135.run(Script3135.groovy:20)

 

can somebody please help

3 answers

1 vote
tom lister Community Leader Nov 21, 2018

Hi @Prabu N

getInstance is deprecated 

try ComponentAccessor.getComponentManager()

Hi Tim,

I tried ComponentAccessor.getComponentManager() but not works. 

snap.png

Yes, it seems this was written long back not compatible for Jira 7.11.

Anybody can help here?

ComponentClassManager componentManager = ComponentAccessor.getComponentClassManager()

Hi Tim,

I tried ComponentAccessor.getComponentManager() but not works. 

It seems this was written long back not compatible for Jira 7.11.

Anybody can help here?

getProjectComponentManager()

Someone already fixed

https://jira.atlassian.com/browse/JRASERVER-15841?focusedCommentId=1972127&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1972127

 

// This script can be run from Scriptrunner console to set Dashboards listed in the variable dashboardIds as favourites for users & groups listed in the variables userNames & groupNames and share Dashboard to groups listed in the variable sharedGroupsimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.UserManagerimport com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.bc.JiraServiceContextimport com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.portal.PortalPageimport com.atlassian.jira.bc.portal.PortalPageService
import com.atlassian.jira.sharing.SharedEntityimport com.atlassian.jira.sharing.SharePermissionImpl
import com.atlassian.jira.sharing.type.ShareType
import org.apache.log4j.Levellog.setLevel(Level.DEBUG)

List<Integer> dashboardIds = [ // change Dashboard Ids for your case  11111,
  22222,
  33333
]
List<String> groupNames = [ // change group names for your case or comment out this list, if userlist is not needed   'jiragroup1''jiragroup2''jiragroup3' 
]
List<String> userNames = [ // change user names for your case or comment out this list, if userlist is not needed    'user1@company.com',
  'user2@company.com',
  'user3@company.com'
] 
List<String> sharedGroups = [ // change Dashboard shared groups for your case or comment out this list, if userlist is not needed  'jiragroup4',
  'jiragroup5'
]    

PortalPage portalPageList
<ApplicationUser> selGrpUsers
List<String> allUserNames = new ArrayList<>()
ApplicationUser curUser =  ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
JiraServiceContext context = new JiraServiceContextImpl(curUser)
UserManager userManager = ComponentAccessor.getUserManager()
GroupManager groupManager = ComponentAccessor.getGroupManager() 
PortalPageService portalPageService = ComponentAccessor.getComponent(PortalPageService)

if (!groupNames?.isEmpty()) { // read list of group names if non-empty  for (String groupName: groupNames) {
    try{
      selGrpUsers = groupManager.getUsersInGroup(groupName) as List<ApplicationUser>
    } catch (Exception ex) {
      log.error("Can't find Jira group with name '"+groupName+"'\n" + ex)
    }
    if (!selGrpUsers?.isEmpty()) allUserNames += selGrpUsers.collect{it.getName().toLowerCase()}
  }
  log.debug("Usernames list for all groups:\n"+allUserNames)}

if (!userNames?.isEmpty()) allUserNames += userNames.collect{it.toLowerCase()}
allUserNames.unique()
log.debug("Usernames list for all groups and separate users:\n"+allUserNames)

if (!allUserNames?.isEmpty()) {
  for (Long dashboardId: dashboardIds) {
    portalPage =  portalPageService.getPortalPage(context, dashboardId)
    if (portalPage) {
      if (!sharedGroups?.isEmpty()) {
        SharedEntity.SharePermissions perms
        List<String> sharedGroupsResult = new ArrayList<>()
        sharedGroups.each {
          if (groupManager.groupExists(it)) {
            if (groupManager.isUserInGroup(curUser, it)) sharedGroupsResult.add(it)
            else log.error("User '"+curUser.name+"' must be a member of the group '"+it+"' to be able to share Jira dashboard to the group. Can't add this group to Dashboard share.")
          } else log.error("Can't find Jira group with name '"+it+"'. Therefore can't share Jira dashboard to non-existing group.")
        }
        if (!sharedGroupsResult?.isEmpty()) {
          perms = new SharedEntity.SharePermissions(sharedGroupsResult.collect{new SharePermissionImpl(ShareType.Name.GROUP, it, null)}.toSet())
          portalPage = portalPage.portalPage(portalPage)
            .permissions(perms)
            .build()                                                                
        }
      }
      for (String userName: allUserNames) {
        try {
          portalPage = portalPageService.updatePortalPage(context, portalPage, true)
          log.info("Adding portal "+dashboardId?.toString()+" to user: "+userName)
        } catch (Exception ex) {
          log.error("Failed Adding portal "+dashboardId?.toString()+" to user: "+userName+"\n"+ex)
        }
      }
    } else log.error("Can't find Jira dashboard with Id: "+dashboardId)  
  }
} else log.error("List of usernames and usernames from groups is empty. Therefore nothing to do.")

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

274 views 1 2
Join discussion

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