Custom Event Listener
Adding a custom listener
- Navigate to General Configuration > ScriptRunner > Event Listeners.
Choose the Custom Event Listener to use your own scripts to respond to events.
Enter an optional Note to identify your script.
- Enter a Script, either Inline or from a File.
- Select Event(s) for the listener to fire on.
- Select Add.
You can also Preview to view your results before saving the listener.
Writing code for custom event listeners
Script binding
The script binding is a set of variables you can use in your custom script. An event
variable in the script binding corresponds to the event which triggered the listener. For example, if you are listening for PageCreateEvent
the event
variable will be a PageCreateEvent
object.
Multiple Events
You can choose to have your handler listen for multiple events. If you need to do different things depending on the type of event, you can check that with instanceof
. Alternatively, you can type the event
variable to the most specific superclass. In the previous example, that would be PageEvent
.
In the following example, the event
is getting page content for both PageCreateEvent
and PageUpdateEvent
. Since PageEvent
is a common superclass for both PageCreateEvent
and PageUpdateEvent
, you could cover both events by using the following code:
Examples
Add a comment when a banned word is used
Some organizations have a particular style guide or would like to enforce specific rules. This event listener example looks at the content of new pages for banned words. If the page content contains any on a list of banned words, a comment is automatically added with an alternative suggestion.
Follow these steps to create the listener:
- Navigate to General Configuration > ScriptRunner > Event Listeners.
- Select Custom Event Listener.
- Enter Add a comment when a banned word is used for Note.
- Enter the following Script:
- Leave Events blank because the event is defined in the code.
- Select Add.
Result
The following image contains a comment generated in response to a banned word:
Add an inline comment when a banned word is used
Similar to the previous example, you can configure your listener to add inline comments instead. As you get these comments, you can dismiss them.
Follow these steps to create the listener:
- Navigate to General Configuration > ScriptRunner > Event Listeners.
- Select Custom Event Listener.
- Enter Add an inline comment when a banned word is used for Note.
- Enter the following Script:
- Leave Events blank because the event is defined in the code.
- Select Add.
Result
The following image is an example of an inline comment:
Create a page in a space when a user is created
This event listener example automatically creates a user profile page in the Team space. You can use this profile page to list their skills and profile.
Follow these steps to create the listener:
- Navigate to General Configuration > ScriptRunner > Event Listeners.
- Select Custom Event Listener.
- Enter Create a page in space when a user is created for Note.
- Enter the following Script:
- Pick UserCreateEvent for Events.
- Select Add.
Result
The following image is an example of a profile page created when the jbloggs user is created:
Collect stats
This event listener example automatically sends statistics to statsd for page views, space views, and users/pages views.
Follow these steps to create the listener:
- Navigate to General Configuration > ScriptRunner > Event Listeners.
- Select Custom Event Listener.
- Enter Collect stats for Note.
- Enter the following Script:
- Pick PageViewEvent for Events.
- Select Add.
Result
The following image is an example of the statistics for page views: