Hello fellow Scriptrunner fans,
I've been using ScriptRunner for over a year now and I've cobbled together the ability to find scripts from these forums or from Adaptavist's script library or from Scriptrunner documentation itself. Basically I'm still a beginner.
I can even modify these scripts now in a basic way to adapt them to what I need done. But now my needs are growing and when I can't find something already pre-written I'm at a loss.
What resources did you use to learn how to create these scripts? Many times there are things in the Adaptavist Script Library and I have no idea why/how they work, they just do. I'd like to learn the how/why. I'm hoping/wishing there is some great ScriptRunner script development book out there that I can read and unlock the mysteries of Grovvy script writing.
For example, I want to build a script that I can use in the script console that takes 1 user that I supply and will add this user to a project role (that I supply) to X number of projects which I supply. I want to learn how to build this myself.
I see things like componentaccessor and all these other Import lines but I have no idea why they are needed, I just import them. I just recently discovered I can use the code editor and hit ctrl-space or ctrl-j and learn about available methods or see the documentation but often times the documentation is difficult to understand. I seem to learn best by seeing functioning code and then tweaking that. I don't know how to translate the code found in the official documentation. Where's my Rosetta stone for this stuff?
For example, from the the latest jira api 8.9.0 I found this;
void addActorsToProjectRole(Collection<String> actors, ProjectRole projectRole, Project project, String actorType, ErrorCollection errorCollection)
Just by the name this sounds real promising for what I want to do. What does this look like in real code in a real groovy script?
Do I replace the blue? What does the blue mean? Do I need to include the blue code? Does Project project need to be there is only 1 project necessary. How do I know how to interpret this?
void addActorsToProjectRole(Collection<String> 'mx45678', <--- user i want to add ProjectRole team contributor, <-- a project role Project TOKE, <-- my project key but i have more String actorType, <--No idea what this is ErrorCollection errorCollection) <-- No idea what this is
Hi @Brian
Let me share few things.
"What resources did you use to learn how to create these scripts? Many times there are things in the Adaptavist Script Library and I have no idea why/how they work, they just do. I'd like to learn the how/why. I'm hoping/wishing there is some great ScriptRunner script development book out there that I can read and unlock the mysteries of Grovvy script writing."
"For example, I want to build a script that I can use in the script console that takes 1 user that I supply and will add this user to a project role (that I supply) to X number of projects which I supply. I want to learn how to build this myself."
"I see things like componentaccessor and all these other Import lines but I have no idea why they are needed, I just import them. I just recently discovered I can use the code editor and hit ctrl-space or ctrl-j and learn about available methods or see the documentation but often times the documentation is difficult to understand. I seem to learn best by seeing functioning code and then tweaking that. I don't know how to translate the code found in the official documentation. Where's my Rosetta stone for this stuff?"
"
void addActorsToProjectRole(Collection<String> actors, ProjectRole projectRole, Project project, String actorType, ErrorCollection errorCollection)"
Few other things I can share.
Finally I would say respond to others' questions on the community. Just by helping them, solving their problem you will also build your knowledge.
I hope it helps.
Ravi
Wow, is this the YouTube famous Ravi? Thank you so much your time and effort to respond. I have watched over half of your Scriptrunner videos on YouTube. I've learned a ton. Thank you for creating those videos.
I just purchased a Groovy course to learn more about what collect, closures, strings, methods, classes and objects are. Classes, objects, closures still mystify me.
I've actually got some notes on different ideas that I'd like you to make YouTube videos on. If you take suggestions, please let me know and I'll respond here.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks :) Yes definitely, do give your suggestions.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That is such a great question and wonderfully stated too.
I wish I had had the answer when I got started. I am not aware of any good scriptrunner development bible or reference.
After a few years of scriptrunner/groovy hacking, I'm probably at the point where you wish you were. But I got there the same way you are... cobbling lots of scripts together, lots of googling, lots of exploration of the javadocs.
A little bit of java experience probably would have helped, but I picked up bits and pieces along the way enough to be able to get by.
Maybe explaining how javadocs work would be helpful here.
The "blue" text actually represents the type of data that the method is expecting. So in your example, this means:
actorType
- is a type that defines the type of role actor to instantiate (ex./ UserRoleActor.TYPE, GroupRoleActor.TYPE)". So look for a class in the Javadoc called UserRoleActor. I found MockUserRoleActor. Not quite, but close. Looking at that I see that it inherits fields from ProjectRoleActor. So a quick console script "return com.atlassian.jira.security.roles.ProjectRoleActor.USER_ROLE_ACTOR_TYPE" actually returns the string "atlassian-user-role-actor". That seems to fit.So putting it all together, I cobbled this script to add user abc to Users role in project JSP
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.component.ComponentAccessor
def projectManager = ComponentAccessor.projectManager
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def projectroleService = ComponentAccessor.getComponent(ProjectRoleService)
def project = projectManager.getProjectObjByKey('JSP')
def userRole = projectRoleManager.getProjectRole('Users')
projectroleService.addActorsToProjectRole(['abc'] as Collection, userRole, project, ProjectRoleActor.USER_ROLE_ACTOR_TYPE , null)
You may notice that you only need import for classes you actually use and define explicitly.
I could import the PrjectManager class and then define the projectManager object as
ProhectManager projectManager =ComponentAccessor.getProjectManager()
But that doesn't seem necessary to me, I like to keep my script as short and tight as possible, So I use def to let groovy guess the class. It does a pretty good job at it.
I hope I've included enough of my thinking/exploration process you give you a leg up into this adventure.
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.