I wrote the following script to add "jira-administrators" group as a board administrator. This runs in the ScriptRunner console but fails with a
java.lang.UnsupportedOperationException: Not implemented at com.atlassian.greenhopper.service.rapid.view.BoardAdminAOMapper.update(BoardAdminAOMapper.java:73)
error on the last line - here is the code:
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.greenhopper.model.rapid.RapidView;;
import com.atlassian.greenhopper.manager.rapidview.RapidViewManager;
import com.atlassian.greenhopper.service.rapid.view.RapidViewService;
import com.atlassian.greenhopper.model.rapid.BoardAdmin;
import com.atlassian.greenhopper.service.rapid.view.BoardAdminService;
import com.atlassian.greenhopper.model.rapid.BoardAdmin.Type;
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.pyxis.greenhopper.jira")
// We need a user to retrieve and update the board. this should probably be a service account
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
// This gets all of the boards available to the current user
// We are going to randomly pick the first one, just for testing purposes
// In a Listener, the event should provide the board and we won't have to look it up
// Note that a Board is called a RapidView
RapidViewService rapidViewService = PluginModuleCompilationCustomiser.getGreenHopperBean(RapidViewService);
List<RapidView> boards = rapidViewService.getRapidViews(currentUser).get();
RapidView ourBoard = boards[0]
// Now we construct a new list of BoardAdmins using the current BoardAdmin list as our base
BoardAdminService boardAdminService = PluginModuleCompilationCustomiser.getGreenHopperBean(BoardAdminService);
List<BoardAdmin> boardAdmins = boardAdminService.getBoardAdmins(ourBoard).collect {it};
// Add the new admin to the boardAdmins list.
// This line constructs a new BoardAdmin
def group = ComponentAccessor.getGroupManager().getGroup("jira-administrators");
BoardAdmin newAdmin = BoardAdmin.builder().key(group.getName()).type(BoardAdmin.Type.GROUP).build();
boardAdmins.add(newAdmin);
// Update the board admins
boardAdminService.updateBoardAdmins(ourBoard, currentUser, boardAdmins);
the last line fails with the error above.
If I change the last line to
// Update the board admins
boardAdminService.updateBoardAdmins(ourBoard, currentUser, [newAdmin]);
it works. I don't see any difference between the two versions. Both are java.util.ArrayList classes. Both contain valid BoardAdmin objects. I don't understand why the first one fails and the second one runs. Any ideas are greatly appreciated.
Hi there.
This is a useful script, and a tricky question.
I think the problem is wether related to the List, nor to the ObjectType of the Element.
The problem seems to be in the data you fetch to get the board admins. The elements contain an id. But if you want to insert a new admin, the id needs to be empty.
See:
for (f in boardAdminService.getBoardAdmins(ourBoard))
{ log.warn(f) }
BoardAdmin newAdmin = BoardAdmin.builder().key(group.getName()).type(BoardAdmin.Type.GROUP).build();
log.warn(newAdmin)
Conclusion: I think you'll need to convert the existing admins to users and re-build an admin object, so that the id gets deleted.
Hope this works
Cheers Leonard
Your response was spot on. I modified the script to create a new list of admins using the original list as input and it works as expected. I appreciate the help.
For anyone who wants to see the correct solution
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.greenhopper.model.rapid.RapidView;;
import com.atlassian.greenhopper.manager.rapidview.RapidViewManager;
import com.atlassian.greenhopper.service.rapid.view.RapidViewService;
import com.atlassian.greenhopper.model.rapid.BoardAdmin;
import com.atlassian.greenhopper.service.rapid.view.BoardAdminService;
import com.atlassian.greenhopper.model.rapid.BoardAdmin.Type;
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.pyxis.greenhopper.jira")
// We need a user to retrieve and update the board. this should probably be a service account
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
// This gets all of the boards available to the current user
// We are going to randomly pick the first one, just for testing purposes
// In a Listener, the event should provide the board and we won't have to look it up
// Note that a Board is called a RapidView
RapidViewService rapidViewService = PluginModuleCompilationCustomiser.getGreenHopperBean(RapidViewService);
List<RapidView> boards = rapidViewService.getRapidViews(currentUser).get();
RapidView ourBoard = boards[0]
// Now we construct a new list of BoardAdmins using the current BoardAdmin list as our base
BoardAdminService boardAdminService = PluginModuleCompilationCustomiser.getGreenHopperBean(BoardAdminService);
List<BoardAdmin> boardAdmins = boardAdminService.getBoardAdmins(ourBoard).collect {
BoardAdmin.builder().key(it.getKey()).type(it.getType()).build();
};
// Add the new admin to the boardAdmins list.
// This line constructs a new BoardAdmin
def group = ComponentAccessor.getGroupManager().getGroup("jira-administrators");
BoardAdmin newAdmin = BoardAdmin.builder().key(group.getName()).type(BoardAdmin.Type.GROUP).build();
boardAdmins.add(newAdmin);
// Update the board admins
boardAdminService.updateBoardAdmins(ourBoard, currentUser, boardAdmins);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This is exactly what I've been trying to write. Thanks for this. Question though, I can't figure out how to use the list "boards" to add the admin across all boards. I've tried to accomplish this through iterating though "boards", but it keeps erroring out.
Can you provide the modification to the above code that uses "boards" instead of "ourBoard" to update the admins?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Jeremy Jedlicka - This is simply a matter of iterating through the different boards in the list. Here is the modified code.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.groups.GroupManager;
import com.atlassian.greenhopper.model.rapid.RapidView;;
import com.atlassian.greenhopper.manager.rapidview.RapidViewManager;
import com.atlassian.greenhopper.service.rapid.view.RapidViewService;
import com.atlassian.greenhopper.model.rapid.BoardAdmin;
import com.atlassian.greenhopper.service.rapid.view.BoardAdminService;
import com.atlassian.greenhopper.model.rapid.BoardAdmin.Type;
import com.onresolve.scriptrunner.runner.customisers.PluginModuleCompilationCustomiser
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
@WithPlugin("com.pyxis.greenhopper.jira")
// We need a user to retrieve and update the board. this should probably be a service account
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
// This gets all of the boards available to the current user
// We are going to randomly pick the first one, just for testing purposes
// In a Listener, the event should provide the board and we won't have to look it up
// Note that a Board is called a RapidView
RapidViewService rapidViewService = PluginModuleCompilationCustomiser.getGreenHopperBean(RapidViewService);
List<RapidView> boards = rapidViewService.getRapidViews(currentUser).get();
// This iterates over each board updating the admins
boards.each { ourBoard ->
// Now we construct a new list of BoardAdmins using the current BoardAdmin list as our base
BoardAdminService boardAdminService = PluginModuleCompilationCustomiser.getGreenHopperBean(BoardAdminService);
List<BoardAdmin> boardAdmins = boardAdminService.getBoardAdmins(ourBoard).collect {
BoardAdmin.builder().key(it.getKey()).type(it.getType()).build();
};
// Add the new admin to the boardAdmins list.
// This line constructs a new BoardAdmin
def group = ComponentAccessor.getGroupManager().getGroup("jira-administrators");
BoardAdmin newAdmin = BoardAdmin.builder().key(group.getName()).type(BoardAdmin.Type.GROUP).build();
boardAdmins.add(newAdmin);
// Update the board admins
boardAdminService.updateBoardAdmins(ourBoard, currentUser, boardAdmins);
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.